1.思路
OOP实现一个简单的图书馆书籍管理
角色:
学生 : 查询书籍 查看自己已经借阅的书籍
管理员 : 查询书籍 借书 还书 借阅历史
实体: 人 书 借阅信息
//===================人员信息定义=======================
//权限信息定义
const int AUTH_STUDENT = 0; // 学生权限
const int AUTH_MANAGER = 1; // 管理员权限
const int AUTH_INVLAID = 2; // 无效权限
const int NAME_LEN = 20;
const int SEX_LEN = 5;
const int PWD_LEN = 20;
// 用户类
class CUser
{
public:
CUser(){}
CUser(int id, char *n, int a, char *s, int auth, char *pwd)
:mID(id), mAge(a), mAuth(auth)
{
strcpy(mName, n);
strcpy(mSex, s);
strcpy(mPassword, pwd);
}
// setxxx getxxx
char* getName(){ return mName; }
char* getPwd(){ return mPassword; }
int getAuth(){ return mAuth; }
private:
int mID;
char mName[NAME_LEN];
int mAge;
char mSex[SEX_LEN];
int mAuth;
char mPassword[PWD_LEN];
};
// 用户数据库类
class CUserDB
{
public:
CUserDB(int size=10)
{
mpUserArr = new CUser[size];
mSize = size;
mIndex = 0;
}
~CUserDB()
{
delete []mpUserArr;
mpUserArr = NULL;
}
// 添加用户信息
void addUser(const CUser &user)
{
if (mIndex == mSize)
{
CUser *ptmp = new CUser[2 * mSize];
for (int i = 0; i < mSize; ++i)
{
ptmp[i] = mpUserArr[i];
}
delete []mpUserArr;
mpUserArr = ptmp;
mSize *= 2;
}
mpUserArr[mIndex++] = user;
}
// 检查用户登录权限
int checkUser(char *name, char *pwd)
{
for (int i = 0; i < mIndex; ++i)
{
if (strcmp(mpUserArr[i].getName(), name) == 0
&& strcmp(mpUserArr[i].getPwd(), pwd) == 0)
{
return mpUserArr[i].getAuth();
}
}
return AUTH_INVLAID;
}
private:
CUser *mpUserArr;
int mSize;
int mIndex;
};
//===================书籍信息定义=======================
const int BOOK_NAME_LEN = 50;
const int BOOK_NUM = 100;
// 书籍类型定义
class CBook
{
public:
CBook(){}
CBook(int isbn, char *n, int a)
:mISBN(isbn), mAmount(a)
{
strcpy(mName, n);
}
int getISBN(){ return mISBN; }
char* getName(){ return mName; }
int getAmount(){ return mAmount; }
private:
int mISBN;
char mName[BOOK_NAME_LEN];
int mAmount;
};
// 书籍的DB定义
class CBookDB
{
public:
CBookDB():mIndex(0){}
// 添加书籍
void addBook(const CBook &book)
{
mBookArr[mIndex++] = book;
}
// 删除书籍
void delBook(int isbn)
{
for (int i = 0; i < mIndex; ++i)
{
if (isbn == mBookArr[i].getISBN())
{
// 数组第i项删除
}
}
}
// 更新书籍册数
void updateBookNumber(int isbn, int amount);
// 按传入的书名进行模糊查询 c++
// 接口的API文档里面:queryBook里面会动态开辟内存,保存符合查询条件的书籍,用完以后,一定自行释放内存
CBook* queryBook(char *name, int &count)
{
//CBook books[BOOK_NUM]; 不能返回局部变量的地址
CBook *books = new CBook[BOOK_NUM];
for (int i = 0; i < mIndex; ++i)
{
// strcpy strcmp strcat strstr
if (NULL != strstr(mBookArr[i].getName(), name))
{
books[count++] = mBookArr[i];
}
}
return books;
}
private:
//CLink mBookLink; // int CBook
CBook mBookArr[BOOK_NUM];
int mIndex;
};
//====================借阅信息定义=====================
class CDate
{
public:
CDate(int y=0, int m=0, int d=0)
{
mYear = y;
mMonth = m;
mDay = d;
}
void show()
{
cout << mYear << "/" << mMonth << "/" << mDay << endl;
}
private:
int mYear;
int mMonth;
int mDay;
};
const int LOAN_INFO_SIZE = 100;
class CLoan
{
public:
CLoan(){}
CLoan(CUser user, CBook book, char* bt, char* bkt, char *manager, char *op)
:mUser(user), mBook(book)
{
strcpy(mborrowTime, bt);
strcpy(mbackTime, bkt);
strcpy(mManager, manager);
strcpy(mOperator, op);
}
private:
CUser mUser;
CBook mBook;
char mborrowTime[NAME_LEN];
char mbackTime[NAME_LEN];
char mManager[NAME_LEN];
char mOperator[NAME_LEN];
};
class CLoanDB
{
public:
// 添加一条借阅信息
void addLoan(){}
void queryStudentLoanInfo(char *username){}
private:
CLoan mLoan[LOAN_INFO_SIZE];
int mIndex;
};
//===================界面类======================
class CMenu
{
public:
CMenu()
{
// 预填写登录用户数据
mUserDB.addUser(CUser(1000, "zhang san", 20, "男", AUTH_STUDENT, "111"));
mUserDB.addUser(CUser(1010, "gao yang", 22, "女", AUTH_STUDENT, "222"));
mUserDB.addUser(CUser(1020, "liu shuo", 18, "女", AUTH_STUDENT, "333"));
mUserDB.addUser(CUser(1030, "liu wei", 19, "男", AUTH_MANAGER, "444"));
// 预填写书籍数据
mBookDB.addBook(CBook(188100, "30天精通C++", 20));
mBookDB.addBook(CBook(167890, "C++ primer", 20));
mBookDB.addBook(CBook(165671, "30天精通Java", 40));
mBookDB.addBook(CBook(121421, "Java并发编程", 40));
mBookDB.addBook(CBook(155334, "30天精通Python", 30));
mBookDB.addBook(CBook(288350, "30天精通PHP", 10));
}
void loginMenu()
{
int choice;
for (;;)
{
cout << "用户名:";
gets(mLoginUser); // scanf zhang san
cout << "密码:";
char pwd[20];
gets(pwd);
// 检测用户登录是否成功
int auth = mUserDB.checkUser(mLoginUser, pwd);
if (auth != AUTH_INVLAID)
{
// 进入相应的主菜单
if (auth == AUTH_STUDENT) // 进入学生主菜单
{
studentMenu();
}
else // 进入管理员主菜单
{
managerMenu();
/*
1. 查询书籍
2. 借书 学生名 书籍名 借阅时间 还书时间 CLoan => CLoanDB CBookDB
3. 还书
4. 查询借阅历史 CLoanDB => CLoan
*/
}
}
else
{
cout << "用户名或密码错误!重新输入..." << endl;
}
}
}
//管理员菜单
void managerMenu()
{
for (;;)
{
cout << "======管理员操作菜单======" << endl;
cout << "1.查询书籍" << endl;
cout << "2.借书" << endl;
cout << "3.还书" << endl; // 输入学生id 输入isbn号 还书操作
cout << "4.借阅历史" << endl;
cout << "========================" << endl;
cout << "选择:";
int choice = 0;
cin >> choice;
switch (choice)
{
case 1:
{
char bookName[NAME_LEN] = { 0 };
cout << "请输入书名:";
cin >> bookName;
cout << "=======================关键字:" << bookName << " 的查询结果如下:=======================" << endl;
printf("%-15s%-30s%-20s\n", "ISBN号码", "书籍名称", "书记册数");
int count = 0;
CBook *pBooks = mBookDB.queryBook(bookName, count);
for (int i = 0; i < count; ++i)
{
printf("%-15d%-30s%-20d\n", pBooks[i].getISBN()
, pBooks[i].getName(), pBooks[i].getAmount());
}
delete[]pBooks;
}
break;
case 2:
{
cout << "请输入学生学号:";
int studentid;
cin >> studentid;
cout << "借阅书籍ISBN号:";
int isbn;
cin >> isbn;
cout << "借阅日期:";
char borrowTime[20];
cin >> borrowTime;
cout << "还书日期:";
char backTime[20];
cin >> backTime;
mLoanDB.addLoan(mUserDB.getUserById(studentid)
,mBookDB.queryBookByISBN(isbn)
, borrowTime, backTime, mLoginUser, "借书");
cout << "添加成功!" << endl;
}
break;
}
}
}
//学生操作主菜单
void studentMenu()
{
cout << "======学生操作菜单======" << endl;
cout << "1.查询书籍" << endl;
cout << "2.已借阅书籍" << endl;
cout << "======================" << endl;
cout << "选择:";
int choice = 0;
cin >> choice;
switch (choice)
{
case 1:
{
cout << "请输入书籍的名称:";
char name[20];
gets(name);
int count = 0;
CBook *book = mBookDB.queryBook(name, count);
if (book == NULL)
{
cout << name << " 该书籍不存在!" << endl;
}
else
{
// 查到以后,打印书籍的信息
}
delete[]book;
}
break;
case 2:
// 查询借阅信息,有很多,一次进行打印
mLoanDB.queryStudentLoanInfo(mLoginUser);
break;
default:
cout << "输入无效,请重新选择..." << endl;
}
}
private:
CUserDB mUserDB; // 人员信息数据库
CBookDB mBookDB; // 书籍信息数据库
CLoanDB mLoanDB; // 借阅信息数据库
char mLoginUser[NAME_LEN]; // 记录当前登录的用户名
}; //464-244=220 300
/*
界面层 CMenu
数据层 CBookDB
*/
int main()
{
CMenu menu;
menu.loginMenu();
return 0;
}