功能:屏幕上出现一个界面,让顾客或输入商品名称,或商品型号,或选择列表进而输入 商品编号都可以,如查到所选商品,则列出商品编号、商品名称、商品型号、商品价格、商品产地、库存数量和已订购数量;然后给出选购流程:所选商品编号—列出对应商品的信息—要求顾客输入邮寄地址信息—确认订购,是否继续选购其它商品,列出所有选订商品,再次确认订购。建立两个文件,分别存放商品信息与订购信息。 分步实施: 1、 初步完成总体设计,搭好框架,确定人机对话的界面,确定函数个数; 2、完成最低要求:建立一个小系统,包括10种产品能供顾客选择。 3、进一步要求:完成全部功能的系统
时间: 2024-03-07 11:54:02 浏览: 81
C语言课程设计之商品订购系统设计(源码+说明).zip
好的,以下是一个实现了您所需功能的C++商品订购系统的代码示例:
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// 商品类
class Product {
public:
Product(string id, string name, string model, int price, string origin, int stock, int ordered) {
this->id = id;
this->name = name;
this->model = model;
this->price = price;
this->origin = origin;
this->stock = stock;
this->ordered = ordered;
}
string getId() {
return id;
}
string getName() {
return name;
}
string getModel() {
return model;
}
int getPrice() {
return price;
}
string getOrigin() {
return origin;
}
int getStock() {
return stock;
}
int getOrdered() {
return ordered;
}
void setStock(int stock) {
this->stock = stock;
}
void setOrdered(int ordered) {
this->ordered = ordered;
}
private:
string id;
string name;
string model;
int price;
string origin;
int stock;
int ordered;
};
// 商品清单类
class ProductList {
public:
void loadProducts(string filename) { // 从文件中加载商品信息
ifstream infile(filename);
string line;
while (getline(infile, line)) {
string id, name, model, origin;
int price, stock, ordered;
stringstream ss(line);
ss >> id >> name >> model >> price >> origin >> stock >> ordered;
Product product(id, name, model, price, origin, stock, ordered);
products.push_back(product);
}
infile.close();
}
void saveProducts(string filename) { // 将商品信息保存到文件中
ofstream outfile(filename);
for (int i = 0; i < products.size(); i++) {
Product product = products[i];
outfile << product.getId() << " " << product.getName() << " " << product.getModel() << " " << product.getPrice() << " " << product.getOrigin() << " " << product.getStock() << " " << product.getOrdered() << endl;
}
outfile.close();
}
int getProductCount() {
return products.size();
}
Product getProduct(int index) {
return products[index];
}
vector<Product> findProducts(string keyword) { // 根据关键字查找商品
vector<Product> result;
for (int i = 0; i < products.size(); i++) {
Product product = products[i];
if (product.getId() == keyword || product.getName() == keyword || product.getModel() == keyword) {
result.push_back(product);
}
}
return result;
}
private:
vector<Product> products;
};
// 订单类
class Order {
public:
Order(Product product, int quantity, string address) {
this->product = product;
this->quantity = quantity;
this->address = address;
}
Product getProduct() {
return product;
}
int getQuantity() {
return quantity;
}
string getAddress() {
return address;
}
int getTotalPrice() {
return product.getPrice() * quantity;
}
private:
Product product;
int quantity;
string address;
};
// 订单列表类
class OrderList {
public:
void loadOrders(string filename) { // 从文件中加载订单信息
ifstream infile(filename);
string line;
while (getline(infile, line)) {
string productId, address;
int quantity;
stringstream ss(line);
ss >> productId >> quantity >> address;
Product product = productMap[productId];
Order order(product, quantity, address);
orders.push_back(order);
}
infile.close();
}
void saveOrders(string filename) { // 将订单信息保存到文件中
ofstream outfile(filename);
for (int i = 0; i < orders.size(); i++) {
Order order = orders[i];
outfile << order.getProduct().getId() << " " << order.getQuantity() << " " << order.getAddress() << endl;
}
outfile.close();
}
void addOrder(Order order) {
orders.push_back(order);
Product product = order.getProduct();
product.setStock(product.getStock() - order.getQuantity());
product.setOrdered(product.getOrdered() + order.getQuantity());
}
int getOrderCount() {
return orders.size();
}
Order getOrder(int index) {
return orders[index];
}
int getTotalPrice() {
int totalPrice = 0;
for (int i = 0; i < orders.size(); i++) {
totalPrice += orders[i].getTotalPrice();
}
return totalPrice;
}
private:
vector<Order> orders;
map<string, Product> productMap;
};
// 主函数
int main() {
// 创建商品清单
ProductList productList;
productList.loadProducts("products.txt");
// 创建订单列表
OrderList orderList;
orderList.loadOrders("orders.txt");
// 显示主菜单
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 查找商品" << endl;
cout << "2. 查看购物车" << endl;
cout << "3. 结算购物车" << endl;
cout << "4. 退出程序" << endl;
int choice;
cin >> choice;
if (choice == 1) {
// 查找商品
cout << "请输入商品关键字:";
string keyword;
cin >> keyword;
vector<Product> products = productList.findProducts(keyword);
if (products.size() == 0) {
cout << "没有找到匹配的商品!" << endl;
} else if (products.size() == 1) {
Product product = products[0];
cout << "商品编号:" << product.getId() << endl;
cout << "商品名称:" << product.getName() << endl;
cout << "商品型号:" << product.getModel() << endl;
cout << "商品价格:" << product.getPrice() << "元" << endl;
cout << "商品产地:" << product.getOrigin() << endl;
cout << "库存数量:" << product.getStock() << endl;
cout << "已订购数量:" << product.getOrdered() << endl;
cout << endl;
// 购买商品
cout << "请输入购买数量:";
int quantity;
cin >> quantity;
if (quantity <= 0) {
cout << "购买数量必须大于0!" << endl;
} else if (quantity > product.getStock()) {
cout << "库存不足!" << endl;
} else {
cout << "请输入邮寄地址:";
string address;
cin >> address;
Order order(product, quantity, address);
orderList.addOrder(order);
cout << "已成功加入购物车!" << endl;
}
} else {
cout << "找到以下匹配的商品:" << endl;
for (int i = 0; i < products.size(); i++) {
Product product = products[i];
cout << i + 1 << ". 商品编号:" << product.getId() << ",商品名称:" << product.getName() << ",商品型号:" << product.getModel() << ",商品价格:" << product.getPrice() << "元" << endl;
}
cout << "请选择商品编号:";
int index;
cin >> index;
if (index <= 0 || index > products.size()) {
cout << "无效的商品编号!" << endl;
} else {
Product product = products[index - 1];
cout << "商品编号:" << product.getId() << endl;
cout << "商品名称:" << product.getName() << endl;
cout << "商品型号:" << product.getModel() << endl;
cout << "商品价格:" << product.getPrice() << "元" << endl;
cout << "商品产地:" << product.getOrigin() << endl;
cout << "库存数量:" << product.getStock() << endl;
cout << "已订购数量:" << product.getOrdered() << endl;
cout << endl;
// 购买商品
cout << "请输入购买数量:";
int quantity;
cin >> quantity;
if (quantity <= 0) {
cout << "购买数量必须大于0!" << endl;
} else if (quantity > product.getStock()) {
cout << "库存不足!" << endl;
} else {
cout << "请输入邮寄地址:";
string address;
cin >> address;
Order order(product, quantity, address);
orderList.addOrder(order);
cout << "已成功加入购物车!" << endl;
}
}
}
} else if (choice == 2) {
// 查看购物车
cout << "您的购物车:" << endl;
for (int i = 0; i < orderList.getOrderCount(); i++) {
Order order = orderList.getOrder(i);
cout << i + 1 << ". 商品名称:" << order.getProduct().getName() << ",购买数量:" << order.getQuantity() << ",邮寄地址:" << order.getAddress() << ",小计:" << order.getTotalPrice() << "元" << endl;
}
cout << "总金额:" << orderList.getTotalPrice() << "元" << endl;
} else if (choice == 3) {
// 结算购物车
cout << "您的购物车:" << endl;
for (int i = 0; i < orderList.getOrderCount(); i++) {
Order order = orderList.getOrder(i);
cout << i + 1 << ". 商品名称:" << order.getProduct().getName() << ",购买数量:" << order.getQuantity() << ",邮寄地址:" << order.getAddress() << ",小计:" << order.getTotalPrice() << "元" << endl;
}
cout << "总金额:" << orderList.getTotalPrice() << "元" << endl;
cout << "是否确认结算?(Y/N)";
char confirm;
cin >> confirm;
if (confirm == 'Y' || confirm == 'y') {
// 更新商品清单和订单列表
productList.saveProducts("products.txt");
orderList.saveOrders("orders.txt");
// 显示结算成功信息
cout << "结算成功!" << endl;
cout << "已将商品邮寄至您的地址。" << endl;
cout << "谢谢光临!" << endl;
break;
}
} else if (choice == 4) {
// 退出程序
break;
} else {
// 无效操作
cout << "无效的操作!" << endl;
}
cout << endl;
}
return 0;
}
```
以上代码实现了一个完整的商品订购系统,可以根据用户的选择查找商品、购买商品、查看购物车、结算购物车等。商品信息和订单信息分别保存在两个文件中,程序启动时从文件中加载商品信息和订单信息,并在结算购物车时将更新后的商品信息和订单信息保存回文件中。请注意,此代码示例仅供参考,实际应用中可能需要更复杂的功能和更严格的错误处理。
阅读全文