总共实现了myDate类,book类,student类,图书借阅记录record类
//
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <ctime>
#include <cstdio>
using namespace std;
//主要就基于C库封装了一个获取时间戳的数据成员和相关方法
class myDate
{
time_t _time;
public:
myDate()
{
time (&this->_time);
cout<<"get time sucessful"<<endl;
}
friend ostream& operator<<(ostream &out,myDate &d);
};
ostream& operator <<(ostream &out,myDate &d)
{
out<<ctime(&(d._time));
return out;
}
//book类主要封装一个图书信息(书名和数量)
class book
{
//string bookid;
string bookname;
int bookcount;
public:
explicit book(string bname,int bcount):
bookname(bname),bookcount(bcount)
{
}
int insertBook()
{
if(bookcount < 0)
return -1;
bookcount += 1;
return 0;
}
int removeBook()
{
if(bookcount <= 0)
return -1;
bookcount -= 1;
return 0;
}
string& getname()
{
return bookname;
}
void print()
{
cout<<"bname:"<<bookname<<",count:"<<bookcount<<endl;
}
};
//record类封装一条借阅记录<who,do,bookname>
class recordPer
{
bool f ;//true brrow -- false return
string sname;
string bname;
myDate time;
public:
recordPer(string &bn,string &sn,bool &b):
bname(bn),sname(sn),time(),f(b)
{
}
friend ostream& operator <<(ostream &out,recordPer &r);
};
ostream& operator <<(ostream &out,recordPer &r)
{
if(r.f == true)
out<<"at "<<r.time<<" "<<r.sname<<"--brrow->"<<r.bname;
else
out<<"at "<<r.time<<" "<<r.sname<<"--return->"<<r.bname;
return out;
}
//其实这个类封装的显得多余,第二版本后面再优化
class bookRecord
{
public:
vector<recordPer > _record;
void print()
{
for(unsigned int i=0;i< _record.size();++i)
{
cout<<_record.at(i)<<endl;
}
}
int insert(recordPer r)
{
_record.push_back(r);
return 0;
}
};
//下面就是整个流程的灵魂,人,借书还书都是人的动作
class student
{
//string stuid;
string stuname;
bookRecord __record;
list<string> needreturn;
public:
student(string &s):stuname(s){}
int borrowBook(string &bookname,vector<book> &v);
int returnBook(string &bookname,vector<book> &v);
string& getname()
{
return stuname;
}
void printAll()
{
this->__record.print();
}
};
int student::borrowBook(string &bookname,vector<book> &v)
{
bool b = true;
for(int i=0;i<v.size();++i)
{
if(v.at(i).getname() == bookname)
{
if(v.at(i).removeBook() != 0)
return -1;
this->__record.insert( recordPer(bookname,this->stuname,b));
this->needreturn.push_back(bookname);
return 0;
}
}
return 1;
}
int student::returnBook(string &bookname,vector<book> &v)
{
bool b = false;
for(int i=0;i<v.size();++i)
{
if(v.at(i).getname() == bookname)
{
this->needreturn.remove(bookname);
if(v.at(i).insertBook() != 0)
return -1;
this->__record.insert( recordPer(bookname,this->stuname,b));
//this->needreturn.remove(bookname);
return 0;
}
}
return 1;
}