C++实现一个简单图书借阅流程
2015-07-07 17:18
731 查看
总共实现了myDate类,book类,student类,图书借阅记录record类
不合理的地方在后期需要改进的地方:
取消掉record类,需要加载上bookid和studentid,或者增加继承的结构
// #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; } int main() { vector<book> _book; vector<student> _stu; string name; string id; int count; int code; while(1) { cout<<"----------BOOK-------------"<<endl; cout<<"1 new book"<<endl; cout<<"2 new user"<<endl; cout<<"3 borrow book"<<endl; cout<<"4 return book"<<endl; cout<<"5 find all record of some student"<<endl; cout<<"6 find all book"<<endl; cout<<"----------BOOK-------------"<<endl; 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)"<<endl; 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 "<<name<<"not found\n"; if(0 == _stu.at(i).borrowBook(id,_book)) cout<<"brrowbook sucessful \n"; else cout<<"brrowbook failed \n"; } else if(code == 4)//return { 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 "<<name<<"not found\n"; if(0 == _stu.at(i).returnBook(id,_book)) cout<<"returnbook sucessful \n"; else cout<<"returnbook failed \n"; } else if(code == 5) { cout<<"input student name:"; int flag = 0; cin>>name; int i; for( i=0;i<_stu.size();++i) { if(_stu[i].getname() == name) { _stu.at(i).printAll(); flag = 1; break; } } if(flag == 0) cout<<"student "<<name<<"not found"<<endl; } else if(code == 6) { for(int i=0;i<_book.size();++i) { _book.at(i).print(); } } } return 0; }
不合理的地方在后期需要改进的地方:
取消掉record类,需要加载上bookid和studentid,或者增加继承的结构
相关文章推荐
- 主页木马的制作方法
- 解密网页加密的两个方法
- 定义标题的最好方法
- Ruby简明教程之方法(Method)介绍
- Ruby基础知识之方法、代码段
- C#获取文件夹及文件的大小与占用空间的方法
- Delphi控件ListView的属性及使用方法详解
- VB6实现连接Access数据库的ADODB代码实现方法
- MySQL重定位数据目录的方法
- PowerShell中Get-Date对象的常用属性和方法介绍
- 在Linux上运行C#的方法
- C#实现绘制面形图表的方法详解
- ruby 流程控制 方法
- CSS清除浮动常用方法小结
- C#实现把dgv里的数据完整的复制到一张内存表的方法
- Oracle中插入特殊字符:&和'的解决方法汇总
- C#获取字符串后几位数的方法
- javascript下拉框不被选中大类的区别方法
- MySQL数据库安全之防止撰改的方法
- VC实现批量删除指定文件的方法