How to relate user with a book (Library system) C++ - c++

I am doing a university project and I am making a Library System.
It has a few restrictions I cannot use what we haven't been taught (so no vectors or std:: things).
Now, I have some classes made.
(1) Date
(2) Book
(3) Student
(4) Library //Haven't made it yet
Now, when a student/user issues a book their account number is being saved in "Book" class and so is the return_date.
Now, I wish to make a pointer or use this-> operator or something that will be in Student class but point to a Book class.
So, when Student wants to see which books they have currently issued will be displayed.
I am not sure how to do that.
Should I make a Library class, give it an array of "Book" class by composition and then make a member function in "Student/User" class and display it there?
I really want to use new/delete keyword, this, or pointers to do this. It's not compulsory but it will help me understand those concepts.
The following are my Book, Student and Date classes.
Date Class:
class Date
{
int Day; //1-31 based on month
int Month; //1-12
int Year; //any year
int checkDay(int );
int Late_Days;
public:
static const int Days_per_Month[13];
Date()
{
Day=0;
Month=0;
Year=0;
}
Date (int dy, int mn, int yr)
{
if (mn>0 && mn <=12)
Month=mn;
else
{
Month=1;
cout<<"Month "<<mn<<" invalid. Set to month 1"<<endl;
}
Year=yr;
Day=checkDay(dy);
}
void setDay(int d) {Day=d;}
void setMonth(int m) {Month=m;}
void setYear(int y) {Year=y;}
void setLateDay(int LD) {Late_Days=LD;}
int getDay() {return Day;}
int getMonth() {return Month;}
int getYear() {return Year;}
int getLateDay() {return Late_Days;}
void Print_Date()
{
cout<<getDay()<<"/"<<getMonth()<<"/"<<getYear()<<endl;
//cout<<Day<<"/"<<Month<<"/"<<Year<<endl;
}
void Update_Date()
{
cout<<"\nEnter Day: ";
cin>>Day;
cout<<"Enter Month: ";
cin>>Month;
cout<<"Year: ";
cin>>Year;
}
void increment_date(int num)
{
int day;
int month_new;
// Day=Day+num;
setDay(getDay()+num);
if( getDay()>Days_per_Month[getMonth()] )
{
day=getDay()-Days_per_Month[getMonth()];
setDay(day);
setMonth(getMonth()+1);
if(Days_per_Month[getMonth()]>12)
{
month_new=1;
setMonth(month_new);
setYear(getYear()+1);
}
}
Print_Date();
}
const int Date:: Days_per_Month[13]={0,31,28,31,30,31,30,31, 31, 30, 31, 30, 31};
int Date::checkDay(int testday) //returntype classname :: funcname (parameteres)
{
//static const int Days_per_Month[13]={0,31,28,31,30,31,30,31, 31, 30, 31, 30, 31};
if(testday > 0 && testday <= Days_per_Month[Month])
return testday;
if ( Month==2 && testday==29 && (Year%400==0 || (Year%4==0 && Year%100!=0)) ) //for leap year
return testday;
cout<<"Day "<<testday<<" invalid. Set to day 1."<<endl;
return 1;
}
Book Class
class Book
{
string Title;
string Author;
unsigned long int ISBN;
int Year_of_Publication;
int Library_Code;
string Category;
string Status;
unsigned int Account_Number; //if issued
int Copies_of_Book;
Date Return_Date;
public:
static int Copy_Number;
Book()
{
Title=" ";
Author=" ";
ISBN=0;
Year_of_Publication=0;
Library_Code=0;
Category=" ";
Status=" ";
Account_Number=0;
Copies_of_Book=0;
Return_Date.setDay(0);
Return_Date.setMonth(0);
Return_Date.setYear(0);
}
Book(string t, string a,unsigned long int isbn, int yop, int libcode, string c, string s, unsigned int an, int cob)
{
setTitle(t);
setAuthor(a);
setISBN(isbn);
setYOP(yop);
setLibraryCode(libcode);
setCategory(c);
setStatus(s);
setAccountNum(an);
setCopiesBook(cob);
}
void setTitle(string T) {Title=T;}
void setAuthor(string A) {Author=A;}
void setISBN(unsigned long int isbn){ISBN=isbn;}
void setYOP(int yop) {Year_of_Publication=yop;}
void setLibraryCode(int libcode) {Library_Code=libcode;}
void setCategory(string c) {Category=c;}
void setStatus(string s) {Status=s;}
void setAccountNum(unsigned int an) {Account_Number=an;}
void setCopiesBook(int cob) {Copies_of_Book=cob;}
void setCopyNumber(int cn) {Copy_Number=cn;}
string getTitle() {return Title;}
string getAuthor() {return Author;}
unsigned long int getISBN() {return ISBN;}
int getYOP() {return Year_of_Publication;}
int getLibraryCode() {return Library_Code;}
string getCategory() {return Category;}
string getStatus() {return Status;}
unsigned int getAccountNum() {return Account_Number;}
int getCopiesBook() {return Copies_of_Book;}
int getCopyNumber() {return Copy_Number;}
void Input_New_Book()
{
cout<<"\nEnter Title: ";
cin>>Title;
cout<<"Enter Author: ";
cin>>Author;
cout<<"Enter ISBN: ";
cin>>ISBN;
cout<<"Enter Year of Publication: ";
cin>>Year_of_Publication;
cout<<"Enter Library Code: ";
cin>>Library_Code;
cout<<"Enter Category: ";
cin>>Category;
cout<<"Enter Total Copies Number: ";
cin>>Copies_of_Book;
cout<<"Enter Status: ";
cin>>Status;
if(Status=="Issued")
{
cout<<"Enter Account Number: ";
cin>>Account_Number;
Return_Date.Update_Date();
}
}
void Display_Book_Info()
{
cout<<"\nTitle: "<<getTitle()<<endl;
cout<<"Author: "<<getAuthor()<<endl;
cout<<"ISBN: "<<getISBN()<<endl;
cout<<"Year of Publication: "<<getYOP()<<endl;
cout<<"Library Code: "<<getLibraryCode()<<endl;
cout<<"Category: "<<getCategory()<<endl;
cout<<"Total Copies: "<<getCopiesBook()<<endl;
cout<<"Status: "<<getStatus()<<endl;
if(getStatus()=="Issued")
{
cout<<"Account Number: "<<getAccountNum()<<endl;
cout<<"Return Date: ";
Return_Date.Print_Date();
}
}
void Update_Status()
{
unsigned int AN;
Display_Book_Info();
cout<<"Please enter the updated status of the book: ";
cin>>Status; //We cannot use string in an switch, so we are using if.
if (Status=="Lost")
{
cout<<"The book is lost."<<endl;
setAccountNum(0);
}
else if (Status=="Available")
{
cout<<"Book is available now to issue."<<endl;
setAccountNum(0); //This indicates it doesn't have nay account number.
}
else if(Status=="Issued")
{
cout<<"The book is in possession of someone."<<endl;
cout<<"Enter their account number:";
cin>>AN;
setAccountNum(AN);
}
else if (Status=="Order")
{
cout<<"The book is in process of being ordered."<<endl;
setAccountNum(0);
}
else
{
cout<<"Wrong entry!\nBook's Status is set to default. (Available)"<<endl;
Status="Available";
setAccountNum(0);
}
cout<<"Status updated successfully!"<<endl;
}
void Issue_Book(int day_num) //This day_num indicated number of days.
{
unsigned int an;
cout<<"Enter account number of User:";
cin>>an;
setAccountNum(an);
Return_Date.Update_Date();
cout<<"Book issued for "<<day_num<<" days."<<endl;
cout<<"Return Date: ";
Return_Date.Print_Date();
}
void ReIssue_Book(int day_num)
{
unsigned int an;
cout<<"Enter account number of User:";
cin>>an;
setAccountNum(an);
Return_Date.increment_date(day_num);
cout<<"Book issued for "<<day_num<<" more days."<<endl;
cout<<"Return Date: ";
Return_Date.Print_Date();
}
void Return_Book()
{
cout<<"Book returned from User: "<<getAccountNum()<<endl;
setAccountNum(0);
cout<<"Book returned successfully!"<<endl;
}
}; //end of class book
int Book::Copy_Number=0;
Student Class
class Student:public Book
{
string Name;
unsigned int Account_Number;
double Fine;
int Books_Issue;
public:
Student()
{
Name=" ";
Account_Number=0;
Fine=0;
Books_Issue=0;
}
void setName(string n) {Name=n;}
void setAccount_Number(unsigned int an) {Account_Number=an;}
void setFine(double f) {Fine=f;}
void setBooksIssue(int n) {Books_Issue=n;}
string getName() {return Name;}
unsigned int getAccount_Number() {return Account_Number;}
double getFine() {return Fine;}
int getBooksIssue() {return Books_Issue;}
void Input_Student_info()
{
cout<<"\nEnter Name: ";
cin>>Name;
cout<<"Enter Account Number: ";
cin>>Account_Number;
}
void Display_Student_info()
{
cout<<"Name: "<<getName()<<endl;
cout<<"Account Number: "<<getAccount_Number()<<endl;
cout<<"Total Fine: "<<getFine()<<endl;
cout<<"Books issued: "<<getBooksIssue()<<endl;
}
void Issue_a_book()
{
if(Books_Issue<=3)
{
Issue_Book(7); //This will issue a book and give it a 7 days return date time
Books_Issue++; //This will mean a book has been issued.
}
else
cout<<"Cannot issue book. You have reached your maximum limit.\nReturn a book in order to issue another."<<endl;
}
void Display_All_Books() //which are currently issued
{
for (int i=0 ; i<=getBooksIssue() ; i++)
{
}
}
};
Sorry for posting huge chunks of code. But some people say we should post the whole code, so this is why I posted all of it organized way.
The main question is:
How should I display issued books in a Student's class?
Should I make a Library class (as a parent class or child class, or use composition and arrays of Book class) first or at the very end?
Let me know what is the best way to do this.
Thank You so much.

It is better to have remove the user issue details from the book class...
Let the book class have only details about the book and methods to update and retrieve book details.
Add the user issue details into a new class which inherits the book class... This way is more modular and keeps the books as separate entities. Now you can make a linked list(as they are dynamic) or array of these classes, each holding a book and the issue and return dates for that particular student.
for example:
class books_issued{
public: book issued_book;
books_issued *new;
<put your issue data like:>
unsigned int Account_Number; //if issued
int Copies_of_Book;
Date Return_Date;
<and issue related methods:>
};
now declare an object of books_issued class in student class,
and write a method to add, remove and view books that have been issued by that particular student.
--- If you need any help with creating linked lists for this purpose, let me know I will be more than happy to help as I had done a very similar library management program for my project as well :-) ---

