my code has 3 classes user
, book
and userList
the user list is a class that creates an array of users. I am a beginner so any help would be greatly appreciated.
I will list the file names and their code after
user.h
#pragma once
#ifndef USER_H
#define USER_H
#include<cstring>
#include<iostream>
#pragma warning( disable : 4716 )
using namespace std;
class user
{
private:
string name;
int age;
int id;
string password;
string email;
public:
static int count;
void setName(string Name);
string getName();
void setPassword(string Password);
string getPassword();
void setEmail(string Email);
string getEmail();
void setAge(int Age);
int getAge();
void setId();
int getId();
user();
user(string Name, int Age, string Email, string Password);
user(const user& u);
bool operator ==(const user& u);
friend ostream& operator<<(ostream& output, const user& u);
friend istream& operator>>(istream& input, user& u);
};
#endif
user.cpp
#include "user.h"
void user::setName(string Name)
{
name = Name;
}
string user::getName()
{
return name;
}
void user::setPassword(string Password)
{
password = Password;
}
string user::getPassword()
{
return password;
}
void user::setEmail(string Email)
{
Email = email;
}
string user::getEmail()
{
return email;
}
void user::setAge(int Age)
{
age = Age;
}
int user::getAge()
{
return age;
}
void user::setId()
{
count++;
id = count;
}
int user::getId()
{
return id;
}
user::user()
{
age = 0;
name = password = email = "";
}
user::user(string Name, int Age, string Email, string Password)
{
name = Name;
password = Password;
email = Email;
age = Age;
}
user::user(const user& u)
{
name = u.name;
age = u.age;
email = u.email;
password = u.password;
id = u.id;
}
bool user::operator==(const user& u)
{
bool status;
if (name == u.name && age == u.age && email == u.email && id == u.id)
{
status = true;
}
else
{
status = false;
}
}
ostream& operator<<(ostream& output, const user& u)
{
output << endl << "User name :" << u.name << endl << "User id :" << u.id << endl << "User age :" << u.age << endl << "User email :" << u.email << endl << "User password :" << u.password << endl;
}
istream& operator>>(istream& input, user& u)
{
input >> u.name >> u.age >> u.email >> u.password;
}
Book.h
#pragma once
#ifndef BOOK_H
#define BOOK_H
#include<cstring>
#include<iostream>
#include "user.h"
#pragma warning( disable : 4716 )
using namespace std;
class Book
{
private:
string title;
string isbn;
int id;
string category;
double averageRating;
user author;
public:
static int count;
void setTitle(string Title);
string getTitle();
void setISBN(string ISBN);
string getISBN();
void setId();
int getId();
void setCategory(string Category);
string getCategory();
void setRating(double Rating);
double getRating();
void setAuthor(user User);
user getAuthor();
Book();
Book(const Book& b);
bool operator==(const Book& b);
friend ostream& operator<<(ostream& output, const Book& b);
friend istream& operator>>(istream& input, Book& b);
};
#endif
Book.cpp
#include "Book.h"
void Book::setTitle(string Title)
{
title = Title;
}
string Book::getTitle()
{
return title;
}
void Book::setISBN(string ISBN)
{
isbn = ISBN;
}
string Book::getISBN()
{
return isbn;
}
void Book::setId()
{
count++;
id = count;
}
int Book::getId()
{
return id;
}
void Book::setCategory(string Category)
{
category = Category;
}
string Book::getCategory()
{
return category;
}
void Book::setRating(double Rating)
{
averageRating = Rating;
}
double Book::getRating()
{
return averageRating;
}
void Book::setAuthor(user User)
{
author = User;
}
user Book::getAuthor()
{
return author;
}
Book::Book()
{
title = isbn = category = "";
averageRating = 0;
}
Book::Book(const Book& b)
{
title = b.title;
isbn = b.isbn;
category = b.category;
author = b.author;
id = b.id;
averageRating = b.averageRating;
}
bool Book::operator==(const Book& b)
{
bool status;
if (title == b.title && isbn == b.isbn && category == b.category && id == b.id && author == b.author)
{
status = true;
}
else
{
status = false;
}
}
ostream& operator<<(ostream& output, const Book& b)
{
output << endl << "Book title :" << b.title << endl << "Book ISBN :" << b.isbn << endl << "Book ID :" << b.id << endl << "Book category :" << b.category << endl << "Average rating :" << b.averageRating << endl;
}
istream& operator>>(istream& input, Book& b)
{
input >> b.title >> b.isbn >> b.category;
}
UserList.h
#pragma once
#include<cstring>
#include<iostream>
#include "user.h"
using namespace std;
class UserList
{
private:
user* users;
int capacity;
int usersCount;
public:
UserList(int Capacity);
void addUser(user user);
user& searchUserID(int id);
user& searchUserName(string name);
void deleteUser(int id);
friend ostream& operator<<(ostream& output, const UserList& userList);
~UserList();
};
UserList.cpp
#include "UserList.h"
UserList::UserList(int Capacity)
{
usersCount = 0;
capacity = Capacity;
user udef;
users = new user[capacity];
for (int i = 0; i < capacity; i++)
{
users[i] = udef;
}
}
void UserList::addUser(user newuser)
{
users[usersCount] = newuser;
usersCount++;
}
user& UserList::searchUserName(string name)
{
cout << "Enter the user's name" << endl;
cin >> name;
for (int i = 0; i < usersCount; i++)
{
if (name == users[i].getName())
{
cout << users[i];
}
else cout << "USER NOT FOUND !!!!" << endl;
}
}
user& UserList::searchUserID(int id)
{
cout << "Enter the user's ID" << endl;
cin >> id;
for (int i = 0; i < usersCount; i++)
{
if (id == users[i].getId())
{
cout << users[i];
}
else cout << "USER NOT FOUND !!!!" << endl;
}
}
void UserList::deleteUser(int id)
{
cout << "Enter the user's ID" << endl;
cin >> id;
int i;
for (i = 0; i < usersCount; i++)
{
if (id == users[i].getId())
{
break;
}
}
for (i; i < usersCount; i++)
{
users[i] = users[i + 1];
}
usersCount--;
}
ostream& operator<<(ostream& output, const UserList& userList)
{
for (int i = 0; i < userList.usersCount; i++)
{
output << "---- USER " << i + 1 << "----" << userList.users[i];
}
}
UserList::~UserList()
{
delete users;
}
main.cpp
#include<cstring>
#include<iostream>
#include "user.h"
#include "Book.h"
#include "UserList.h"
#pragma warning( disable : 4716 )
using namespace std;
int user::count = 0;
int Book::count = 0;
int main()
{
int size;
cout << "enter the size of the list" << endl;
cin >> size;
UserList ul1(size);
cout << ul1 << endl;
return 0;
}