图书管理系统心得
首先,还是先贴代码
#include<bits/stdc++.h>
using namespace std;
class Time
{
private:
int year;
int month;
int day;
public:
Time(){year=2018;month=1;day=1;}
Time(int x,int y,int z):year(x),month(y),day(z){};
int getYear(){return year;}
int getMonth(){return month;}
int getDay(){return day;}
void setYear(int year){this->year=year;}
void setMonth(int month){this->month=month;}
void setDay(int day){this->day=day;}
friend istream& operator>>(istream &is,Time &t);
friend ostream& operator<<(ostream &os,const Time &t);
bool operator<(const Time &t1)const;
friend Time operator+(Time m,int i);
};
Time operator+(Time m,int i)
{
m.month=m.month+i;
if(m.month>12)
{
m.month=m.month-12;
m.year++;
}
return m;
}
istream& operator>>(istream &is,Time &t)
{
is>>t.year;
while(t.year<=999||t.year>=10000)
{
cout<<"Date year is not legal,please retype"<<endl;
is>>t.year;
}
is>>t.month;
while(t.month<=0||t.month>=13)
{
cout<<"Date month is not legal,please retype"<<endl;
is>>t.month;
}
is>>t.day;
while(t.day<=0||t.day>=32)
{
cout<<"Date day is not legal,please retype"<<endl;
is>>t.day;
}
return is;
}
ostream& operator<<(ostream &os,const Time &t)
{
os<<t.year<<" "<<t.month<<" "<<t.day<<" ";
return os;
}
bool Time::operator<(const Time &t1)const
{
return year!=t1.year?year<t1.year:month!=t1.month?month<t1.month:day<t1.day;
}
///
class Record
{
private:
string book_no;
string stu_no;
int num;
int left;
Time t,back_t;
string type; //借书还书
bool type1; //0学生1书
bool xu; //0没续借 1续借
public:
Record(){};
Record(string bk_no,string s_n,int nu,int le,Time t1,Time b_t,string ty,bool ty1,bool x){book_no=bk_no;stu_no=s_n;num=nu;left=le;t=t1;back_t=b_t;type=ty;type1=ty1;xu=x;}
string getBook_no(){return book_no;}
string getStu_no(){return stu_no;}
string getType(){return type;}
int getNum(){return num;}
int getLeft(){return left;}
Time getTime(){return t;}
Time getB_Time(){return back_t;}
void setB_Time(Time b_t){back_t=b_t;}
void setBook_no(string no){book_no=no;}
void setStu_no(string no){stu_no=no;}
void setType(string type){this->type=type;}
void setNum(int nu){num=nu;}
void setLeft(int le){left=le;}
void setTime(Time t){this->t=t;}
void display();
friend ostream& operator<<(ostream &os,Record &r);
friend istream& operator>>(istream &r_is,Record &r);
};
ostream& operator<<(ostream &os,Record &r)
{
os<<r.book_no<<" ";
os<<r.stu_no<<" ";
os<<r.t<<" ";
os<<r.back_t<<" ";
os<<r.num<<" ";
os<<r.left<<" ";
os<<r.type<<" ";
os<<r.type1<<" ";
os<<r.xu<<endl;
return os;
}
void Record::display()
{
cout<<"书号:"<<book_no<<endl;
if(type1==0)
{
cout<<"学号:"<<stu_no<<endl;
if(type=="借书")
cout<<"借阅日期:"<<t<<endl;
if(type=="还书")
cout<<"归还日期:"<<t<<endl;
}
if(type1==1)
{
cout<<"被"<<stu_no<<"借走"<<endl;
cout<<"应在"<<back_t<<"归还"<<endl;
if(xu==0)
{
cout<<"没有续借";
}
else
{
cout<<"续借1次";
}
}
}
istream& operator>>(istream &r_is,Record &r)
{
r_is>>r.book_no;
r_is>>r.stu_no;
r_is>>r.t;
r_is>>r.back_t;
r_is>>r.num;
r_is>>r.left;
r_is>>r.type;
r_is>>r.type1;
r_is>>r.xu;
return r_is;
}
//
class Book
{
private:
string book_no;
string name;
string author;
string chubanshe;
int num;
int left;
vector<Record> record;
vector<Record>::iterator it;
int record_num;
public:
Book():record_num(0){};
Book(string bk_no,string n,string aut,string chubanshe,int num,int left):record_num(0){book_no=bk_no;name=n;author=aut;this->chubanshe=chubanshe;this->num=num;this->left=left;}
string getBook_no(){return book_no;}
string getName(){return name;}
string getAuthor(){return author;}
string getChubanshe(){return chubanshe;}
int getNum(){return num;}
int getLeft(){return left;}
int getRecord_num(){return record_num;}
void setBook_no(string bk_no){book_no=bk_no;}
void setName(string n){name=n;}
void setAuthor(string aut){author=aut;}
void setChubanshe(string chubanshe){this->chubanshe=chubanshe;}
void setNum(int n){num=n;}
void setLeft(int l){left=l;}
void getBack_t(string b_n);
void addRecord(Record r1);
void displayBook();
friend ostream& operator<<(ostream &os,Book &b);
friend istream& operator>>(istream &is,Book &b);
};
void Book::displayBook()
{
cout<<"书号:"<<book_no<<endl;
cout<<"书名:"<<name<<endl;
cout<<"剩余:"<<left<<endl;
}
void Book::addRecord(Record r1)
{
record.push_back(r1);
record_num++;
if(r1.getType()=="借书")
{
left--;
}
if(r1.getType()=="还书")
{
left++;
}
}
ostream& operator<<(ostream &os,Book &b)
{
os<<b.book_no<<" ";
os<<b.name<<" ";
os<<b.author<<" ";
os<<b.chubanshe<<" ";
os<<b.num<<" ";
os<<b.left<<" ";
os<<b.record_num<<endl;
for(int i=0;i<b.record_num;i++)
os<<b.record[i];
return os;
}
istream& operator>>(istream &b_is,Book &b)
{
int i;
b.record.clear();
b_is>>b.book_no;
if(b.book_no=="end")return b_is;
b_is>>b.name>>b.author>>b.chubanshe>>b.num>>b.left>>i;
b.record_num=i;
for(int j=0;j<i;j++)
{
Record re;
b_is>>re;
b.record.push_back(re);
}
return b_is;
}
class User
{
private:
string stu_no;
string name;
int book_max;
int book_now;
int record_num;
int GG; //违纪不可再借书
vector<Record> r;
vector<Record>::iterator it;
public:
User():record_num(0),GG(0){};
User(string no):record_num(0),GG(0){stu_no=no;};
User(string no,string name):book_max(10),book_now(0),record_num(0),GG(0){stu_no=no;this->name=name;}
User(const User&u);
string getStu_no(){return stu_no;}
string getName(){return name;}
int getBook_max(){return book_max;}
int getBook_now(){return book_now;}
int getGG(){return GG;}
int getRecord_num(){return record_num;}
void setStu_no(string s_no){stu_no=s_no;}
void setName(string n){name=n;}
void setBook_max(int b_m){book_max=b_m;}
void setBook_now(int b_n){book_now=b_n;}
void setGG(int g){GG=g;}
vector<Record>& getRecord(){return r;}
void addRecord(Record r1);
friend ostream& operator<<(ostream &os,User &u);
friend istream& operator>>(istream &is,User &u);
friend bool operator==(User u1,User u2);
};
User::User(const User&u)
{
stu_no=u.stu_no;
name=u.name;
book_now=u.book_now;
book_max=u.book_max;
record_num=u.record_num;
GG=u.GG;
r=u.r;
}
bool operator==(User u1,User u2)
{
}
ostream& operator<<(ostream &os,User &u)
{
os<<u.stu_no<<" ";
os<<u.name<<" ";
os<<u.book_max<<" ";
os<<u.book_now<<" ";
os<<u.GG<<" ";
os<<u.record_num<<endl;
vector<Record>::iterator iter;
for(iter=u.r.begin();iter!=u.r.end();iter++)
os<<*iter;
return os;
}
istream& operator>>(istream &is,User &u)
{
int i;
Record re;
u.r.clear();
is>>u.stu_no;
if(u.stu_no=="end")return is;
is>>u.name>>u.book_max>>u.book_now>>u.GG>>i;
u.record_num=i;
for(int j=0;j<i;j++)
{
is>>re;
u.r.push_back(re);
}
return is;
}
void User::addRecord(Record r1)
{
r.push_back(r1);
record_num++;
if(r1.getType()=="借书")
{
book_now++;
}
if(r1.getType()=="还书")
{
book_now--;
}
}
class Manager
{
private:
vector<Book> bk;
multimap<string,int> m1;
vector<User> ur;
multimap<string,int> m2;
Time t;
public:
Manager();
~Manager();
void b_displayByno(string n);
void b_delByno(string n);
void s_delByno(string n);
void s_displayByno(string n);
void addBook(Book b);
void addBook();
void addStu();
void addStu(User u);
void displayBook();
};
void Manager::displayBook()
{
vector<Book>::iterator iter;
for(iter=bk.begin();iter!=bk.end();iter++)
{
iter->displayBook();
}
}
void Manager::s_displayByno(string n)
{
multimap<string,int>::iterator it;
it=m2.find(n);
if(it!=m2.end())
cout<<ur[it->second];
else
cout<<"没找到"<<endl;
// vector<User>::iterator iter;
// bool a=1;
// for(iter=ur.begin();iter!=ur.end();iter++)
// {
// if(iter->getStu_no()==n)
// {cout<<*iter;a=0;}
// }
// if(a)
// cout<<"无此用户"<<endl;
}
void Manager::s_delByno(string n)
{
multimap<string,int>::iterator it;
it=m2.find(n);
if(it!=m2.end())
{
ur.erase(ur.begin()+it->second);
cout<<"删除成功"<<endl;
}
else
cout<<"没找到"<<endl;
// vector<User>::iterator iter;
// bool a=1;
// for(iter=ur.begin();iter!=ur.end();)
// {
// if(iter->getStu_no()==n)
// {iter=ur.erase(iter);cout<<"用户已删除"<<endl;a=0;}
// else
// {
// iter++;
// }
// }
// if(a)
// cout<<"未找到该用户"<<endl;
}
void Manager::addStu(User u)
{
ur.push_back(u);
int i=ur.size();
m2.insert(make_pair(u.getStu_no(),i-1));
cout<<"用户已添加"<<endl;
}
void Manager::addStu()
{
while(1)
{
User u;
cout<<"现在图书馆有"<<ur.size()<<"名用户 "<<endl;
cout<<"请输入新用户的学号/名字/最多借书:(学号输入-1结束)"<<endl;
string temp;int i;
cin>>temp;
if(temp=="-1")break;
u.setStu_no(temp);
cin>>temp;
u.setName(temp);
cin>>i;
u.setBook_max(i);
u.setBook_now(0);
ur.push_back(u);
i=ur.size();
m2.insert(make_pair(u.getStu_no(),i-1));
cout<<"用户已添加"<<endl;
}
}
void Manager::b_displayByno(string n) //调用display
{
multimap<string,int>::iterator it;
it=m1.find(n);
if(it!=m1.end())
bk[it->second].displayBook();
else
cout<<"没找到"<<endl;
// vector<Book>::iterator iter;
// bool a=1;
// for(iter=bk.begin();iter!=bk.end();iter++)
// {
// if(iter->getBook_no()==n)
// {cout<<*iter;a=0;}
// }
// if(a)
// cout<<"无此图书"<<endl;
}
void Manager::addBook()
{
while(1)
{
Book b;
cout<<"现在图书馆有"<<bk.size()<<"本书"<<endl;
cout<<"请输入新书的序号/名字/作者/出版社/总数:(书号输入-1结束)"<<endl;
string temp;int i;
cin>>temp;
if(temp=="-1")break;
b.setBook_no(temp);
cin>>temp;
b.setName(temp);
cin>>temp;
b.setAuthor(temp);
cin>>temp;
b.setChubanshe(temp);
cin>>i;
b.setNum(i);
b.setLeft(i);
bk.push_back(b);
i=bk.size();
m1.insert(make_pair(b.getBook_no(),i-1));
cout<<"图书已添加"<<endl;
}
}
void Manager::addBook(Book b)
{
bk.push_back(b);
int i;
i=bk.size();
m1.insert(make_pair(b.getBook_no(),i-1));
cout<<"图书已添加"<<endl;
}
void Manager::b_delByno(string n)
{
multimap<string,int>::iterator it;
it=m1.find(n);
if(it!=m1.end())
{
bk.erase(bk.begin()+it->second);
cout<<"删除成功"<<endl;
}
else
cout<<"没找到"<<endl;
// vector<Book>::iterator iter;
// bool a=1;
// for(iter=bk.begin();iter!=bk.end();)
// {
// if(iter->getBook_no()==n)
// {iter=bk.erase(iter);cout<<"图书已删除"<<endl;a=0;}
// else
// {
// iter++;
// }
// }
// if(a)
// cout<<"未找到该图书"<<endl;
}
Manager::~Manager()
{
ofstream s_outfile("20171752Stu.txt",ios::out),b_outfile("20171752Book.txt",ios::out);
if(!s_outfile)
return;
if(!b_outfile)
return;
vector<Book>::iterator b_it;
vector<User>::iterator s_it;
for(b_it=bk.begin();b_it!=bk.end();b_it++)
{
b_outfile<<*b_it;
}
b_outfile<<"end";
for(s_it=ur.begin();s_it!=ur.end();s_it++)
{
s_outfile<<*s_it;
}
s_outfile<<"end";
bk.clear();
ur.clear();
s_outfile.close();
b_outfile.close();
}
Manager::Manager()
{
User u;
Book b;
int i,j;
ifstream s_infile("20171752Stu.txt",ios::in),b_infile("20171752Book.txt",ios::in);
if(!s_infile)
return;
if(!b_infile)
return;
bk.clear();
ur.clear();
i=0;j=0;
while(s_infile>>u&&u.getStu_no()!="end")
{
ur.push_back(u);
m2.insert(make_pair(u.getStu_no(),i));
i++;
}
while(b_infile>>b&&b.getBook_no()!="end")
{
bk.push_back(b);
m1.insert(make_pair(b.getBook_no(),j));
j++;
}
s_infile.close();
b_infile.close();
}
class Userop
{
private:
vector<Book> bk;
//vector<User> ur;
User u;//当前用户
int stu_no;
Time t;
public:
Userop();
Userop(User u);
~Userop();
void lend(string bk_no);
void lend();
void huan(string bk_no);
void huan();
void displayBook();
void search_Book(string n);
};
void Userop::search_Book(string n)
{
vector<Book>::iterator iter;
bool a=1;
for(iter=bk.begin();iter!=bk.end();iter++)
{
if(iter->getBook_no()==n)
{iter->displayBook();a=0;}
}
if(a)
cout<<"无此图书"<<endl;
}
void Userop::displayBook()
{
vector<Book>::iterator iter;
for(iter=bk.begin();iter!=bk.end();iter++)
{
iter->displayBook();
}
}
void Userop::huan()
{
string bk_no;
cout<<"请输入欲还书的书号:";
cin>>bk_no;
vector<Book>::iterator it;
vector<Record>::iterator r_it;
int num,left;bool flag=1,overdue=0;
{for(it=bk.begin();it!=bk.end();it++)
{
if(it->getBook_no()==bk_no)
{
Time t2;
r_it=u.getRecord().begin();
for(;r_it!=u.getRecord().end();r_it++)
{
if(bk_no==r_it->getBook_no())
{
t2=r_it->getB_Time();
if(t2<t)
{
overdue=1;u.setGG(1);
}
}
}
flag=0;
num=it->getNum();
left=it->getLeft()+1;
Record r1(bk_no,u.getStu_no(),num,left,t,t2,"还书",1,0),r2(bk_no,u.getStu_no(),num,left,t,t2,"还书",0,0);
it->addRecord(r1);
u.addRecord(r2);
if(overdue) //将对书的操作放到addrecord里了。。。为了方便。。
{cout<<"您已超期,请联系管理员"<<endl;}
else
cout<<"还书成功"<<endl;
}
}
}
if(flag)
{
cout<<"错误"<<endl;
}
}
//void Userop::huan(string bk_no)
//{
// vector<Book>::iterator it;
// vector<Record>::iterator r_it;
// int num,left;bool flag=1,overdue=0;
// {for(it=bk.begin();it!=bk.end();it++)
// {
// if(it->getBook_no()==bk_no)
// {
// Time t2;
// r_it=ur[stu_no].getRecord().begin();
// for(;r_it!=ur[stu_no].getRecord().end();r_it++)
// {
// if(bk_no==r_it->getBook_no())
// {
// t2=r_it->getB_Time();
// if(t2<t)
// {
// overdue=1;ur[stu_no].setGG(1);
// }
// }
// }
// flag=0;
// num=it->getNum();
// left=it->getLeft()+1;
// Record r1(bk_no,ur[stu_no].getStu_no(),num,left,t,t2,"还书",1,0),r2(bk_no,ur[stu_no].getStu_no(),num,left,t,t2,"还书",0,0);
// it->addRecord(r1);
// ur[stu_no].addRecord(r2);
// if(overdue) //将对书的操作放到addrecord里了。。。
// {cout<<"您已超期,请联系管理员"<<endl;}
// else
// cout<<"还书成功"<<endl;
// }
// }
// }
// if(flag)
// {
// cout<<"错误"<<endl;
// }
//}
void Userop::lend()
{
string bk_no;
cout<<"请输入欲借书的书号:";
cin>>bk_no;
vector<Book>::iterator it;
int num,left;bool flag=1;
for(it=bk.begin();it!=bk.end();it++)
{
if(it->getBook_no()==bk_no&&it->getLeft()>0&&u.getBook_now()<u.getBook_max())
{
if(u.getGG()==1)
{
cout<<"您已经有超期记录,请联系管理员."<<endl;
break;
}
else
{
flag=0;
num=it->getNum();
left=it->getLeft()-1;
Record r1(bk_no,u.getStu_no(),num,left,t,t+2,"借书",1,0),r2(bk_no,u.getStu_no(),num,left,t,t+2,"借书",0,0);
it->addRecord(r1);
u.addRecord(r2);
cout<<"借阅成功"<<endl;
}
}
}
if(flag)
{
cout<<"错误"<<endl;
}
}
//void Userop::lend(string bk_no)
//{
// vector<Book>::iterator it;
// int num,left;bool flag=1;
// for(it=bk.begin();it!=bk.end();it++)
// {
// if(it->getBook_no()==bk_no&&it->getLeft()>0&&ur[stu_no].getBook_now()<ur[stu_no].getBook_max())
// {
// if(ur[stu_no].getGG()==1)
// {
// cout<<"您已经有超期记录,请联系管理员."<<endl;
// break;
// }
// else
// {
// flag=0;
// num=it->getNum();
// left=it->getLeft()-1;
// Record r1(bk_no,ur[stu_no].getStu_no(),num,left,t,t+2,"借书",1,0),r2(bk_no,ur[stu_no].getStu_no(),num,left,t,t+2,"借书",0,0);
// it->addRecord(r1);
// ur[stu_no].addRecord(r2);
// cout<<"借阅成功"<<endl;
// }
// }
// }
// if(flag)
// {
// cout<<"错误"<<endl;
// }
//
//}
Userop::Userop(User u1)
{
User u;
Book b;
bool bo=1;
int j;
ifstream s_infile("20171752Stu.txt",ios::in),b_infile("20171752Book.txt",ios::in);
if(!s_infile)
return;
if(!b_infile)
return;
bk.clear();
j=0;
while(s_infile>>u&&u.getStu_no()!="end")
{
if(u1.getStu_no()==u.getStu_no())
{this->u=u;bo=0;}
}
while(b_infile>>b&&b.getBook_no()!="end")
{
bk.push_back(b);
j++;
}
s_infile.close();
b_infile.close();
Time t1;
{
cout<<"系统时间读入:(年/月/日 无斜杠)";
cin>>t1;
t=t1;
}
if(bo)
{cout<<"该用户不存在,退出系统."<<endl;}
}
//Userop::Userop()
//{
// User u;
// Book b;
// int i,j;
// ifstream s_infile("20171752Stu.txt",ios::in),b_infile("20171752Book.txt",ios::in);
// if(!s_infile)
// return;
// if(!b_infile)
// return;
// bk.clear();
ur.clear();
// i=0;j=0;
// while(s_infile>>u&&u.getStu_no()!="end")
// {
// ur.push_back(u);
// i++;
// }
// while(b_infile>>b&&b.getBook_no()!="end")
// {
// bk.push_back(b);
// j++;
// }
// s_infile.close();
// b_infile.close();
// string s_no;
// vector<User>::iterator it;
// int m=0;bool bo=1;
// cout<<"请输入您的学号"<<endl;
// cin>>s_no;
// for(it=ur.begin();it!=ur.end();it++,m++)
// {
// if(it->getStu_no()==s_no)
// {
// cout<<"登录成功"<<endl;
// stu_no=m;bo=0;
// }
// }
// Time t1;
// {
// cout<<"系统时间读入:(年/月/日 无斜杠)";
// cin>>t1;
// t=t1;
// }
// if(bo)
// {cout<<"该用户不存在,退出系统."<<endl;}
//}
Userop::~Userop()
{
ifstream s_infile("20171752Stu.txt",ios::in),b_infile("20171752Book.txt",ios::in);
vector<User> ur;
User u;
ur.clear();
while(s_infile>>u&&u.getStu_no()!="end")
{
if(this->u.getStu_no()==u.getStu_no())
{u=this->u;}
ur.push_back(u);
}
s_infile.close();
ofstream s_outfile("20171752Stu.txt",ios::out),b_outfile("20171752Book.txt",ios::out);
if(!s_outfile)
return;
if(!b_outfile)
return;
vector<Book>::iterator b_it;
vector<User>::iterator s_it;
for(b_it=bk.begin();b_it!=bk.end();b_it++)
{
b_outfile<<*b_it;
}
b_outfile<<"end";
for(s_it=ur.begin();s_it!=ur.end();s_it++)
{
s_outfile<<*s_it;
}
s_outfile<<"end";
bk.clear();
ur.clear();
s_outfile.close();
b_outfile.close();
}
/
int main()
{
User u("20171752");
// Manager m1;
// m1.addStu();
// m1.addBook();
Userop uop1(u);
// uop1.lend();
uop1.huan();
system("pause");
}
大体代码就长这个样子
首先先说不足,没用继承,没有登陆。
然后一些收获,首先写数据类的时候,先从基本的数据写起,不要一上来就加一些花里胡哨的数据,然后在大体写的差不多的时候,再把一些数据添上;其次,这次系统使用了文件操作,文件操作对于我们来说是一个刚接触的问题,使用上有一些问题,但在后期的使用中已经差不多了;最后体会到了操作类里用一个单独的用户操作的用意,为了代码的安全着想。
遇到的一些问题,在添加记录的时候遇到过记录向量清除不干净的现象,可能是由于创建对象时未对数据进行初始化。还有就是代码有的时候感觉比较乱,总是忘记函数或者数据成员叫什么名字。