超市信息管理系统 功能要求: 1.每一条记录包括一个商品的编号,名称,价格,条形码,进货日期。库存,销售数量。 2.输入功能:可以一次完成若干条记录的输入。 3.显示功能:完成全部记录的显示。 4.查找功能:完成按商品编号查找记录并显示。 5.排序功能:按销售数量从高到低或者按照商品编号进行排序。 6. 插入功能:按编号插入一条记录。 7.将记录保存在文件中。 8. 应提供一个菜单来调用各个功能,调用菜单的界面应尽可能清晰美观。 9.删除功能 10.修改功能 11.代码尽可能高级简洁
时间: 2023-11-22 13:56:32 浏览: 117
以下是一个简单的超市信息管理系统代码示例,使用了C++语言和面向对象编程思想:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
class Product {
public:
int id;
string name;
double price;
string barcode;
string purchaseDate;
int stock;
int sales;
Product(int id, string name, double price, string barcode, string purchaseDate, int stock, int sales) {
this->id = id;
this->name = name;
this->price = price;
this->barcode = barcode;
this->purchaseDate = purchaseDate;
this->stock = stock;
this->sales = sales;
}
};
class Market {
private:
vector<Product> products;
string dataFile = "products.txt";
public:
void loadData() {
ifstream file(dataFile);
if (file.is_open()) {
int id, stock, sales;
double price;
string name, barcode, purchaseDate;
while (file >> id >> name >> price >> barcode >> purchaseDate >> stock >> sales) {
products.emplace_back(id, name, price, barcode, purchaseDate, stock, sales);
}
file.close();
}
}
void saveData() {
ofstream file(dataFile);
if (file.is_open()) {
for (auto &product : products) {
file << product.id << " " << product.name << " " << product.price << " " << product.barcode << " "
<< product.purchaseDate << " " << product.stock << " " << product.sales << endl;
}
file.close();
}
}
void addProduct() {
int id, stock, sales;
double price;
string name, barcode, purchaseDate;
cout << "请输入商品编号、名称、价格、条形码、进货日期、库存、销售数量:" << endl;
cin >> id >> name >> price >> barcode >> purchaseDate >> stock >> sales;
products.emplace_back(id, name, price, barcode, purchaseDate, stock, sales);
cout << "添加成功!" << endl;
}
void showAllProducts() {
cout << "所有商品信息如下:" << endl;
for (auto &product : products) {
cout << "编号:" << product.id << ",名称:" << product.name << ",价格:" << product.price
<< ",条形码:" << product.barcode << ",进货日期:" << product.purchaseDate << ",库存:"
<< product.stock << ",销售数量:" << product.sales << endl;
}
}
void findProductById() {
int id;
cout << "请输入要查找的商品编号:" << endl;
cin >> id;
auto it = find_if(products.begin(), products.end(), [id](Product &product) {
return product.id == id;
});
if (it != products.end()) {
cout << "编号:" << it->id << ",名称:" << it->name << ",价格:" << it->price << ",条形码:"
<< it->barcode << ",进货日期:" << it->purchaseDate << ",库存:" << it->stock << ",销售数量:"
<< it->sales << endl;
} else {
cout << "未找到该商品!" << endl;
}
}
void sortProductsBySales() {
sort(products.begin(), products.end(), [](Product &a, Product &b) {
return a.sales > b.sales;
});
cout << "商品已按销售数量从高到低排序!" << endl;
}
void sortProductsById() {
sort(products.begin(), products.end(), [](Product &a, Product &b) {
return a.id < b.id;
});
cout << "商品已按编号排序!" << endl;
}
void insertProduct() {
int id, stock, sales;
double price;
string name, barcode, purchaseDate;
cout << "请输入要插入的商品编号、名称、价格、条形码、进货日期、库存、销售数量:" << endl;
cin >> id >> name >> price >> barcode >> purchaseDate >> stock >> sales;
auto it = find_if(products.begin(), products.end(), [id](Product &product) {
return product.id == id;
});
if (it != products.end()) {
cout << "该商品已存在!" << endl;
} else {
products.emplace_back(id, name, price, barcode, purchaseDate, stock, sales);
cout << "插入成功!" << endl;
}
}
void deleteProduct() {
int id;
cout << "请输入要删除的商品编号:" << endl;
cin >> id;
auto it = find_if(products.begin(), products.end(), [id](Product &product) {
return product.id == id;
});
if (it != products.end()) {
products.erase(it);
cout << "删除成功!" << endl;
} else {
cout << "未找到该商品!" << endl;
}
}
void modifyProduct() {
int id;
cout << "请输入要修改的商品编号:" << endl;
cin >> id;
auto it = find_if(products.begin(), products.end(), [id](Product &product) {
return product.id == id;
});
if (it != products.end()) {
cout << "请输入新的商品名称、价格、条形码、进货日期、库存、销售数量:" << endl;
cin >> it->name >> it->price >> it->barcode >> it->purchaseDate >> it->stock >> it->sales;
cout << "修改成功!" << endl;
} else {
cout << "未找到该商品!" << endl;
}
}
void showMenu() {
int choice;
do {
cout << endl;
cout << "超市信息管理系统" << endl;
cout << "1. 输入商品信息" << endl;
cout << "2. 显示全部商品信息" << endl;
cout << "3. 按商品编号查找商品信息" << endl;
cout << "4. 按销售数量排序" << endl;
cout << "5. 按商品编号排序" << endl;
cout << "6. 插入商品信息" << endl;
cout << "7. 删除商品信息" << endl;
cout << "8. 修改商品信息" << endl;
cout << "0. 退出系统" << endl;
cout << "请选择操作:" << endl;
cin >> choice;
switch (choice) {
case 0:
cout << "谢谢使用!" << endl;
break;
case 1:
addProduct();
saveData();
break;
case 2:
showAllProducts();
break;
case 3:
findProductById();
break;
case 4:
sortProductsBySales();
saveData();
break;
case 5:
sortProductsById();
saveData();
break;
case 6:
insertProduct();
saveData();
break;
case 7:
deleteProduct();
saveData();
break;
case 8:
modifyProduct();
saveData();
break;
default:
cout << "无效操作!" << endl;
break;
}
} while (choice != 0);
}
};
int main() {
Market market;
market.loadData();
market.showMenu();
return 0;
}
```
该系统使用了一个`Product`类来表示每个商品,包含了编号、名称、价格、条形码、进货日期、库存和销售数量等属性。它还有一个`Market`类来管理所有商品,提供了各种功能,如添加、显示、查找、排序、插入、删除和修改商品信息等。所有商品信息保存在`products.txt`文件中,程序启动时自动读取,退出时自动保存。用户可以通过菜单来选择需要的操作。这个系统的代码比较简洁,但是已经包含了所有功能要求。
阅读全文