图书管理系统
以下为源代码:
include
include
include
using namespace std;
class user; class common; class vip; class honored;
class book
{
protected:
string book_name;
string author;
string publisher;
double price;
book* next;
public:
book() :next(NULL) {};
void addbook(book); //添加书籍
void showbook(book); //展示书籍
void deletbook(book); //删除书籍
void loadbook(book);
void savebook(book);
friend user;
};
void book::addbook(book p)
{
book* p1 = p;
while (p->next != NULL)
{
p = p->next;
}
book* p2 = new book;
cout << "请输入书名:" << endl;
cin >> p2->book_name;
cout << "请输入作者:" << endl;
cin >> p2->author;
cout << "请输入出版社:" << endl;
cin >> p2->publisher;
cout << "请输入价格:" << endl;
cin >> p2->price;
if (cin.fail()) {
cin.clear();
cin.ignore(1000, '\n');
system("cls");
cout << "无效输入!" << endl;
return;
}
p->next = p2;
system("cls");
cout << "添加成功!" << endl;
}
void book::showbook(book* p)
{
if (p->next == NULL)
{
cout << "目前无书籍信息!\t\t" << endl;
return;
}
p = p->next;
int n = 1;
while (1)
{
cout << "NO." << n << ":" << '\t' << endl;
cout << "书名:" << "《" << p->book_name << "》" << '\t';
cout << "作者:" << p->author << '\t';
cout << "出版社:" << p->publisher << '\t';
cout << "价格:" << p->price << '\n';
if (p->next == NULL)break;
p = p->next;
n++;
}
}
void book::deletbook(book* p)
{
if (p->next == NULL)
{
cout << "目前无书籍信息!\t\t" << endl;
return;
}
string na; cout << "请输入要删除的书名:" << endl; cin >> na;
while (1)
{
if (p->next->book_name != na)
{
p = p->next;
if (p->next == NULL)
{
system("cls");
cout << "没有这本书的信息!" << endl << endl;
break;
}
}
else
{
cout << "找到了!" << endl;
cout << "—————————————————————————————————————————————————————————\n";
cout << "书名:" << "《" << p->next->book_name << "》" << '\t';
cout << "作者:" << p->next->author << '\t';
cout << "出版社:" << p->next->publisher << '\t';
cout << "价格:" << p->next->price << '\n';
cout << "—————————————————————————————————————————————————————————\n\n";
cout << "是否删除该书籍信息?" << endl;
cout << "1) 是\t0)否" << endl;
int a; cin >> a;
switch (a)
{
case 0:
{ break; }
case 1:
{
p->next = p->next->next;
system("cls");
cout << "删除成功!" << endl;
break;
}
default:
cout << "ERROR!" << endl;
}
break;
}
}
}
void book::loadbook(book* p)
{
ifstream fin;
fin.open("book.txt", ios::in);
if (!fin)
{
cout << "文件打开失败!" << endl;
exit(1);
}
string name;
while (fin >> name)
{
book* newnode = new book;
newnode->book_name = name;
fin >> newnode->author;
fin >> newnode->publisher;
fin >> newnode->price;
p->next = newnode;
p = p->next;
}
fin.close();
}
void book::savebook(book* p)
{
ofstream fout;
fout.open("book.txt", ios::out);
if (!fout)
{
cout << "文件打开失败!" << endl;
exit(1);
}
while (p->next != NULL)
{
fout << p->next->book_name << "\t";
fout << p->next->author << "\t";
fout << p->next->publisher << "\t";
fout << p->next->price << "\n";
p = p->next;
}
fout.close();
}
class user
{
protected:
string ID; //用户ID
string key; //用户密码
string adress; //用户地址
double pay;
string status; //用户身份
book* list; //购物车
public:
user(string, string, string);
string getID() { return ID; } //取用户ID
string getadress() { return adress; }//取用户地址
string getkey() { return key; } //取用户密码
void main_user(); //用户界面
void main_shop_list(); //购物车管理界面
void showbook(); //显示书籍信息
void addbook(book); //向购物车中添加书籍
void deletebook(book); //从购物车中删除书籍
void load(book); //从文件读入购物车信息
void save(book); //向文件输出购物车信息
int signin(string i, string k) //用户登录函数
{
if (i == ID && k == key) return 1;
else return 0;
}
void showbuyer(); //显示用户信息
void sum(book); //购物车结算函数
};
user::user(string I, string k, string ad)
{
ID = I; key = k; adress = ad; status = "user";
list = new book();
}
void user::addbook(book p)
{
book* p1 = new book;
p1->loadbook(p1);
p1->showbook(p1);
if (p1->next == NULL)
{
cout << "购物车当前为空!" << endl;
return;
}
cout << endl << "请输入要添加的书籍名称:" << endl;
string name; cin >> name;
while (p1->next != NULL)
{
if (p1->next->book_name == name)
{
cout << "找到了!" << endl;
cout << "—————————————————————————————————————————————————————————\n";
cout << "书名:" << "《" << p1->next->book_name << "》" << '\t';
cout << "作者:" << p1->next->author << '\t';
cout << "出版社:" << p1->next->publisher << '\t';
cout << "价格:" << p1->next->price << '\n';
cout << "—————————————————————————————————————————————————————————\n\n";
cout << "是否将此书加入购物车?" << endl;
cout << "1) 是\t0)否" << endl;
int o; cin >> o;
if (o == 1)
{
while (p->next != NULL)
p = p->next;
p->next = p1->next;
p1->next->next = NULL;
system("cls");
cout << "添加成功!" << endl;
break;
}
else if (o == 0)
{
break;
}
else
{
cout << "请输入正确指令!" << endl;
break;
}
}
p1 = p1->next;
}
if (p1->next == NULL)
{
cout << "未找到符合条件的书籍!" << endl;
}
}
void user::deletebook(book* p)
{
if (p->next == NULL)
{
cout << "购物车当前为空!" << endl;
return;
}
cout << endl << "请输入要删除的书籍名称:" << endl;
string name; cin >> name;
int i = 0;
while (p->next != NULL)
{
if (p->next->book_name == name)
{
i++;
cout << "找到了!" << endl;
cout << "—————————————————————————————————————————————————————————\n";
cout << "书名:" << "《" << p->next->book_name << "》" << '\t';
cout << "作者:" << p->next->author << '\t';
cout << "出版社:" << p->next->publisher << '\t';
cout << "价格:" << p->next->price << '\n';
cout << "—————————————————————————————————————————————————————————\n\n";
cout << "是否将此书从购物车中移除?" << endl;
cout << "1) 是\t0)否" << endl;
int o; cin >> o;
if (o == 1)
{
p->next = p->next->next;
system("cls");
cout << "删除成功!" << endl;
break;
}
else if (o == 0)
{
break;
}
else
{
cout << "请输入正确指令!" << endl;
break;
}
}
p = p->next;
}
if (i == 0)
{
cout << "未找到符合条件的书籍!" << endl;
}
}
void user::load(book* p)
{
if (p->next != NULL)p->next = NULL;
string fname = this->ID + ".txt";
ifstream fin;
fin.open(fname, ios::in);
if (!fin)
{
cout << "文件打开失败!" << endl;
exit(1);
}
string name;
book* p1 = new book;
book* p2 = p1;
list->loadbook(p1);
while (fin >> name)
{
book* newnode = new book;
newnode->book_name = name;
fin >> newnode->author;
fin >> newnode->publisher;
fin >> newnode->price;
while (p1->next != NULL)
{
if (p1->next->book_name == newnode->book_name)
{
newnode->author = p1->next->author;
newnode->publisher = p1->next->publisher;
newnode->price = p1->next->price;
break;
}
else p1 = p1->next;
}
if (p1->next != NULL)
{
p->next = newnode;
p = p->next;
p1 = p2;
}
}
fin.close();
}
void user::save(book* p)
{
string fname = this->ID + ".txt";
ofstream fout;
fout.open(fname, ios::out);
if (!fout)
{
cout << "文件打开失败!" << endl;
exit(1);
}
while (p->next != NULL)
{
fout << p->next->book_name << "\t";
fout << p->next->author << "\t";
fout << p->next->publisher << "\t";
fout << p->next->price << "\n";
p = p->next;
}
fout.close();
}
void user::main_user()
{
int o;
while (1)
{
system("cls");
printf("\n\n\n");
printf("\t\t\t\t\t+------------------------------+\n");
printf("\t\t\t\t\t|--------------用户-----------|\n");
printf("\t\t\t\t\t+-------------------------------+\n");
printf("\t\t\t\t\t\t1 查看书籍\n");
printf("\t\t\t\t\t\t2 管理购物车\n");
printf("\t\t\t\t\t\t3 查看个人信息\n");
printf("\t\t\t\t\t\t4 登出\n");
printf("\n\n\n");
printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
printf("\t\t\t\t");
cin >> o;
switch (o)
{
case 1:
{
system("cls");
showbook();
break;
}
case 2:
{
main_shop_list();
break;
}
case 3:
{
showbuyer();
break;
}
case 4:
{return; }
default:
{cout << "请输入正确指令!" << endl << endl; }
}
system("pause");
}
}
void user::main_shop_list()
{
this->load(list);
while (1)
{
system("cls");
cout << this->getID() << "的购物车:" << endl << endl;
list->showbook(list);
printf("\n\n\n");
printf("\t\t\t\t\t+-------------DoKiDoKi----------+\n");
printf("\t\t\t\t\t|--------------购物车-----------|\n");
printf("\t\t\t\t\t+-------------------------------+\n");
printf("\t\t\t\t\t\t1 向购物车中添加书籍\n");
printf("\t\t\t\t\t\t2 从购物车中删除书籍\n");
printf("\t\t\t\t\t\t3 结算\n");
printf("\t\t\t\t\t\t4 退出购物车\n");
printf("\n\n\n");
printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
printf("\t\t\t\t");
int o;
cin >> o;
switch (o)
{
case 1:
{
addbook(list);
break;
}
case 2:
{
deletebook(list);
break;
}
case 3:
{
sum(list);
break;
}
case 4:
{
save(list);
return;
}
default:
{cout << "指令错误!" << endl; }
}
system("pause");
}
}
void user::showbook()
{
ifstream fin;
fin.open("book.txt", ios::in);
if (!fin)
{
cout << "文件打开失败!" << endl;
exit(1);
}
int i = 0;
while (fin >> list->book_name)
{
cout << "NO." << ++i << endl;
fin >> list->author;
fin >> list->publisher;
fin >> list->price;
cout << "书名:" << "《" << list->book_name << "》" << '\t';
cout << "作者:" << list->author << '\t';
cout << "出版社:" << list->publisher << '\t';
cout << "价格:" << list->price << endl;
}
fin.close();
}
void user::showbuyer()
{
cout << "用户名:" << getID() << endl;
cout << "用户地址:" << getadress() << endl;
cout << "用户身份:" << status << endl;
}
void user::sum(book* p)
{
double pay = 0;
while (p->next != NULL)
{
pay += p->next->price;
p = p->next;
}
cout << "用户 " << this->getID() << " 应付" << pay << "元" << endl;
}
class admin //管理员类
{
private:
string ID;
string key;
string status;
book* list;
public:
string getID() { return ID; } //取管理员ID
string getkey() { return key; } //取管理员密码
admin(string i, string k)
{
ID = i; key = k; status = "ad"; list = new book;
}
int signin()
{
string i, k;
cout << "请输入账号:";
cin >> i;
cout << "请输入密码:";
cin >> k;
if (i == ID && k == key) return 1;
else return 0;
}
void main_ad();
};
void admin::main_ad()
{
list->loadbook(list);
while (1)
{
system("cls");
printf("\n\n\n");
printf("\t\t\t\t\t+------------------------------+\n");
printf("\t\t\t\t\t|--------------管理员-----------|\n");
printf("\t\t\t\t\t+-------------------------------+\n");
printf("\t\t\t\t\t\t1 查看书籍\n");
printf("\t\t\t\t\t\t2 添加书籍\n");
printf("\t\t\t\t\t\t3 删除书籍\n");
printf("\t\t\t\t\t\t4 保存登出\n");
printf("\n\n\n");
printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
printf("\t\t\t\t");
int o;
cin >> o;
switch (o)
{
case 1:
{system("cls");
list->showbook(list);
break;
}
case 2:
{
list->addbook(list);
break;
}
case 3:
{
list->deletbook(list);
break;
}
case 4:
{
list->savebook(list);
return;
}
default:
{ cout << "请输入正确指令!" << endl << endl; }
}
system("pause");
}
}
int main()
{
admin A("admin", "160922");
user B("me", "19170", "中国某省某市某区");
while (1)
{
system("cls");
printf("\n\n\n");
printf("\t\t\t\t\t+-------------------------------+\n");
printf("\t\t\t\t\t|-----------图书购买系统--------|\n");
printf("\t\t\t\t\t+-------------------------------+\n");
printf("\t\t\t\t\t\t1 管理员登录\n");
printf("\t\t\t\t\t\t2 用户登录\n");
printf("\t\t\t\t\t\t3 退出\n");
printf("\n\n\n");
printf("\t\t\t\t\t请输入你想实现的功能前的代号:\n");
printf("\t\t\t\t");
int o;
cin >> o;
switch (o)
{
case 1:
{
if (A.signin() == 1)
{
A.main_ad();
}
else cout << "账号或密码错误" << endl;
break;
}
case 2:
{
string id, k;
cout << "请输入账号:"; cin >> id;
cout << "请输入密码:"; cin >> k;
if (id == "me" && k == "19170")
{
B.main_user();
break;
}
else
cout << "账号或密码错误" << endl;
break;
break;
}
case 3:
{ return 0; }
default:
{ cout << "请输入正确指令!" << endl << endl; }
}
system("pause");
}
}
源程序运行及功能
该图书管理系统的功能包括添加书籍、展示书籍、删除书籍、加载书籍和保存书籍等。系统使用了类的概念,包括了图书类和用户类。
缺陷分析
在阅读源代码时,我发现了一些潜在的缺陷和改进点:
- 输入验证不完善:在添加书籍功能中,对于价格的输入没有进行有效的验证,导致用户可以输入非法的价格。
- 错误处理不完善:在输入无效数据时,系统没有给出明确的错误提示,而是简单地清屏并输出"无效输入",这对用户来说不够友好。
- 文件操作不安全:在加载书籍和保存书籍的功能中,使用了文件操作,但没有进行错误处理和异常处理,可能导致文件打开失败或写入失败的情况。
改进建议
为了改进这些缺陷,我提出以下建议:
- 输入验证:在添加书籍功能中,应该对价格进行有效的验证,确保用户输入的价格是合法的数字。
- 错误处理:在输入无效数据时,应该给出明确的错误提示,告知用户输入的数据无效,并要求重新输入。
- 异常处理:在文件操作中,应该添加适当的异常处理机制,以处理文件打开失败或写入失败的情况,并向用户显示错误信息。
改进过程
在进行二次开发时,我按照上述建议进行了改进。具体的改进过程和代码修改可以在我的博客文章中找到。
结论
通过对图书管理系统的分析和改进,我意识到在软件开发过程中,我们需要关注输入验证、错误处理和异常处理等方面,以提高软件的稳定性和用户体验。
标签:cout,管理系统,void,next,book,printf,二次开发,图书,string From: https://www.cnblogs.com/keji-tu/p/18056687