archieve-projects/书店-图书-CPP/图书管理.cpp

214 lines
5.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <time.h>
#include <iostream>
#include <string>
#include <iomanip>
#define MAXSIZE 10000
using namespace std;
//全局变量
static int Bcount = 2;
static int flag = 0;
class Book
{
private:
string ISBN;
string Bname;
double price;
string author;
public:
Book() {};
Book(string is, string nam, double p, string au)
{
ISBN = is;
Bname = nam;
price = p;
author = au;
}
friend ostream& operator<<(ostream& output, const Book& b);
string getISBN() const { return ISBN; }
string getName() const { return Bname; }
string getAuthor() const { return author; }
double getPrice() const { return price; }
void modISBN(string i) { ISBN = i; }
void modName(string n) { Bname = n; }
void modPrice(double pr) { price = pr; }
void modAuthor(string au) { author = au; }
};
//输出流运算符重载
ostream& operator<<(ostream& output, const Book& b)
{
cout.setf(ios_base::left);
output << b.getISBN()<<"\t"<<b.getName() << "\t" << b.getAuthor() << "\t" << b.getPrice() << "\t" << endl;
return output;
}
//预置书籍类数据
Book Books[MAXSIZE] = {
Book("9787302257646","程序设计基础",25,"张三"),
Book("9787302219972","操作系统原理",32,"李四")
};
class Admin
{
public:
Admin() {};
void AddBook();
void DelBook();
void SearchBook();
void modify();
};
//输出所有Book
void BooksPrint()
{
cout << "———————— 书籍信息 ————————" << endl;
cout << "序号\tISBN\t\t书名\t\t作者\t价格" << endl;
for (int i = 0; i < Bcount; i++) cout << i + 1 << "\t" << Books[i];
cout << endl;
}
//添加新书籍
void NewBook()
{
string ISBN;
string Bname;
double price;
string author;
cout << "------------------------------------------" << endl;
cout << "正在添加新的书籍信息,当前已有书籍数目:" << Bcount << endl;
cout << "输入ISBN号"; cin >> ISBN;
cout << "请输入书名:"; cin >> Bname;
cout << "请输入价格:"; cin >> price;
cout << "请输入作者名:"; cin >> author;
Books[Bcount] = Book(ISBN, Bname, price, author);
Bcount++;
cout << "添加成功!" << endl;
}
//增加书刊
void Admin::AddBook()
{
int select = 1;
cout << setw(20) << ">>>>>>>>>>增加书刊<<<<<<<<<<" << endl;
while (select)
{
NewBook();
cout << "输入0退出该操作输入其他数字继续添加。" << endl << "请输入:";
cin >> select;
}
}
//删除书刊
void Admin::DelBook()
{
string s = "1";
while (1) {
cout << "请输入作者名、书名或ISBN号输入0返回主菜单";
cin >> s;
if (s == "0") break; //退出判断
int flag = 0; //查找成功状态符
//查找书籍类
for (int i = 0; i < Bcount; i++)
if (Books[i].getAuthor() == s || Books[i].getISBN() == s || Books[i].getName() == s)
{
flag = 1;
cout << "发现目标书籍如下是否删除该书籍信息Y 是 / N 否)"
<< endl << "------------------------"
<< endl << Books[i] << "------------------------" << endl << "请选择:";
char c; cin >> c;
if (c == 'Y' || c == 'y')
{
for (int j = i; j < Bcount - 1; j++)
{
Books[j] = Books[j + 1];
}
Bcount--;
}
else continue;
}
else continue;
if (flag) cout << "删除成功!" << endl;
else cerr << "未找到目标书籍。" << endl;
}
}
//修改书籍类
void modifyBook()
{
int select = 1;
BooksPrint();
int a, b;
string s = "";
double pr = 0;
cout << "要操作的书籍编号是:"; cin >> a;
if (a<1 || a>Bcount)
{
cerr << "序号错误,请检查。" << endl;
}
else
{
cout << "1.ISBN\t2.书籍名称\t3.作者\t4.价格\t0.放弃修改" << endl << "要修改的信息是:"; cin >> b;
switch (b)
{
case 1: cout << "输入修改后的信息:"; cin >> s; Books[a - 1].modISBN(s); cout << Books[a - 1] << "修改成功!" << endl; break;
case 2: cout << "输入修改后的信息:"; cin >> s; Books[a - 1].modName(s); cout << Books[a - 1] << "修改成功!" << endl; break;
case 3: cout << "输入修改后的信息:"; cin >> s; Books[a - 1].modAuthor(s); cout << Books[a - 1] << "修改成功!" << endl; break;
case 4: cout << "输入修改后的信息:"; cin >> pr; Books[a - 1].modPrice(pr); cout << Books[a - 1] << "修改成功!" << endl; break;
case 0: break;
}
}
}
//修改书籍信息
void Admin::modify()
{
int select = 1;
cout << setw(20) << ">>>>>>>>>>修改书刊信息<<<<<<<<<<" << endl;
while (select)
{
modifyBook();
cout << "输入 0 退出该操作,输入其他数字继续修改" << endl << "请输入:";
cin >> select;
}
}
//运行中函数
void MainLoop()
{
Admin admin;
int select = 1;
while (select)
{
cout << "==============================="
<< endl << "====欢迎使用图书管理系统===="
<< endl << "===============================" << endl
<< setw(20) << "————————————————" << endl
<< "1.增加书刊信息" << endl
<< "2.修改书刊信息" << endl
<< "3.删除书刊信息" << endl
<< "4.查看所有图书" << endl
<< "0.退出书店系统" << endl
<< setw(20) << "————————————————" << endl;
cout << "请输入操作序号:";
cin >> select;
if (select < 0 || select >4)
{
cerr << "非法值,请重新输入。" << endl;
continue;
}
else
{
switch (select)
{
case 1: admin.AddBook(); continue;
case 2: admin.modify(); continue;
case 3: admin.DelBook(); continue;
case 4: BooksPrint(); continue;
case 0: break;
}
}
}
}
//主函数
int main()
{
MainLoop();
return 0;
}