archieve-projects/书店-图书-CPP/Books.h

496 lines
13 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 Jcount = 2;
static double TTotal;
static int flag = 0;
class BookBase
{
private:
string ISBN = "00000000";
string Bname = "默认书籍";
int stock = 0;
double price = 0;
double total = 0;
public:
BookBase() {};
BookBase(string is, string nam, int s, double p)
{
ISBN = is;
Bname = nam;
stock = s;
price = p;
}
string getISBN() const { return ISBN; }
string getName() const { return Bname; }
int getStock() const { return stock; }
double getPrice() const { return price; }
double getTotal() const { return total; }
void modISBN(string i) { ISBN = i; }
void modName(string n) { Bname = n; }
void modStock(int st) { stock = st; }
void modPrice(double pr) { price = pr; }
void addStock(int s)
{
stock += s;
cout << "库存增加成功!目前库存为:" << getStock() << endl;
}
void outStock(int s)
{
if (s <= stock)
{
stock -= s;
cout << "销售成功!目前库存为:" << getStock() << endl;
total = s * price;
TTotal += total;
cout << "销售额为:" << getTotal() << endl;
flag = 1;
}
else
{
cerr << "库存不足,请检查,目前库存仅有" << getStock() << "" << endl;
return;
}
}
};
//期刊类
class Journal :public BookBase
{
private:
string express;
public:
Journal() {};
Journal(string is, string nam, int s, double p, string exp) :BookBase(is, nam, s, p) { express = exp; }
friend ostream& operator<<(ostream& output, const Journal& j);
string getExpress() const { return express; }
void modExpress(string ex) { express = ex; }
};
//输出流运算符重载
ostream& operator<<(ostream& output, const Journal& j)
{
cout.setf(ios_base::left);
output << j.getISBN() << "\t" << setw(25) << j.getName() << setw(20) << j.getExpress() << setw(15) << j.getPrice() << setw(15) << j.getStock() << endl;
return output;
}
//书籍类
class Book :public BookBase
{
private:
string author;
public:
Book() {};
Book(string is, string nam, int s, double p, string au) :BookBase(is, nam, s, p) { author = au; }
friend ostream& operator<<(ostream& output, const Book& b);
string getAuthor() const { return author; }
void modAuthor(string au) { author = au; }
};
//输出流运算符重载
ostream& operator<<(ostream& output, const Book& b)
{
cout.setf(ios_base::left);
output << b.getISBN() << "\t" << setw(25) << b.getName() << setw(20) << b.getAuthor() << setw(15) << b.getPrice() << setw(15) << b.getStock() << endl;
return output;
}
/*
* 以上为书籍相关类及成员函数,以下为管理员类及其成员函数
*/
class Admin
{
private:
string ID;
public:
Admin() {};
string getID() const { return ID; }
Admin(string id) { ID = id; cout << this->getID() << ",欢迎您登陆管理系统!所有权限已开启。" << endl; }
void changeID(string id) { ID = id; }
void AddBook();
void DelBook();
void SearchBook();
void inStock();
void outStock();
void printAll();
void statics();
void modify();
};
//预置书籍类数据
static Book Books[MAXSIZE] = {
Book("9787302257646","程序设计基础",190,25,"张三"),
Book("9787302219972","单片机技术及应用",182,32,"李四")
};
//预置期刊类数据
static Journal Journals[MAXSIZE] = {
Journal("9787302257611","电脑爱好者",200,32,"北京大学出版社"),
Journal("9787302257622","无线电年刊",190,35,"江西财大出版社")
};
//输出所有Journal
void JournalsPrint()
{
cout << "———————— 期刊类 ————————" << endl;
cout.setf(ios_base::left);
cout << "序号\tISBN\t\t"<<setw(25)<<"期刊名"<<setw(20)<<"出版社"<<setw(15)<<"价格"<<setw(15)<<"库存" << endl;
for (int i = 0; i < Jcount; i++) cout << i + 1 << "\t" << Journals[i];
cout << endl;
}
//输出所有Book
void BooksPrint()
{
cout << "———————— 书籍类 ————————" << endl;
cout.setf(ios_base::left);
cout << "序号\tISBN\t\t" << setw(25) << "书名" << setw(20) << "作者" << setw(15) << "价格" << setw(15) << "库存" << endl;
for (int i = 0; i < Bcount; i++) cout << i + 1 << "\t" << Books[i];
cout << endl;
}
void Admin::statics()
{
char c;
if (flag)
{
cout << "------------------------------------------" << endl;
cout << setw(25) << internal << "销售统计" << endl;
cout << "总销售额:" << TTotal << endl;
cout << "是否将销售额导出至文件Y/N"; cin >> c;
if(c=='Y' || c=='y')
{
ofstream file;
file.open("销售额.txt", ios::out);
file << "总销售额:" << TTotal << endl;
file.close();
cout << "导出成功!" << endl;
}
cout << "------------------------------------------" << endl;
}
else
{
cerr << "无任何销售记录!" << endl;
}
}
//添加新书籍
void NewBook()
{
string ISBN;
string Bname;
int stock;
double price;
string author;
cout << "------------------------------------------" << endl;
cout << "正在添加新的书籍信息,当前已有书籍数目:" << Bcount << endl;
cout << "输入ISBN号"; cin >> ISBN;
cout << "请输入书名:"; cin >> Bname;
cout << "请输入库存:"; cin >> stock;
cout << "请输入价格:"; cin >> price;
cout << "请输入作者名:"; cin >> author;
Books[Bcount] = Book(ISBN, Bname, stock, price, author);
Bcount++;
cout << "添加成功!" << endl;
}
//添加新期刊
void NewJournal()
{
string ISBN;
string Bname;
int stock;
double price;
string express;
cout << "正在添加新的期刊信息,当前已有期刊数目:" << Bcount << endl;
cout << "输入ISBN号"; cin >> ISBN;
cout << "请输入期刊名:"; cin >> Bname;
cout << "请输入库存:"; cin >> stock;
cout << "请输入价格:"; cin >> price;
cout << "请输入作者名:"; cin >> express;
Journals[Jcount] = Journal(ISBN, Bname, stock, price, express);
Jcount++;
cout << "添加成功!" << endl;
}
//增加书刊
void Admin::AddBook()
{
int select = 1;
cout << setw(20) << ">>>>>>>>>>增加书刊<<<<<<<<<<" << endl
<< setw(20) << "1.书籍类" << endl
<< setw(20) << "2.期刊类" << endl
<< setw(20) << "0.退出该操作" << endl;
while (select)
{
cout << "要对哪类进行增加操作输入0返回主菜单:";
cin >> select;
if (select < 0 || select > 2)
{
cerr << "非法值,请重新输入。" << endl;
continue;
}
else
{
switch (select)
{
case 1: NewBook(); continue;
case 2: NewJournal(); continue;
case 0: break;
}
}
}
}
//删除书刊
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;
//查找期刊类
for (int i = 0; i < Jcount; i++)
if (Journals[i].getExpress() == s || Journals[i].getISBN() == s || Journals[i].getName() == s)
{
flag = 1;
cout << "发现目标期刊如下是否删除该期刊信息Y 是 / N 否)"
<< endl << "------------------------"
<< endl << Journals[i] << "------------------------" << endl << "请选择:";
char c; cin >> c;
if (c == 'Y' || c == 'y')
{
for (int j = i; j < Jcount - 1; j++)
{
Journals[j] = Journals[j + 1];
}
Jcount--;
}
else continue;
}
else continue;
if (flag)cout << "删除成功!" << endl;
else cerr << "未找到目标书籍。" << endl;
}
}
//入库
void Admin::inStock()
{
int select = 1;
cout << setw(20) << ">>>>>>>>>>入库书刊<<<<<<<<<<" << endl
<< setw(20) << "1.书籍类" << endl
<< setw(20) << "2.期刊类" << endl
<< setw(20) << "0.退出该操作" << endl;
while (select)
{
cout << "要对哪类进行入库操作输入0返回主菜单:";
cin >> select;
if (select < 0 || select > 2)
{
cerr << "非法值,请重新输入。" << endl;
continue;
}
else
{
switch (select)
{
case 1:
{
BooksPrint();
int a, b;
cout << "要操作的书籍编号是:"; cin >> a;
if (a<1 || a>Bcount)
{
cerr << "序号错误,请检查。" << endl;
break;
}
cout << "要增加的库存量是:"; cin >> b;
Books[a - 1].addStock(b);
continue;
}
case 2:
{
JournalsPrint();
int a, b;
cout << "要操作的期刊编号是:"; cin >> a;
if (a<1 || a>Jcount)
{
cerr << "序号错误,请检查。" << endl;
break;
}
cout << "要增加的库存量是:"; cin >> b;
Books[a - 1].addStock(b);
continue;
}
case 0: break;
}
}
}
}
//修改书籍类
void modifyBook()
{
int select = 1;
BooksPrint();
int a, b;
string s;
int st;
double pr;
cout << "要操作的书籍编号是:"; cin >> a;
if (a<1 || a>Bcount)
{
cerr << "序号错误,请检查。" << endl;
}
else
{
cout << "1.ISBN\t2.书籍名称\t3.作者\t4.库存量\t5.价格\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 >> st; Books[a - 1].modStock(st); cout << Books[a - 1] << "修改成功!" << endl; break;
case 5: cout << "输入修改后的信息:"; cin >> pr; Books[a - 1].modPrice(pr); cout << Books[a - 1] << "修改成功!" << endl; break;
case 0: break;
}
}
}
//修改期刊类
void modifyJournal()
{
int select = 1;
JournalsPrint();
int a, b;
string s;
int st;
double pr;
cout << "要操作的书籍编号是:"; cin >> a;
if (a<1 || a>Bcount)
{
cerr << "序号错误,请检查。" << endl;
}
else
{
cout << "1.ISBN\t2.书籍名称\t3.出版社\t4.库存量\t5.价格\t0.放弃修改" << endl << "要修改的信息是:"; cin >> b;
switch (b)
{
case 1: cout << "输入修改后的信息:"; cin >> s; Journals[a - 1].modISBN(s); cout << Journals[a - 1] << "修改成功!" << endl; break;
case 2: cout << "输入修改后的信息:"; cin >> s; Journals[a - 1].modName(s); cout << Journals[a - 1] << "修改成功!" << endl; break;
case 3: cout << "输入修改后的信息:"; cin >> s; Journals[a - 1].modExpress(s); cout << Journals[a - 1] << "修改成功!" << endl; break;
case 4: cout << "输入修改后的信息:"; cin >> st; Journals[a - 1].modStock(st); cout << Journals[a - 1] << "修改成功!" << endl; break;
case 5: cout << "输入修改后的信息:"; cin >> pr; Journals[a - 1].modPrice(pr); cout << Journals[a - 1] << "修改成功!" << endl; break;
case 0: break;
}
}
}
//修改书籍信息
void Admin::modify()
{
int select = 1;
cout << setw(20) << ">>>>>>>>>>修改书刊信息<<<<<<<<<<" << endl
<< setw(20) << "1.书籍类" << endl
<< setw(20) << "2.期刊类" << endl
<< setw(20) << "0.退出该操作" << endl;
while (select)
{
cout << "要对哪类进行修改操作输入0返回主菜单:";
cin >> select;
if (select < 0 || select > 2)
{
cerr << "非法值,请重新输入。" << endl;
continue;
}
else
{
switch (select)
{
case 1: modifyBook(); continue;
case 2: modifyJournal(); continue;
case 0: break;
}
}
}
}
//入库
void Admin::outStock()
{
int select = 1;
cout << setw(20) << ">>>>>>>>>>销售书刊<<<<<<<<<<" << endl
<< setw(20) << "1.书籍类" << endl
<< setw(20) << "2.期刊类" << endl
<< setw(20) << "0.退出该操作" << endl;
while (select)
{
cout << "要对哪类进行销售操作输入0返回主菜单:";
cin >> select;
if (select < 0 || select > 2)
{
cerr << "非法值,请重新输入。" << endl;
continue;
}
else
{
switch (select)
{
case 1:
{
BooksPrint();
int a, b;
cout << "要操作的书籍序号是:"; cin >> a;
if(a<1 || a>Bcount)
{
cerr << "序号错误,请检查。" << endl;
break;
}
cout << "销售的库存量是:"; cin >> b;
Books[a - 1].outStock(b);
continue;
}
case 2:
{
JournalsPrint();
int a, b;
cout << "要操作的期刊序号是:"; cin >> a;
if (a<1 || a>Jcount)
{
cerr << "序号错误,请检查。" << endl;
break;
}
cout << "要销售的库存量是:"; cin >> b;
Books[a - 1].outStock(b);
continue;
}
case 0: break;
}
}
}
}
//
inline void Admin::printAll()
{
BooksPrint();
JournalsPrint();
}