修改这段代码中的错误
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <limits>
using namespace std;
class buyer; // 基类前向声明
class buyer // 基类
{
protected:
string name; // 姓名
int buyerID; // 购书人编号
string address; // 地址
double pay; // 购书费用
public:
buyer();
buyer(string n, int b, string a, double p);
virtual ~buyer(); // 虚析构函数
string getbuyname(); // 取姓名
string getaddress(); // 取地址
double getpay(); // 取应付费用
int getid(); // 取购书人编号
virtual void display() = 0; // 显示对象
virtual void setpay(double = 0) = 0; // 计算购书费用
};
// 会员类
class member : public buyer
{
private:
int leaguer_grade; // 会员级别
public:
member(string n, int b, int l, string a, double p) : buyer(n, b, a, p)
{
leaguer_grade = l;
} // 构造函数
~member(); // 析构函数
void display(); // 显示函数
void setpay(double p);
};
// 贵宾类
class honoured_guest : public buyer
{
private:
double discount_rate; // 折扣率
public:
honoured_guest(string n, int b, double r, string a, double p) : buyer(n, b, a, p)
{
discount_rate = r;
} // 构造函数
~honoured_guest(); // 析构函数
void display(); // 显示对象
void setpay(double p); // 计算购书费用
};
// 普通人类
class layfolk : public buyer
{
public:
layfolk(string n, int b, string a, double p) : buyer(n, b, a, p) {} // 构造函数
~layfolk(); // 析构函数
void display(); // 显示对象
void setpay(double p); // 计算购书费用
};
// 图书类
class book
{
protected:
string book_ID; // 书号
string book_name; // 书名
string author; // 作者
string publishing; // 出版社
double price; // 定价
public:
book();
book(string b_id, string b_n, string au, string pu, double pr);
virtual ~book(); // 虚析构函数
void display();
string getbook_ID(); // 取书号
string getbook_name(); // 取书名
string getauthor(); // 取作者
string getpublishing(); // 取出版社
double getprice(); // 取定价
};
class ShoppingCart {
private:
vector<book*> books;
vector<int> quantities;
public:
~ShoppingCart(); // 析构函数
void addBook(book* b, int quantity);
void removeBook(int index);
void display();
double calculateTotalPrice();
vector<book*> getBooks() const;
vector<int> getQuantities() const;
};
class Order {
private:
int orderID;
buyer* customer;
ShoppingCart cart;
string orderDate;
public:
Order(int id, buyer* c, ShoppingCart& cart, string date)
: orderID(id), customer(c), cart(cart), orderDate(date) {}
~Order(); // 析构函数
void display();
void writeToFile(ofstream& out);
};
void displayMenu();
void processChoice(int choice, vector<buyer*>& customers, vector<book*>& books, ShoppingCart& cart, int& currentCustomerIndex);
vector<buyer*> readCustomersFromFile(const string& filename);
vector<book*> readBooksFromFile(const string& filename);
void writeOrderToFile(const Order& order, const string& filename);
// 基类构造函数
buyer::buyer() : name(""), buyerID(0), address(""), pay(0) {}
buyer::buyer(string n, int b, string a, double p) : name(n), buyerID(b), address(a), pay(p) {}
buyer::~buyer() {}
// 成员函数定义
string buyer::getbuyname() { return name; }
string buyer::getaddress() { return address; }
double buyer::getpay() { return pay; }
int buyer::getid() { return buyerID; }
// 会员类的显示函数
void member::display() {
cout << "会员信息:" << endl;
cout << "姓名:" << name << endl;
cout << "编号:" << buyerID << endl;
cout << "级别:" << leaguer_grade << endl;
cout << "地址:" << address << endl;
}
// 会员类的计算购书费用
void member::setpay(double p) {
switch (leaguer_grade) {
case 1: pay += p * 0.95; break;
case 2: pay += p * 0.90; break;
case 3: pay += p * 0.85; break;
case 4: pay += p * 0.80; break;
case 5: pay += p * 0.70; break;
default: cout << "无效的会员级别!" << endl;
}
}
member::~member() {}
// 贵宾类的显示函数
void honoured_guest::display() {
cout << "贵宾信息:" << endl;
cout << "姓名:" << name << endl;
cout << "编号:" << buyerID << endl;
cout << "折扣率:" << discount_rate << endl;
cout << "地址:" << address << endl;
}
// 贵宾类计算购书费用
void honoured_guest::setpay(double p) {
pay += p * discount_rate;
}
honoured_guest::~honoured_guest() {}
// 普通类显示函数
void layfolk::display() {
cout << "普通客户信息:" << endl;
cout << "姓名:" << name << endl;
cout << "编号:" << buyerID << endl;
cout << "地址:" << address << endl;
}
// 普通类计算购书费用
void layfolk::setpay(double p) {
pay += p;
}
layfolk::~layfolk() {}
// 图书类构造函数
book::book() : book_ID(""), book_name(""), author(""), publishing(""), price(0) {}
book::book(string b_id, string b_n, string au, string pu, double pr)
: book_ID(b_id), book_name(b_n), author(au), publishing(pu), price(pr) {}
book::~book() {}
// 图书类成员函数
void book::display() {
cout << "书籍信息:" << endl;
cout << "书号:" << book_ID << endl;
cout << "书名:" << book_name << endl;
cout << "作者:" << author << endl;
cout << "出版社:" << publishing << endl;
cout << "价格:" << price << endl;
}
string book::getbook_ID() { return book_ID; }
string book::getbook_name() { return book_name; }
string book::getauthor() { return author; }
string book::getpublishing() { return publishing; }
double book::getprice() { return price; }
// 购物车类成员函数
void ShoppingCart::addBook(book* b, int quantity) {
books.push_back(b);
quantities.push_back(quantity);
}
void ShoppingCart::removeBook(int index) {
if (index >= 0 && index < static_cast<int>(books.size())) {
books.erase(books.begin() + index);
quantities.erase(quantities.begin() + index);
}
}
void ShoppingCart::display() {
if (books.empty()) {
cout << "购物车为空。" << endl;
return;
}
cout << "购物车内容:" << endl;
for (size_t i = 0; i < books.size(); ++i) {
books[i]->display();
cout << "数量: " << quantities[i] << endl;
cout << "-----------------" << endl;
}
}
double ShoppingCart::calculateTotalPrice() {
double total = 0.0;
for (size_t i = 0; i < books.size(); ++i) {
total += books[i]->getprice() * quantities[i];
}
return total;
}
vector<book*> ShoppingCart::getBooks() const { return books; }
vector<int> ShoppingCart::getQuantities() const { return quantities; }
ShoppingCart::~ShoppingCart() {}
// 订单类成员函数
void Order::display() {
cout << "订单信息:" << endl;
cout << "订单号:" << orderID << endl;
cout << "日期:" << orderDate << endl;
customer->display();
cout << "总价:" << cart.calculateTotalPrice() << endl;
cout << "详细内容:" << endl;
cart.display();
}
void Order::writeToFile(ofstream& out) {
out << "订单号:" << orderID << endl;
out << "日期:" << orderDate << endl;
out << "客户类型:" << (typeid(*customer).name() == typeid(member).name() ? "会员"
: typeid(*customer).name() == typeid(honoured_guest).name() ? "贵宾"
: "普通客户") << endl;
out << "客户姓名:" << customer->getbuyname() << endl;
out << "客户编号:" << customer->getid() << endl;
out << "客户地址:" << customer->getaddress() << endl;
out << "总价:" << cart.calculateTotalPrice() << endl;
out << "详细内容:" << endl;
for (size_t i = 0; i < cart.getBooks().size(); ++i) {
out << "书名:" << cart.getBooks()[i]->getbook_name() << endl;
out << "书号:" << cart.getBooks()[i]->getbook_ID() << endl;
out << "数量:" << cart.getQuantities()[i] << endl;
out << "单价:" << cart.getBooks()[i]->getprice() << endl;
out << "-----------------" << endl;
}
}
Order::~Order() {}
// 显示菜单函数
void displayMenu() {
cout << "===== 网上购书系统 =====" << endl;
cout << "1. 显示所有客户" << endl;
cout << "2. 显示所有书籍" << endl;
cout << "3. 选择客户" << endl;
cout << "4. 添加书籍到购物车" << endl;
cout << "5. 显示购物车" << endl;
cout << "6. 结账并生成订单" << endl;
cout << "7. 退出系统" << endl;
cout << "========================" << endl;
cout << "请输入您的选择:";
}
// 处理用户选择函数
void processChoice(int choice, vector<buyer*>& customers, vector<book*>& books, ShoppingCart& cart, int& currentCustomerIndex) {
static int orderID = 1; // 用于生成订单号
switch (choice) {
case 1: // 显示所有客户
if (customers.empty()) {
cout << "没有客户信息。" << endl;
} else {
for (auto customer : customers) {
customer->display();
cout << "-----------------" << endl;
}
}
break;
case 2: // 显示所有书籍
if (books.empty()) {
cout << "没有书籍信息。" << endl;
} else {
for (auto book : books) {
book->display();
cout << "-----------------" << endl;
}
}
break;
case 3: // 选择客户
if (customers.empty()) {
cout << "没有客户信息。" << endl;
} else {
int id;
cout << "请输入客户编号:";
cin >> id;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除输入缓冲区
for (size_t i = 0; i < customers.size(); ++i) {
if (customers[i]->getid() == id) {
currentCustomerIndex = static_cast<int>(i);
cout << "已选择客户:" << customers[i]->getbuyname() << endl;
break;
}
}
if (currentCustomerIndex == -1) {
cout << "未找到指定编号的客户。" << endl;
}
}
break;
case 4: // 添加书籍到购物车
if (currentCustomerIndex == -1) {
cout << "请先选择客户。" << endl;
} else if (books.empty()) {
cout << "没有书籍信息。" << endl;
} else {
int bookIndex;
cout << "请输入书籍编号(从1开始):";
cin >> bookIndex;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除输入缓冲区
if (bookIndex >= 1 && bookIndex <= static_cast<int>(books.size())) {
int quantity;
cout << "请输入购买数量:";
cin >> quantity;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除输入缓冲区
if (quantity > 0) {
cart.addBook(books[bookIndex - 1], quantity);
cout << "已将书籍添加到购物车。" << endl;
} else {
cout << "购买数量必须大于0。" << endl;
}
} else {
cout << "无效的书籍编号。" << endl;
}
}
break;
case 5: // 显示购物车
cart.display();
break;
case 6: // 结账并生成订单
if (currentCustomerIndex == -1) {
cout << "请先选择客户。" << endl;
} else if (cart.getBooks().empty()) {
cout << "购物车为空。" << endl;
} else {
string date;
cout << "请输入订单日期(格式:YYYY-MM-DD):";
cin >> date;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除输入缓冲区
Order order(orderID++, customers[currentCustomerIndex], cart, date);
order.display();
char confirm;
cout << "确认生成订单?(y/n): ";
cin >> confirm;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除输入缓冲区
if (confirm == 'y' || confirm == 'Y') {
writeOrderToFile(order, "orders.txt");
cout << "订单已生成并保存。" << endl;
cart = ShoppingCart(); // 清空购物车
} else {
cout << "订单已取消。" << endl;
}
}
break;
case 7: // 退出系统
cout << "感谢使用网上购书系统,再见!" << endl;
exit(0); // 退出程序
break;
default:
cout << "无效的选择,请重新输入。" << endl;
break;
}
cout << endl;
}
// 从文件读取客户信息
vector<buyer*> readCustomersFromFile(const string& filename) {
vector<buyer*> customers;
ifstream infile(filename);
if (!infile) {
cerr << "无法打开文件 " << filename << endl;
return customers;
}
string type, name, address;
int id;
double rate;
while (infile >> type >> id >> name >> address) {
if (type == "会员") {
int level;
infile >> level;
customers.push_back(new member(name, id, level, address, 0));
} else if (type == "贵宾") {
infile >> rate;
customers.push_back(new honoured_guest(name, id, rate, address, 0));
} else if (type == "普通客户") {
customers.push_back(new layfolk(name, id, address, 0));
}
}
infile.close();
return customers;
}
// 从文件读取书籍信息
vector<book*> readBooksFromFile(const string& filename) {
vector<book*> books;
ifstream infile(filename);
if (!infile) {
cerr << "无法打开文件 " << filename << endl;
return books;
}
string id, name, author, pub;
double price;
while (infile >> id >> name >> author >> pub >> price) {
books.push_back(new book(id, name, author, pub, price));
}
infile.close();
return books;
}
// 将订单写入文件
void writeOrderToFile(const Order& order, const string& filename) {
ofstream outfile(filename, ios::app);
if (!outfile) {
cerr << "无法打开文件 " << filename << endl;
return;
}
order.writeToFile(outfile);
outfile << endl;
outfile.close();
}
int main() {
vector<buyer*> customers;
vector<book*> books;
ShoppingCart cart;
int currentCustomerIndex = -1;
customers = readCustomersFromFile("customers.txt");
books = readBooksFromFile("books.txt");
int choice;
do {
displayMenu();
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清除输入缓冲区
processChoice(choice, customers, books, cart, currentCustomerIndex);
} while (true);
// 清理内存
for (auto customer : customers) {
delete customer;
}
for (auto book : books) {
delete book;
}
return 0;
}