Related

Im trying to assign value to a array of char using strcpy(), but it gives a error that cannot convert char** to char*

I'm working on a assignment and I can't seem to figure out the reason of the error. The strcpy() function was working when I tried on the University's PC, now I'm trying to do it at home and its not working properly.
#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
class Employee{
int E_Id;
char*E_Name[30];
int No_Hours;
int Rate_Hour;
public:
void SetData(int Id, char*Name[30], int Hours, int Rate)
{
E_Id = Id;
strcpy(E_Name,Name); //Error Here
No_Hours = Hours;
Rate_Hour = Rate;
}
void DispData()
{
cout<<"Employee ID: "<<E_Id<<endl;
cout<<"Employee Name: "<<E_Name<<endl;
cout<<"Number of Hours: "<<No_Hours<<endl;
cout<<"Rate per Hour: "<<Rate_Hour<<endl;
}
void InputData()
{
cout<<"Give Employee ID: ";
cin>>E_Id;
cout<<"Give Employee Name: ";
cin>>E_Name;
cout<<"Give Number of Hours: ";
cin>>No_Hours;
cout<<"Give Rate per Hour: ";
cin>>Rate_Hour;
}
int GetEId()
{
return E_Id;
}
char*GetEName()
{
return E_Name;
}
int GetNoHours()
{
return No_Hours;
}
int GetRateHour()
{
return Rate_Hour;
}
Employee()
{
PId = 0;
strcpy(E_Name, "")
No_Hours = 0;
Rate_Hour = 0;
}
Employee(int Id, char*Name, int Hours, int Rate)
{
E_Id = Id;
strcpy(E_Name, Name); //Error Here
No_Hours = Hours;
Rate_Hour = Rate;
}
~Employee()
{
cout<<"Obeject Destroyed"<<endl;
}
};
int main()
{
Employee*e;
e = new Employee[10];
int i;
cout<<"Give Data"<<endl;
for(i=0;i<10;i++)
{
(e+i)->InputData();
}
int high = (e+0)->GetNoHours()*(e+0)->GetRateHours();
int loc = 0;
for(i=0;i<10;i++)
{
if((e+i)->GetNoHours()*(e+i)->GetRateHours()>high)
{
high = (e+i)->GetNoHours()*(e+i)->GetRateHours();
loc = i;
}
}
cout<<"Employee with Highest Salary"<<endl;
(e+loc)->DispData();
delete[]e;
getch();
return 0;
}
In this program have to use pointers to make an array of 10 employees and tell which employee earns the most salary.
This is wrong
char*E_Name[30]; // array of char pointers
it should be
char E_Name[30]; // array of chars
An array of chars can hold a string. An array of char pointers is something else.
This is wrong
void SetData(int Id, char*Name[30], int Hours, int Rate)
it should be
void SetData(int Id, char*Name, int Hours, int Rate)
Since you cannot have an array as a parameter to a function you use a pointer instead. So if you want to pass an array of char to a function, the function should be declared with a pointer to char.
Basically you should be using either char arrays or char pointers, but not both combined.

