C++实现一个简单图书借阅流程


总共实现了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;
}

 


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,或者增加继承的结构

本文永久更新链接地址:http://www.bkjia.com/Linux/2015-07/120010tm

相关内容