问题如下:
用C++完成下列任务:
(1) 创建一个具有几个私有成员变量的产品类,如产品ID、产品名称、产品数量和价格。
(2) 创建一个基类Person,该类有几个私有成员变量,如人名、性别、手机号码和地址。性别应该是一个枚举。
(3) 在Person类的基础上创建一个子类Customer。它有额外的私有成员变量、 如客户ID、资本和客户正在购买的产品的向量。每个客户有5,000元人民币的初始资金。
(4) 在Person类的基础上创建一个子类Manager。该经理有额外的私有成员变量,如年龄和职称。
(5) 创建一个主进程类。它有一些私有成员变量,例如一个保存所有产品信息的产品向量,一个保存所有客户的客户向量,一个能够管理客户和产品的经理,以及一个正在购买产品的客户。此外,它还有一些函数,用于输出供经理和客户选择的操作菜单。
(6) 当程序运行时,它显示的主菜单(Main operation menu)如下:
(7) 如果输入的是1,那么就进入经理的菜单(Manager’s operation menu),如下所示:
经理可以添加新产品、查询产品、添加新客户和查询客户。如果用户输入5,则程序将返回到主操作菜单中显示的上级菜单。
a) 添加产品:经理可以将新产品添加到产品向量中,以便客户可以购买这些产品。
经理可以继续输入产品ID、名称、数量和价格,以创建并将新产品添加到产品向量中。不能将重复的产品ID添加到产品向量中。当经理输入0停止添加时,经理操作菜单(Manager’s operation menu)将在窗口中输出,并且经理可以输入另一个数字执行其他操作。若我们在经理的菜单选择“1:add products”,输出示例(Operation of adding products)如下所示:
b) 查询产品:管理员可以输入产品ID来查询特定产品,或输入“*”来列出产品向量中的所有产品。如果没有特定产品,则输出“No product found!”
若我们在经理的菜单选择“2: query products”输出示例(Operation of querying products)如下所示:
c) 添加客户:经理可以将新客户添加到客户向量中。
管理员可以输入客户ID、姓名、性别、手机号码和地址来创建并将新客户添加到客户向量中。重复的客户ID不能添加到客户向量中。当管理员输入0以停止添加时,管理操作菜单将在窗口上输出,管理员可以输入另一个数字进行其他操作。
若我们在经理的菜单选择“3: add customers”输出示例(Operation of adding customers)如下所示:
d) 查询产品:管理员可以输入产品ID来查询特定产品,或输入“*”来列出产品向量中的所有产品。如果没有特定产品,则输出“未找到产品!”
若我们在经理的菜单选择“4: query customers”输出示例(Operation of query customers)如下所示:
e) 移动到上级菜单:如果管理员输入5,那么程序就会移动到主菜单.
若我们在经理的菜单选择“5: move to parent menu”输出示例(Operation of move to parent menu )如下所示:
(8) 在主操作菜单中,如果用户输入2,则系统将要求用户输入客户ID以确认哪个客户将购买产品。如果客户ID存在于客户向量中,若我们在主菜单选择“2: customer”输出示例(Customer’s operation menu)如下所示:
a) 将产品添加到购物车:客户可以将产品添加到购物车中,以便客户可以购买这些产品。客户可以输入产品ID和他/她想要购买的数量。如果客户资金不足或客户想要的数量大于产品的可用数量,程序将显示提醒。如果添加操作成功运行,程序应输出成功信息。此外,产品向量中的产品数量应相应减少。
若我们在客户操作菜单选择“1: add products into cart”输出示例(Operation of adding products into cart)如下所示:
b) 查询购物车中的产品:客户可以查询购物车中添加的所有产品。客户可以输入特定的产品ID来查询一个产品,或输入“*”来列出购物车中的所有产品。
若我们在客户操作菜单选择“2: query products In cart”输出示例(Operation of querying products in cart)如下所示:
c) 结账:客户可以查看购物车中所有产品的账单。它将列出客户的ID、姓名、性别和资本,购物车中的所有产品以及总价格。如果购物车结账成功,客户的资本应根据客户购买的产品总价扣除。
若我们在客户操作菜单选择“3: check up”输出示例(checking up operation)如下所示:
d) 若我们在客户操作菜单选择”move to parent menu” ,程序将会回到主菜单。输出示例如下
根据以上要求本人的代码如下:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 产品类
class Product {
private:
int productId;
string productName;
int productQuantity;
double productPrice;
public:
// 构造函数
Product(int id, string name, int quantity, double price)
: productId(id), productName(name), productQuantity(quantity), productPrice(price) {}
// 获取产品ID
int getProductId() const {
return productId;
}
// 获取产品名称
string getProductName() const {
return productName;
}
// 获取产品数量
int getProductQuantity() const {
return productQuantity;
}
// 获取产品价格
double getProductPrice() const {
return productPrice;
}
};
// 性别枚举
enum Gender {
Male,
Female
};
// 基类Person
class Person {
private:
string name;
Gender gender;
string phoneNumber;
string address;
public:
// 构造函数
Person(string n, Gender g, string phone, string addr)
: name(n), gender(g), phoneNumber(phone), address(addr) {}
// 获取姓名
string getName() const {
return name;
}
// 获取性别
Gender getGender() const {
return gender;
}
// 获取手机号码
string getPhoneNumber() const {
return phoneNumber;
}
// 获取地址
string getAddress() const {
return address;
}
};
// 子类Customer
class Customer : public Person {
private:
int customerId;
double capital;
vector<Product> products;
public:
// 构造函数
Customer(string n, Gender g, string phone, string addr, int id)
: Person(n, g, phone, addr), customerId(id), capital(5000) {}
// 获取客户ID
int getCustomerId() const {
return customerId;
}
// 获取资本
double getCapital() const {
return capital;
}
// 获取购买的产品列表
vector<Product> getProducts() const {
return products;
}
// 添加产品到购买列表
void addProduct(const Product& product) {
products.push_back(product);
}
};
// 子类Manager
class Manager : public Person {
private:
int age;
string title;
public:
// 构造函数
Manager(string n, Gender g, string phone, string addr, int a, string t)
: Person(n, g, phone, addr), age(a), title(t) {}
// 获取年龄
int getAge() const {
return age;
}
// 获取职称
string getTitle() const {
return title;
}
};
// 产品类和Person类的定义
class MainProcess {
private:
vector<Product> products; // 所有产品的向量
vector<Customer> customers; // 所有客户的向量
Manager manager; // 管理经理
Customer currentCustomer; // 当前购买产品的客户
public:
// 构造函数
MainProcess(const Manager& mgr) : manager(mgr) {}
// 主菜单
void showMainMenu() {
cout << "********************" << endl;
cout << "*1: manager*" << endl;
cout << "*2: customer*" << endl;
cout << "*3: quit the program*" << endl;
cout << "********************" << endl;
cout << "Please input a number to set your role: ";
}
// 经理菜单
void showManagerMenu() {
cout << "********************" << endl;
cout << "*1: add products*" << endl;
cout << "*2: query products*" << endl;
cout << "*3: add customers*" << endl;
cout << "*4: query customers*" << endl;
cout << "*5: move to parent menu*" << endl;
cout << "********************" << endl;
cout << "Please input a number to run the program: ";
}
// 经理菜单操作:添加产品
void addProduct() {
int productId;
string productName;
int productQuantity;
double productPrice;
int choice;
do {
cout << "Product id: ";
cin >> productId;
// 检查产品ID是否已存在
auto it = find_if(products.begin(), products.end(), [&](const Product& p) {
return p.getProductId() == productId;
});
if (it != products.end()) {
cout << "Product ID already exists. Please enter a different ID." << endl;
continue;
}
cout << "Product name: ";
cin >> productName;
cout << "Product amount: ";
cin >> productQuantity;
cout << "Product price: ";
cin >> productPrice;
// 创建新产品并添加到产品向量中
products.push_back(Product(productId, productName, productQuantity, productPrice));
cout << "Add another product (0: no, 1: yes)? ";
cin >> choice;
} while (choice != 0);
showManagerMenu();
}
// 经理菜单操作:查询产品
void queryProducts() {
string productId;
cout << "Input a product Id (input \"*\" to list all products): ";
cin >> productId;
if (productId == "*") {
cout << "Product ID\tName\t\tAmount\tPrice" << endl;
for (const auto& product : products) {
cout << product.getProductId() << "\t\t" << product.getProductName() << "\t\t"
<< product.getProductQuantity() << "\t\t" << product.getProductPrice() << endl;
}
} else {
auto it = find_if(products.begin(), products.end(), [&](const Product& p) {
return to_string(p.getProductId()) == productId;
});
if (it != products.end()) {
cout << "Product ID\tName\t\tAmount\tPrice" << endl;
cout << it->getProductId() << "\t\t" << it->getProductName() << "\t\t"
<< it->getProductQuantity() << "\t\t" << it->getProductPrice() << endl;
} else {
cout << "No product found!" << endl;
}
}
showManagerMenu();
}
// 经理菜单操作:添加客户
void addCustomer() {
int customerId;
string customerName;
int customerGender;
string customerPhoneNumber;
string customerAddress;
int choice;
do {
cout << "Customer id: ";
cin >> customerId;
// 检查客户ID是否已存在
auto it = find_if(customers.begin(), customers.end(), [&](const Customer& c) {
return c.getCustomerId() == customerId;
});
if (it != customers.end()) {
cout << "Customer ID already exists. Please enter a different ID." << endl;
continue;
}
cout << "Customer name: ";
cin >> customerName;
cout << "Customer gender (1: male, 2: female): ";
cin >> customerGender;
cout << "Mobile number: ";
cin >> customerPhoneNumber;
cout << "Address: ";
cin.ignore();
getline(cin, customerAddress);
// 创建新客户并添加到客户向量中
customers.push_back(Customer(customerId, customerName, static_cast<Gender>(customerGender),
customerPhoneNumber, customerAddress));
cout << "Add another customer (0: no, 1: yes)? ";
cin >> choice;
} while (choice != 0);
showManagerMenu();
}
// 经理菜单操作:查询客户
void queryCustomers() {
string customerId;
cout << "Input a customer Id (input \"*\" to list all customers): ";
cin >> customerId;
if (customerId == "*") {
cout << "Customer ID\tName\t\tGender\tMobile number" << endl;
for (const auto& customer : customers) {
cout << customer.getCustomerId() << "\t\t" << customer.getCustomerName() << "\t\t"
<< static_cast<int>(customer.getCustomerGender()) << "\t\t" << customer.getCustomerPhoneNumber() << endl;
}
} else {
auto it = find_if(customers.begin(), customers.end(), [&](const Customer& c) {
return to_string(c.getCustomerId()) == customerId;
});
if (it != customers.end()) {
cout << "Customer ID\tName\t\tGender\tMobile number" << endl;
cout << it->getCustomerId() << "\t\t" << it->getCustomerName() << "\t\t"
<< static_cast<int>(it->getCustomerGender()) << "\t\t" << it->getCustomerPhoneNumber() << endl;
} else {
cout << "No customer found!" << endl;
}
}
showManagerMenu();
}
// 运行主进程
void run() {
int roleChoice;
int managerChoice;
do {
showMainMenu();
cin >> roleChoice;
switch (roleChoice) {
case 1: { // 经理操作
showManagerMenu();
cin >> managerChoice;
switch (managerChoice) {
case 1:
addProduct();
break;
case 2:
queryProducts();
break;
case 3:
addCustomer();
break;
case 4:
queryCustomers();
break;
case 5:
break;
default:
cout << "Invalid option. Please try again." << endl;
break;
}
} while (managerChoice != 5); // 根据实际情况进行调整
break;
int main() {
vector<Customer> customers;
vector<Product> products;
// 添加一些示例数据
customers.push_back(Customer(1111, "Mike", Gender::Male, "1234567890", "123 Main St"));
customers.push_back(Customer(2222, "Alice", Gender::Female, "0987654321", "456 Park Ave"));
products.push_back(Product(1000, "Milk", 20, 25.70));
products.push_back(Product(2000, "Pork", 10, 17.59));
int role;
int customerId;
while (true) {
cout << "********************" << endl;
cout << "*1: manager*" << endl;
cout << "*2: customer*" << endl;
cout << "*3: quit the program *" << endl;
cout << "********************" << endl;
cout << "Please input a number to set your role: ";
cin >> role;
if (role == 1) {
int managerChoice;
do {
showManagerMenu();
cin >> managerChoice;
switch (managerChoice) {
case 1:
addProduct();
break;
case 2:
queryProducts();
break;
case 3:
addCustomer();
break;
case 4:
queryCustomers();
break;
case 5:
break; // 如果需要添加更多选项,请根据实际情况进行调整
default:
cout << "Invalid option. Please try again." << endl;
break;
}
} while (managerChoice != 5); // 根据实际情况进行调整
}
else if (role == 2) {
// 客户操作
cout << "please input your customer Id: ";
cin >> customerId;
// 检查客户Id是否存在
bool customerExists = false;
Customer currentCustomer;
for (const auto& customer : customers) {
if (customer.getCustomerId() == customerId) {
customerExists = true;
currentCustomer = customer;
break;
}
}
if (customerExists) {
int option;
while (true) {
cout << "********************" << endl;
cout << "*1: add products into cart*" << endl;
cout << "*2: query products In cart*" << endl;
cout << "*3: check up *" << endl;
cout << "*4: move to parent menu *" << endl;
cout << "********************" << endl;
cout << "Please input a number to run the program: ";
cin >> option;
if (option == 1) {
// 添加产品到购物车
int productId, quantity;
cout << "Please input product Id: ";
cin >> productId;
cout << "Input the amount of the product you want to buy: ";
cin >> quantity;
// 检查产品是否存在和数量是否足够
bool productExists = false;
Product currentProduct;
for (const auto& product : products) {
if (product.getProductId() == productId) {
productExists = true;
currentProduct = product;
break;
}
}
if (productExists) {
if (currentCustomer.getCapital() >= quantity * currentProduct.getProductPrice() &&
quantity <= currentProduct.getProductQuantity()) {
currentCustomer.addProduct(productId);
currentProduct.decreaseQuantity(quantity);
currentCustomer.setCapital(currentCustomer.getCapital() - quantity * currentProduct.getProductPrice());
cout << "add product into cart successfully!" << endl;
} else {
cout << "Insufficient funds or quantity not available." << endl;
}
} else {
cout << "Product not found." << endl;
}
}
else if (option == 2) {
// 查询购物车中的产品
string productId;
cout << "Input a product Id for query (input \"*\" to list all products in your cart): ";
cin >> productId;
cout << "Product ID\tName\t\tAmount\tPrice" << endl;
if (productId == "*") {
// 列出购物车中的所有产品
for (const auto& product : currentCustomer.getCart()) {
const Product& currentProduct = getProductById(products, product.first);
cout << currentProduct.getProductId() << "\t\t"
<< currentProduct.getProductName() << "\t\t"
<< product.second << "\t\t"
<< currentProduct.getProductPrice() << endl;
}
} else {
// 查询指定产品
const Product& currentProduct = getProductById(products, stoi(productId));
if (currentProduct.getProductId() != -1) {
cout << currentProduct.getProductId() << "\t\t"
<< currentProduct.getProductName() << "\t\t"
<< currentCustomer.getProductQuantity(productId) << "\t\t"
<< currentProduct.getProductPrice() << endl;
} else {
cout << "Product not found." << endl;
}
}
}
else if (option == 3) {
// 结账
cout << "Product ID\tName\t\tAmount\tPrice" << endl;
for (const auto& product : currentCustomer.getCart()) {
const Product& currentProduct = getProductById(products, product.first);
cout << currentProduct.getProductId() << "\t\t"
<< currentProduct.getProductName() << "\t\t"
<< product.second << "\t\t"
<< currentProduct.getProductPrice() << endl;
}
cout << "Total price: " << currentCustomer.calculateTotalPrice(products) << endl;
cout << "Customer information:" << endl;
currentCustomer.displayInfo();
int confirm;
cout << "Please confirm to check up (0:no, 1:yes)? ";
cin >> confirm;
if (confirm == 1) {
currentCustomer.checkout(products);
cout << "Checked up successfully!" << endl;
}
}
else if (option == 4) {
// 返回主菜单
break;
}
else {
cout << "Invalid option. Please try again." << endl;
}
}
} else {
cout << "Customer not found." << endl;
}
}
else if (role == 3) {
// 退出程序
break;
}
else {
cout << "Invalid role. Please try again." << endl;
}
}
return 0;
}
但是该代码不能正常运行,本人使用的是JetBrains Clion IDE,报错如下:
想请教以下如何修改错误?修改后的代码是什么样的?如果代码运行后功能不全,还望能给出相关的代码。
在你代码基础上修改完善,纯人工编写,部分运行结果如下:
代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
// 产品类
class Product {
private:
int productId;
string productName;
int productQuantity;
double productPrice;
public:
// 构造函数
Product(int id, string name, int quantity, double price)
: productId(id), productName(name), productQuantity(quantity), productPrice(price) {}
// 获取产品ID
int getProductId() const {
return productId;
}
// 获取产品名称
string getProductName() const {
return productName;
}
// 获取产品数量
int getProductQuantity() const {
return productQuantity;
}
// 获取产品价格
double getProductPrice() const {
return productPrice;
}
// 减少特定数量的商品
void decreaseProduct(int n) {
productQuantity -= n;
}
};
// 性别枚举
enum Gender {
Male,
Female
};
// 基类Person
class Person {
private:
string name;
Gender gender;
string phoneNumber;
string address;
public:
// 构造函数
Person(string n, Gender g, string phone, string addr)
: name(n), gender(g), phoneNumber(phone), address(addr) {}
// 获取姓名
string getName() const {
return name;
}
// 获取性别
Gender getGender() const {
return gender;
}
// 获取手机号码
string getPhoneNumber() const {
return phoneNumber;
}
// 获取地址
string getAddress() const {
return address;
}
};
// 子类Customer
class Customer : public Person {
private:
int customerId;
double capital;
vector<Product> products;
public:
// 构造函数
Customer(int id = 0, string n = "", Gender g = Gender::Male, string phone = "", string addr = "")
: Person(n, g, phone, addr), customerId(id), capital(5000) {}
// 获取客户ID
int getCustomerId() const {
return customerId;
}
// 获取资本
double getCapital() const {
return capital;
}
// 获取购买的产品列表
vector<Product> getProducts() const {
return products;
}
// 添加产品到购买列表
void addProduct(const Product& product) {
products.push_back(product);
}
//支付
void pay(double p) {
capital -= p;
}
};
// 子类Manager
class Manager : public Person {
private:
int age;
string title;
public:
// 构造函数
Manager(string n, Gender g, string phone, string addr, int a, string t)
: Person(n, g, phone, addr), age(a), title(t) {}
// 获取年龄
int getAge() const {
return age;
}
// 获取职称
string getTitle() const {
return title;
}
};
// 产品类和Person类的定义
class MainProcess {
private:
vector<Product> products; // 所有产品的向量
vector<Customer> customers; // 所有客户的向量
Manager manager; // 管理经理
int currentCustomer; // 当前购买产品的客户在vector中的下标,默认-1
public:
// 构造函数
MainProcess(const Manager& mgr) : manager(mgr) { currentCustomer = -1; }
// 主菜单
void showMainMenu() {
cout << "********************" << endl;
cout << "*1: manager*" << endl;
cout << "*2: customer*" << endl;
cout << "*3: quit the program*" << endl;
cout << "********************" << endl;
cout << "Please input a number to set your role: ";
}
// 经理菜单
void showManagerMenu() {
cout << "********************" << endl;
cout << "*1: add products*" << endl;
cout << "*2: query products*" << endl;
cout << "*3: add customers*" << endl;
cout << "*4: query customers*" << endl;
cout << "*5: move to parent menu*" << endl;
cout << "********************" << endl;
cout << "Please input a number to run the program: ";
}
// 经理菜单操作:添加产品
void addProduct() {
int productId;
string productName;
int productQuantity;
double productPrice;
int choice;
char tmp[100] = { 0 };
do {
cout << "Product id: ";
cin >> productId;
// 检查产品ID是否已存在
auto it = find_if(products.begin(), products.end(), [&](const Product& p) {
return p.getProductId() == productId;
});
if (it != products.end()) {
cout << "Product ID already exists. Please enter a different ID." << endl;
continue;
}
getchar(); //吸收回车符
cout << "Product name: ";
cin.getline(tmp,99);
productName = tmp;
cout << "Product amount: ";
cin >> productQuantity;
cout << "Product price: ";
cin >> productPrice;
// 创建新产品并添加到产品向量中
products.push_back(Product(productId, productName, productQuantity, productPrice));
cout << "Add another product (0: no, 1: yes)? ";
cin >> choice;
} while (choice != 0);
showManagerMenu();
}
// 经理菜单操作:查询产品
void queryProducts() {
string productId;
cout << "Input a product Id (input \"*\" to list all products): ";
cin >> productId;
if (productId == "*") {
cout << "Product ID\tName\t\tAmount\tPrice" << endl;
for (const auto& product : products) {
cout << product.getProductId() << "\t\t" << product.getProductName() << "\t\t"
<< product.getProductQuantity() << "\t\t" << product.getProductPrice() << endl;
}
}
else {
auto it = find_if(products.begin(), products.end(), [&](const Product& p) {
return to_string(p.getProductId()) == productId;
});
if (it != products.end()) {
cout << "Product ID\tName\t\tAmount\tPrice" << endl;
cout << it->getProductId() << "\t\t" << it->getProductName() << "\t\t"
<< it->getProductQuantity() << "\t\t" << it->getProductPrice() << endl;
}
else {
cout << "No product found!" << endl;
}
}
showManagerMenu();
}
// 经理菜单操作:添加客户
void addCustomer() {
int customerId;
string customerName;
int customerGender;
string customerPhoneNumber;
string customerAddress;
int choice;
do {
cout << "Customer id: ";
cin >> customerId;
// 检查客户ID是否已存在
auto it = find_if(customers.begin(), customers.end(), [&](const Customer& c) {
return c.getCustomerId() == customerId;
});
if (it != customers.end()) {
cout << "Customer ID already exists. Please enter a different ID." << endl;
continue;
}
cout << "Customer name: ";
cin >> customerName;
cout << "Customer gender (1: male, 2: female): ";
cin >> customerGender;
cout << "Mobile number: ";
cin >> customerPhoneNumber;
cout << "Address: ";
cin.ignore();
getline(cin, customerAddress);
// 创建新客户并添加到客户向量中
customers.push_back(Customer(customerId, customerName, static_cast<Gender>(customerGender),
customerPhoneNumber, customerAddress));
cout << "Add another customer (0: no, 1: yes)? ";
cin >> choice;
} while (choice != 0);
showManagerMenu();
}
// 经理菜单操作:查询客户
void queryCustomers() {
string customerId;
cout << "Input a customer Id (input \"*\" to list all customers): ";
cin >> customerId;
if (customerId == "*") {
cout << "Customer ID\tName\t\tGender\tMobile number" << endl;
for (const auto& customer : customers) {
cout << customer.getCustomerId() << "\t\t" << customer.getName() << "\t\t"
<< static_cast<int>(customer.getGender()) << "\t\t" << customer.getPhoneNumber() << endl;
}
}
else {
auto it = find_if(customers.begin(), customers.end(), [&](const Customer& c) {
return to_string(c.getCustomerId()) == customerId;
});
if (it != customers.end()) {
cout << "Customer ID\tName\t\tGender\tMobile number" << endl;
cout << it->getCustomerId() << "\t\t" << it->getName() << "\t\t"
<< static_cast<int>(it->getGender()) << "\t\t" << it->getPhoneNumber() << endl;
}
else {
cout << "No customer found!" << endl;
}
}
showManagerMenu();
}
//获取当前客户
void getCurrentCustomer() {
int id;
currentCustomer = -1;
cout << "Please input your customer Id:";
cin >> id;
for (int i = 0; i < customers.size(); i++) {
if (customers.at(i).getCustomerId() == id) {
currentCustomer = i;
break;
}
}
if (currentCustomer == -1) {
cout << "Not find this customer!!" << endl;
}
}
//顾客菜单
void showCustomMenu()
{
cout << "********************" << endl;
cout << "*1: add products into cart*" << endl;
cout << "*2: query products In cart*" << endl;
cout << "*3: check up *" << endl;
cout << "*4: move to parent menu *" << endl;
cout << "********************" << endl;
cout << "Please input a number to run the program: ";
}
//添加商品到购物车
void addProduct2Cart()
{
// 添加产品到购物车
int productId, quantity;
int proIndex = 0;
cout << "Please input product Id: ";
//判断产品是否存在
int proExist = 0;
cin >> productId;
for (int i = 0; i < products.size(); i++) {
if (products.at(i).getProductId() == productId) {
proExist = 1;
proIndex = i;
break;
}
}
if (proExist)
{
cout << "Input the amount of the product you want to buy: ";
while (1) {
//判断商品的数量,库存数量不足则提示
cin >> quantity;
if (products.at(proIndex).getProductQuantity() < quantity)
cout << "Don't have so much product, input the amount of the product you want to buy:";
else
{
//判断用户的余额是否足够
if (customers.at(currentCustomer).getCapital() < products.at(proIndex).getProductPrice() * quantity)
cout << "Don't have so much money, input the amount of the product you want to buy: ";
else
break;
}
}
//将商品添加到购物车
Product pro(products.at(proIndex).getProductId(), products.at(proIndex).getProductName(), quantity, products.at(proIndex).getProductPrice());
customers.at(currentCustomer).addProduct(pro);
//从库存中减去已售数量
products.at(proIndex).decreaseProduct(quantity);
cout << "add product into cart successfully!" << endl;
}
else
cout << "Product not found.";
}
//查询购物车中的商品
void queryProductInCart() {
// 查询购物车中的产品
string productId;
cout << "Input a product Id for query (input \"*\" to list all products in your cart): ";
cin >> productId;
cout << "Product ID\tName\t\tAmount\tPrice" << endl;
if (productId == "*") {
// 列出购物车中的所有产品
vector<Product> allPro = customers.at(currentCustomer).getProducts();
for (int i = 0; i < allPro.size(); i++) {
const Product& currentProduct = allPro.at(i);
cout << currentProduct.getProductId() << "\t\t"
<< currentProduct.getProductName() << "\t\t"
<< currentProduct.getProductQuantity() << "\t\t"
<< currentProduct.getProductPrice() << endl;
}
}
else {
// 查询指定产品
int cnt = 0;
vector<Product> allPro = customers.at(currentCustomer).getProducts();
for (int i = 0; i < allPro.size(); i++) {
const Product& currentProduct = allPro.at(i);
if (to_string(currentProduct.getProductId()) == productId)
{
cout << currentProduct.getProductId() << "\t\t"
<< currentProduct.getProductName() << "\t\t"
<< currentProduct.getProductQuantity() << "\t\t"
<< currentProduct.getProductPrice() << endl;
cnt++;
}
}
if(cnt == 0) {
cout << "Product not found." << endl;
}
}
}
//支付
void checkUp() {
cout << "Product ID\tName\t\tAmount\tPrice" << endl;
double pay = 0;
// 列出购物车中的所有产品
vector<Product> allPro = customers.at(currentCustomer).getProducts();
for (int i = 0; i < allPro.size(); i++) {
const Product& currentProduct = allPro.at(i);
pay += currentProduct.getProductQuantity() * currentProduct.getProductPrice();
cout << currentProduct.getProductId() << "\t\t"
<< currentProduct.getProductName() << "\t\t"
<< currentProduct.getProductQuantity() << "\t\t"
<< currentProduct.getProductPrice() << endl;
}
cout << "Total price:" << fixed << setprecision(2) << pay << endl;
cout << "Customer information:" << endl;
cout << "Customer Id " << customers.at(currentCustomer).getCustomerId()
<< " customer name: " << customers.at(currentCustomer).getName()
<< " Capital: " << customers.at(currentCustomer).getCapital() << endl;
if (pay <= customers.at(currentCustomer).getCapital()) {
cout << "Please confirm to check up(0:no, 1:yes)?";
int op;
cin >> op;
if (op != 0) {
customers.at(currentCustomer).pay(pay); //从顾客账户扣除金额
cout << "Checked up successfully!" << endl;
}
}
else {
cout << "Don't have so much money!!" << endl;
}
}
// 运行主进程
void run() {
int roleChoice; //角色选择
int subChoice; //二级菜单选择
int flag = 1;
while (1) {
flag = 1;
showMainMenu();
cin >> roleChoice;
if (roleChoice == 1)
{
//经理
while (flag) {
showManagerMenu();
cin >> subChoice;
switch (subChoice) {
case 1:
addProduct(); break; //添加产品
case 2:
queryProducts(); break; //查询产品
case 3:
addCustomer(); break; //添加顾客
case 4:
queryCustomers(); break; //查询顾客
case 5:
flag = 0; break; //返回上一级
default:
break;
}
}
}
else if (roleChoice == 2)
{
//顾客
getCurrentCustomer();
if (currentCustomer >= 0)
{
while (flag) {
showCustomMenu();
cin >> subChoice;
switch (subChoice) {
case 1:
addProduct2Cart(); break; //添加商品到购物车
case 2:
queryProductInCart(); break; //查询购物车中的商品
case 3:
checkUp(); break; //支付
case 4:
flag = 0; break;
default:
break;
}
}
}
}else
cout << "Invalid role. Please try again." << endl;
}
}
};
int main() {
Manager mg("王经理",Gender::Male,"13355558888","北京市朝阳区光明大街2号",34,"总经理");
MainProcess mainPro(mg);
mainPro.run();
return 0;
}
代码量有点大,是自己写的吗?看情况编译还没通过。