写一个c++项目程序设计一个网上购书系统,包含book类,buyer类,buyer类派生了member类、honoured_guest类、layfolk类;order类,容器类,加入文件流输入输出,进行人机交互
时间: 2024-03-29 17:04:14 浏览: 68
好的,我可以为您提供一个大致的实现思路和代码示例。
首先,我们需要定义 Book 类,其中包含书籍的一些基本信息,如书名、作者、出版社、价格等。代码如下:
```cpp
class Book {
public:
Book(string name, string author, string publisher, double price) :
name(name), author(author), publisher(publisher), price(price) {}
string getName() { return name; }
string getAuthor() { return author; }
string getPublisher() { return publisher; }
double getPrice() { return price; }
private:
string name;
string author;
string publisher;
double price;
};
```
接下来,我们需要定义 Buyer 类,其中包含买家的一些基本信息,如姓名、地址、联系方式等。代码如下:
```cpp
class Buyer {
public:
Buyer(string name, string address, string phone) :
name(name), address(address), phone(phone) {}
string getName() { return name; }
string getAddress() { return address; }
string getPhone() { return phone; }
private:
string name;
string address;
string phone;
};
```
然后,我们可以定义 Member 类、HonouredGuest 类和 Layfolk 类,它们都是 Buyer 类的派生类。代码如下:
```cpp
class Member : public Buyer {
public:
Member(string name, string address, string phone, int level, int points) :
Buyer(name, address, phone), level(level), points(points) {}
int getLevel() { return level; }
int getPoints() { return points; }
private:
int level;
int points;
};
class HonouredGuest : public Buyer {
public:
HonouredGuest(string name, string address, string phone, string specialReq) :
Buyer(name, address, phone), specialReq(specialReq) {}
string getSpecialReq() { return specialReq; }
private:
string specialReq;
};
class Layfolk : public Buyer {
public:
Layfolk(string name, string address, string phone) :
Buyer(name, address, phone) {}
};
```
接下来,我们需要定义 Order 类,用于表示订单信息,包含订单编号、买家信息、购买书籍信息等。代码如下:
```cpp
class Order {
public:
Order(int id, Buyer* buyer, Book* book, int quantity) :
id(id), buyer(buyer), book(book), quantity(quantity) {}
int getId() { return id; }
Buyer* getBuyer() { return buyer; }
Book* getBook() { return book; }
int getQuantity() { return quantity; }
private:
int id;
Buyer* buyer;
Book* book;
int quantity;
};
```
然后,我们可以定义一个容器类,用于存储所有的订单信息。可以定义一些函数来添加、删除、修改订单信息等。代码如下:
```cpp
class OrderContainer {
public:
void addOrder(Order* order) {
orders.push_back(order);
}
void removeOrder(int id) {
for (auto it = orders.begin(); it != orders.end(); it++) {
if ((*it)->getId() == id) {
orders.erase(it);
break;
}
}
}
void modifyOrder(int id, int quantity) {
for (auto it = orders.begin(); it != orders.end(); it++) {
if ((*it)->getId() == id) {
(*it)->getBook()->setQuantity(quantity);
break;
}
}
}
private:
vector<Order*> orders;
};
```
最后,我们需要实现文件流输入输出和人机交互。可以定义一些函数来读取和写入订单信息,以及与用户进行交互,如显示菜单、接收用户输入等。代码如下:
```cpp
void addOrder(OrderContainer& container) {
int id, type, quantity;
string name, address, phone, specialReq, bookName, author, publisher;
double price;
cout << "请输入订单编号:";
cin >> id;
cout << "请选择买家类型(1.普通买家 2.会员 3.贵宾):";
cin >> type;
cout << "请输入买家姓名:";
cin >> name;
cout << "请输入买家地址:";
cin >> address;
cout << "请输入买家电话:";
cin >> phone;
switch (type) {
case 1:
container.addOrder(new Order(id, new Layfolk(name, address, phone), nullptr, 0));
break;
case 2:
int level, points;
cout << "请输入会员等级:";
cin >> level;
cout << "请输入会员积分:";
cin >> points;
container.addOrder(new Order(id, new Member(name, address, phone, level, points), nullptr, 0));
break;
case 3:
cout << "请输入特殊要求:";
cin >> specialReq;
container.addOrder(new Order(id, new HonouredGuest(name, address, phone, specialReq), nullptr, 0));
break;
}
cout << "请输入书籍名称:";
cin >> bookName;
cout << "请输入书籍作者:";
cin >> author;
cout << "请输入书籍出版社:";
cin >> publisher;
cout << "请输入书籍价格:";
cin >> price;
cout << "请输入书籍数量:";
cin >> quantity;
container.addOrder(new Order(id, container.getBuyer(id), new Book(bookName, author, publisher, price), quantity));
}
void removeOrder(OrderContainer& container) {
int id;
cout << "请输入订单编号:";
cin >> id;
container.removeOrder(id);
}
void modifyOrder(OrderContainer& container) {
int id, quantity;
cout << "请输入订单编号:";
cin >> id;
cout << "请输入书籍数量:";
cin >> quantity;
container.modifyOrder(id, quantity);
}
void showMenu() {
cout << "欢迎使用网上购书系统!" << endl;
cout << "1. 添加订单" << endl;
cout << "2. 删除订单" << endl;
cout << "3. 修改订单" << endl;
cout << "4. 退出系统" << endl;
}
int main() {
OrderContainer container;
int choice;
do {
showMenu();
cin >> choice;
switch (choice) {
case 1:
addOrder(container);
break;
case 2:
removeOrder(container);
break;
case 3:
modifyOrder(container);
break;
}
} while (choice != 4);
return 0;
}
```
这是一个初步的实现,具体细节还需要根据实际需求进行调整。希望能够帮助到您。
阅读全文