Having issue with base constructors used in derived class

I'm running a code in which I'm using two base classes and deriving a derived class from them. I'm calling both of their constructors in the Derived class and I'm giving arguments in main. When I compile it doesn't give me any kind of error. But when I try to run the program it doesn't run, I'm really confused!
Here is my code
#include <iostream>
#include<iomanip>
using namespace std;
class Date
{
int day;
int month;
int year;
public:
Date(int d,int m,int y)
{
day=d;
month=m;
year=y;
}
void display()
{
cout<<endl<<day<<"\\"<<month<<"\\"<<year<<endl;
}
void set()
{
cout<<"Enter day :";
cin>>day;
cout<<"Enter month :";
cin>>month;
cout<<"Enter year :";
cin>>year;
}
// sets the date members
};
class Time
{
int hour;
int minute;
int second;
public:
Time(int h,int min,int s)
{
hour=h;
minute=min;
second=s;
}
void display() // displays the time
{
cout<<endl<<hour<<":"<<minute<<":"<<second<<endl;
}
void set()
{
cout<<"Enter hour :";
cin>>hour;
cout<<"Enter minute :";
cin>>minute;
cout<<"Enter seconds :";
cin>>second;
}
// sets the time members
};
class DateAndTime : public Date, public Time
{
int digital;
public:
DateAndTime(int a,int b,int c,int d,int e,int f):
Date(a,b,c),Time(d,e,f)
{
}
void set()
{
Date:set();
Time:set();
}
void display()
{
Date:display();
Time:display();
}
// prints date and time
};
int main()
{
DateAndTime Watch(17,02,1999,03,36,56);
Watch.display();
Watch.set();
Watch.display();
return 0;
}
The function
void set()
{
Date:set();
Time:set();
}
is wrong but the compiler does not complain because it treats Date: and Time: as labels. If you separate the labels and the function calls, the function is:
void set()
{
Date: // Unused label.
set(); // Calls the function again, leading to infinite recursion
// and stack overflow.
Time: // Unused label.
set(); // The function never gets here.
}
You need to use:
void set()
{
Date::set();
Time::set();
}
You need to update display similarly. Use:
void display()
{
Date::display();
Time::display();
}

