设为首页 加入收藏

TOP

C++实现一个简单图书借阅流程(一)
2015-07-20 12:52:40 来源: 作者: 【 】 浏览:31
Tags:实现 一个 简单 图书 借阅 流程

总共实现了myDate类,book类,student类,图书借阅记录record类


//
#include
#include
#include
#include


#include
#include
using namespace std;


//主要就基于C库封装了一个获取时间戳的数据成员和相关方法
class myDate
{
? ? ? ? time_t _time;
public:
? ? ? ? myDate()
? ? ? ? {
? ? ? ? ? ? ? ? time (&this->_time);
? ? ? ? ? ? ? ? cout<<"get time sucessful"<? ? ? ? }
? ? ? ? friend ostream& operator<<(ostream &out,myDate &d);
};
ostream& operator <<(ostream &out,myDate &d)
{
? ? ? ? out<? ? ? ? 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:"<? ? ? ? }
};


//record类封装一条借阅记录
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 "<"<? ? else
? ? ? ? out<<"at "<"<? ? return out;
}


//其实这个类封装的显得多余,第二版本后面再优化
class bookRecord
{
public:


? ? ? ? vector _record;
? ? ? ? void print()
? ? ? ? {
? ? ? ? ? ? ? ? for(unsigned int i=0;i< _record.size();++i)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? cout<<_record.at(i)<? ? ? ? ? ? ? ? }
? ? ? ? }
? ? ? ? int insert(recordPer r)
? ? ? ? {
? ? ? ? ? ? _record.push_back(r);
? ? ? ? ? ? return 0;
? ? ? ? }
};


//下面就是整个流程的灵魂,人,借书还书都是人的动作
class student
{
? ? ? ? //string stuid;
? ? ? ? string stuname;
? ? ? ? bookRecord __record;
? ? ? ? list needreturn;
public:
? ? ? ? student(string &s):stuname(s){}
? ? ? ? int borrowBook(string &bookname,vector &v);
? ? ? ? int returnBook(string &bookname,vector &v);
? ? ? ? string& getname()
? ? ? ? {
? ? ? ? ? ? return stuname;
? ? ? ? }
? ? ? ? void printAll()
? ? ? ? {
? ? ? ? ? ? this->__record.print();
? ? ? ? }
};
int student::borrowBook(string &bookname,vector &v)
{
? ? bool b = true;
? ? for(int i=0;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 &v)
{
? ? bool b = false;
? ? for(int i=0;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;
}


?



int main()
{
? ? vector _book;
? ? vector _stu;
? ? string name;
? ? string id;
? ? int count;



? ? int code;
? ? while(1)
? ? {
? ? ? ? cout<<"----------BOOK-------------"<? ? ? ? cout<<"1 new book"<? ? ? ? cout<<"2 new user"<? ? ? ? cout<<"3 borrow book"<? ? ? ? cout<<"4 return book"<? ? ? ? cout<<"5 find all record of some student"<? ? ? ? cout<<"6 find all book"<? ? ? ? cout<<"----------BOOK-------------"<

? ? ? ? cout<<"input op:";
? ? ? ? cin>>code;
? ? ? ? //code = getchar();
? ? ? ? //code -= 48;
? ? ? ? if(code <1 || code >6)
? ? ? ? {
? ? ? ? ? ? cout<<"input error\n";
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? if(code == 1)
? ? ? ? {
? ? ? ? ? ? cout<<"input book infomation:name(string) count(int)"<? ? ? ? ? ? cin>>name>>count;
? ? ? ? ? ? _book.push_back(book(name,count));
? ? ? ? }
? ? ? ? else if(code == 2)
? ? ? ? {
? ? ? ? ? ? cout<<"input student information:name(string)\n";
? ? ? ? ? ? cin>>name;
? ? ? ? ? ? _stu.push_back(student(name));
? ? ? ? }
? ? ? ? else if(code == 3)//brrow
? ? ? ? {
? ? ? ? ? ? cout<<"input student name && book name :";
? ? ? ? ? ? int flag = 0;
? ? ? ? ? ? cin>>name>>id;
? ? ? ? ? ? int i;
? ? ? ? ? ? for( i=0;i<_stu.size();++i)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(_stu[i].getname() == name)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? flag = 1;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? if(flag != 1)
? ? ? ? ? ? ? ? cout<<"student "<? ? ? ? ? ? if

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言程序监控变量的变化 下一篇阿里巴巴fastjson的使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: