如何将用户与图书(图书馆系统)C++联系起来



我正在做一个大学项目,我正在制作一个图书馆系统。它有一些限制,我不能使用我们没有学到的东西(所以没有矢量或std::东西(。现在,我上了一些课。

(1( 日期(2( Book(3( 学生(4( 图书馆//还没有实现

现在,当学生/用户发行一本书时,他们的账号会保存在"book"类中,return_date也是如此。现在,我想制作一个指针,或者使用this->运算符,或者在Student类中指向Book类的东西。因此,当学生想要查看他们当前发行的书籍时,将显示这些书籍。我不知道该怎么做。我应该创建一个Library类,通过组合给它一个"Book"类的数组,然后在"Student/User"类中创建一个成员函数并在那里显示它吗?

我真的想用new/delete关键字、this或指针来做这件事。这不是强制性的,但它将帮助我理解这些概念。

以下是我的图书、学生和约会课程。

日期类别:

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;
}

图书类别

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;

学生班

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++)
{
}
}
};

很抱歉发布了大量代码。但有些人说我们应该发布整个代码,所以这就是为什么我以有组织的方式发布了所有代码。

主要问题是:

我应该如何在学生的课堂上展示已发行的书籍?我应该先创建一个Library类(作为父类或子类,或者使用Book类的组合和数组(还是在最后?让我知道最好的方法是什么。

非常感谢。

  1. 最好从图书类中删除用户问题的详细信息。。。让book类只具有关于book的详细信息以及更新和检索book详细信息的方法。

  2. 将用户问题详细信息添加到继承图书类的新类中。。。这种方式更加模块化,并将书籍作为单独的实体。现在,你可以制作一个链接列表(因为它们是动态的(或这些类的数组,每个类都有一本书,以及特定学生的发行日期和返校日期。

例如:

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:>

};

现在在学生类中声明books_issued类的对象,并编写一个方法来添加、删除和查看该特定学生发行的书籍。

---如果你需要任何帮助来创建链接列表,请告诉我,我会非常乐意提供帮助,因为我也为我的项目做了一个非常相似的库管理程序:-(---

最新更新