Parsing returned value to a another method

I want float variable 'avg' to be returned and then in the main pass it to 'void batsman::display(float a)' using a parameter. then display the average marks in display method. this method gives me 2 errors. any other way?
#include<iostream.h>
class batsman
{
int marks[5];
char name[15],country[15];
public:
void input();
float cal();
void display();
};
void batsman::input()
{
int i;
cout<<"Enter player name: ";
cin>>name;
cout<<"Enter player country: ";
cin>>country;
cout<<"Enter player marks"<<"\n";
for(i=0;i<5;i++)
{
cout<<"Mark "<<i+1<<": ";
cin>>marks[i];
}
}
float batsman::cal()
{
int i;
int tot=0;
float avg;
for(i=0;i<5;i++)
{
tot=tot+marks[i];
}
avg=(float)tot/5;
return avg;
}
void batsman::display(float a)
{
float avg1;
avg1=a;
cout<<"Player name: "<<name<<"\n";
cout<<"Player country: "<<country<<"\n";
cout<<"Average: "<<avg1<<"\n";
}
int main()
{
batsman b1;
b1.input();
b1.cal();
b1.display(b1.batsman::cal());
//cout<<"Average: "<<b1.batsman::cal()<<"\n";
}
The code has several errors.
iostream.h should be iostream
using namespace std; // add this at top so that cout is found
display() should be display(float a) in the class declaration.
After those changes the code ran as expected.
Change display's definition in your class to display(float a), and prepend std:: to cout and cin. As a recommendation, don't use iostream.h, use iostream.

Pointer to an object in inheritance

Here I'm not getting correct output, Here I used inheritance concept, but I don't know, how to call method using object of pointer type.
Please some one give me some solution.
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class person
{
private:
char *name,*blood,*dob;
int height,weight;
public:
static int count;
person()
{
strcpy(name,"Name");
strcpy(blood,"Blood");
height=0;
weight=0;
}
~person()
{
}
friend class person_info;
};
class person_info
{
private:
char *add,*tel,*lic,*insu;
public:
void getdata(person *obj,int n);
void display(person *obj);
};
void person_info::getdata(person *obj,int n)
{
for(int i=0;i<n;i++)
{
cin.ignore();
cout<<"Enter Name Of the Person : ";
cin.getline(obj->name,30);
cout<<"Enter Blood group Of the Person : ";
cin.getline(obj->blood,30);
cout<<"Enter date of birth of the Person : ";
cin.getline(obj->dob,30);
cout<<"Enter Height of the Person : ";
cin>>obj->height;
cout<<"Enter Weight of the Person : ";
cin>>obj->weight;
cout<<"Enter Address Of the Person : ";
cin.getline(this->add,30);
cout<<"Enter Insurance no. Of the Person : ";
cin.getline(this->insu,30);
cout<<"Enter Telephone no Of the Person : ";
cin.getline(this->tel,30);
cout<<"Enter License no the Person : ";
cin.getline(obj->blood,30);
}
}
void person_info::display(person *obj)
{
cout<<setw[10]<<"Name"<<setw[10]<<"Address"<<setw[10]<<"D. O. B."<<setw[3]<<"Blood G."<<setw[4]<<"Height"<<setw[10]<<"Weight"<<setw[10]<<"Insrn No."<<setw[10]<<"Tele No."<<setw[11]<<"Licence No.";
cout<<setw[10]<<obj->name<<setw[10]<<this->add<<setw[10]<<obj->dob<<setw[3]<<obj->blood<<setw[4]<<obj->height<<setw[10]<<obj->weight<<setw[10]<<this->insu<<setw[10]<<this->tel<<setw[11]<<this->lic;
}
int main()
{
int ch=0,i=0,n;
do
{
cout<<"1.getdata"<<endl;
cout<<"2.display data"<<endl;
cout<<"Enter choice";
cin>>ch;
person_info *p[2]; \\I think this part is not correct
p[2]=new person_info(); \\I think this part is not correct
person *p1[2]; \\I think this part is not correct
p1[2]=new person(); \\I think this part is not correct
switch(ch)
{
case 1:
cout<<"Enter No. Entries to be Entered :";
cin>>n;
p[2]->getdata(p1[2],n);
break;
case 2:
for(int j=0;j<i;j++)
{
p[i]->display(p1[i]);
}
break;
}
}while(ch!=3);
You neither initialized your members char *name,*blood,*dob;, nor you allocated memory for them. I recommend to use std::string instead of char*.
#include <string>
class person
{
private:
std::string name;
std::string blood;
public:
person()
: name( "Name" )
, blood( "Blood" )
{}
person( const char *n, const char *b )
: name( n )
, blood( b )
{}
};
Note: strcpy(name,"Name"); copies the string "Name" to the memory referenced by char *name, but you never allocate any dynamic memory for name. name is not initialized and undefined.

c++ printing object and constructor

Hello everyone I had a problem while running the following program in the last loop it only print three 0 without displaying any result I am not sure what is wrong with the program,perhaps it has something to do with the constructor, however when I write my getter method in the second last loop with setter method it display the correct result. any help will be appreciated.
#ifndef EMPLOYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED
#include<string>
using namespace std;
class Employee
{
private:
string name;
int idnumber;
string department;
string position;
public:
Employee(string ,int ,string ,string);
void setName(string );
void setDepartment(string);
void setPosition(string);
void setIDNumber(int);
string getName() const
{
return name;
}
string getDepartment() const
{
return department;
}
string getPosition() const
{
return position;
}
int getIDnumber() const
{
return idnumber;
}
};
#include "Employee.h";
#include <iostream>
#include <iomanip>
using namespace std;
Employee::Employee(string name ,int IDNumber ,string department ,string position)
{
name=name;
idnumber=IDNumber;
department=department;
position=position;
}
void Employee::setDepartment(string Department)
{
department=Department;
}
void Employee::setName(string Name)
{
name=Name;
}
void Employee::setPosition(string Position)
{
position=Position;
}
void Employee::setIDNumber(int Number)
{
idnumber=Number;
}
int main()
{
string name;
int IDNumber;
string department;
string position;
const int Item=3;
Employee info1(" ",0," "," ");
Employee info2(" ",0," "," ");
Employee info3(" ",0," "," ");
Employee Info[Item]={info1,info2,info3};
}
for(Employee element:Info)
{
cout<<"please enter your name "<<endl;
cin>>name;
element.setName(name);
cout<<element.getName()<<endl;
cout<<"please enter your department "<<endl;
cin>>department;
element.setDepartment(department);
cout<<element.getDepartment()<<endl;
cout<<"please enter your position"<<endl;
cin>>position;
element.setPosition(position);
cout<<element.getPosition()<<endl;
cout<<"please enter your IDNumber"<<endl;
cin>>IDNumber;
element.setIDNumber(IDNumber);
cout<<element.getIDnumber()<<endl;
}
for(Employee element:Info)
{
cout<<element.getName()<<setw(8)<<element.getDepartment()
cout<<setw(8)<<element.getPosition()<<setw(8)<<element.getIDnumber()<<endl;
}
}
for(Employee element:Info)
element is a copy of an element from Info array. You are busy filling this copy, then it gets discarded. Contents of Info remain unchanged, with empty strings all around.
Make it
for(Employee& element:Info)
Note the ampersand.