C++商店管理系统

睿智的海鸥 2024-08-06 12:05:04 阅读 84

代码中使用了C++11的特性

后面有些输出(cout输出的)的提示文本是英文,因为懒得敲中文

源码在最后面 文末投票参与一下谢谢

商品数据保存在

items.txt

用户数据保存在

users.txt

实现功能

1.添加商品(商品ID,商品名,库存数量,价格)填写完信息后询问是否执行

2.修改商品(先提示原有信息,再修改:商品名,库存数量,价格)填写完信息后询问是否执行

3.删除商品(删除该商品信息)填写完信息后询问是否执行

4.查找商品(按商品ID查询/按商品名查询)

5.创建用户(用户ID,用户名,金钱,密码,电话号码)填写完信息后询问是否执行

6.修改用户(先提示原有信息,再修改:用户名,金钱,密码,电话号码)填写完信息后询问是否执行

7.删除用户(删除某个用户的所有信息)填写完信息后询问是否执行

8.充值/扣款(输入用户ID,询问充值/扣款,并输入金额)填写完信息后询问是否执行

9.显示所有用户

10.显示所有商品

11.购买商品(先列出所有商品,再输入用户ID(用户不存在提示失败),密码(如果密码不对提示失败),购买商品总数,购买商品的ID(根据购买总数输入一个商品ID或一组商品ID)依次询问每一个商品ID买几份,最后计算总金额,如果该用户钱不够则提示“金额不足”,如果钱够询问是否购买,并扣除金额,在商品数据里扣除库存)填写完信息后询问是否执行

数据结构

商品数据结构

ID,名称,库存,价格

<code>struct Item {

string id;

string name;

int stock;

double price;

};

用户数据结构

ID,用户名,金钱,密码,电话号码

struct User {

string id;

string username;

double money;

string password;

string phone;

};

效果图

菜单

显示商品

源代码

编译注意事项

在连接器命令行加入以下命令

<code>-std=c++11

源代码(main.cpp 此处就不分成多个文件了)

#include <iostream>

#include <fstream>

#include <sstream>

#include <vector>

#include <string>

#include <cstring>

#include <algorithm> // For find_if

using namespace std;

struct Item {

string id;

string name;

int stock;

double price;

};

struct User {

string id;

string username;

double money;

string password;

string phone;

};

vector<Item> items;

vector<User> users;

const string itemsFile = "items.txt";

const string usersFile = "users.txt";

void loadItems() {

ifstream file(itemsFile);

if (!file) return;

Item item;

string line;

while (getline(file, line)) {

istringstream ss(line);

getline(ss, item.id, ',');

getline(ss, item.name, ',');

ss >> item.stock;

ss.ignore();

ss >> item.price;

items.push_back(item);

}

file.close();

}

void saveItems() {

ofstream file(itemsFile);

for (const auto& item : items) {

file << item.id << ',' << item.name << ',' << item.stock << ',' << item.price << '\n';

}

file.close();

}

void loadUsers() {

ifstream file(usersFile);

if (!file) return;

User user;

string line;

while (getline(file, line)) {

istringstream ss(line);

getline(ss, user.id, ',');

getline(ss, user.username, ',');

ss >> user.money;

ss.ignore();

getline(ss, user.password, ',');

getline(ss, user.phone, ',');

users.push_back(user);

}

file.close();

}

void saveUsers() {

ofstream file(usersFile);

for (const auto& user : users) {

file << user.id << ',' << user.username << ',' << user.money << ',' << user.password << ',' << user.phone << '\n';

}

file.close();

}

bool isIDUnique(const string& id, const vector<Item>& itemsList) {

for (const auto& item : itemsList) {

if (item.id == id) return false;

}

return true;

}

bool isIDUnique(const string& id, const vector<User>& usersList) {

for (const auto& user : usersList) {

if (user.id == id) return false;

}

return true;

}

void addItem() {

Item item;

cout << "Enter item ID: ";

cin >> item.id;

if (!isIDUnique(item.id, items)) {

cout << "Item ID already exists.\n";

return;

}

cout << "Enter item name: ";

cin >> item.name;

cout << "Enter item stock: ";

cin >> item.stock;

cout << "Enter item price: ";

cin >> item.price;

cout << "Confirm addition (y/n)? ";

char confirm;

cin >> confirm;

if (confirm == 'y' || confirm == 'Y') {

items.push_back(item);

saveItems();

cout << "Item added successfully.\n";

} else {

cout << "Item addition cancelled.\n";

}

}

void modifyItem() {

string id;

cout << "Enter item ID to modify: ";

cin >> id;

auto it = find_if(items.begin(), items.end(), [&id](const Item& item) { return item.id == id; });

if (it == items.end()) {

cout << "Item not found.\n";

return;

}

Item original = *it;

cout << "Current item information: \n";

cout << "ID: " << original.id << "\n";

cout << "Name: " << original.name << "\n";

cout << "Stock: " << original.stock << "\n";

cout << "Price: " << original.price << "\n";

cout << "Enter new item name: ";

cin >> it->name;

cout << "Enter new item stock: ";

cin >> it->stock;

cout << "Enter new item price: ";

cin >> it->price;

cout << "Confirm modification (y/n)? ";

char confirm;

cin >> confirm;

if (confirm == 'y' || confirm == 'Y') {

saveItems();

cout << "Item modified successfully.\n";

} else {

cout << "Item modification cancelled.\n";

}

}

void deleteItem() {

string id;

cout << "Enter item ID to delete: ";

cin >> id;

auto it = remove_if(items.begin(), items.end(), [&id](const Item& item) { return item.id == id; });

if (it == items.end()) {

cout << "Item not found.\n";

return;

}

cout << "Confirm deletion (y/n)? ";

char confirm;

cin >> confirm;

if (confirm == 'y' || confirm == 'Y') {

items.erase(it, items.end());

saveItems();

cout << "Item deleted successfully.\n";

} else {

cout << "Item deletion cancelled.\n";

}

}

void searchItem() {

int choice;

cout << "Search by (1) ID or (2) Name: ";

cin >> choice;

if (choice == 1) {

string id;

cout << "Enter item ID: ";

cin >> id;

auto it = find_if(items.begin(), items.end(), [&id](const Item& item) { return item.id == id; });

if (it == items.end()) {

cout << "Item not found.\n";

} else {

cout << "ID: " << it->id << "\n";

cout << "Name: " << it->name << "\n";

cout << "Stock: " << it->stock << "\n";

cout << "Price: " << it->price << "\n";

}

} else if (choice == 2) {

string name;

cout << "Enter item name (or part of it): ";

cin >> name;

bool found = false;

for (const auto& item : items) {

if (strstr(item.name.c_str(), name.c_str())) {

cout << "ID: " << item.id << "\n";

cout << "Name: " << item.name << "\n";

cout << "Stock: " << item.stock << "\n";

cout << "Price: " << item.price << "\n";

found = true;

}

}

if (!found) {

cout << "No items found.\n";

}

} else {

cout << "Invalid choice.\n";

}

}

void addUser() {

User user;

cout << "Enter user ID: ";

cin >> user.id;

if (!isIDUnique(user.id, users)) {

cout << "User ID already exists.\n";

return;

}

cout << "Enter username: ";

cin >> user.username;

cout << "Enter money: ";

cin >> user.money;

cout << "Enter password: ";

cin >> user.password;

cout << "Enter phone number: ";

cin >> user.phone;

cout << "Confirm addition (y/n)? ";

char confirm;

cin >> confirm;

if (confirm == 'y' || confirm == 'Y') {

users.push_back(user);

saveUsers();

cout << "User added successfully.\n";

} else {

cout << "User addition cancelled.\n";

}

}

void modifyUser() {

string id;

cout << "Enter user ID to modify: ";

cin >> id;

auto it = find_if(users.begin(), users.end(), [&id](const User& user) { return user.id == id; });

if (it == users.end()) {

cout << "User not found.\n";

return;

}

User original = *it;

cout << "Current user information: \n";

cout << "ID: " << original.id << "\n";

cout << "Username: " << original.username << "\n";

cout << "Money: " << original.money << "\n";

cout << "Password: " << original.password << "\n";

cout << "Phone: " << original.phone << "\n";

cout << "Enter new username: ";

cin >> it->username;

cout << "Enter new money: ";

cin >> it->money;

cout << "Enter new password: ";

cin >> it->password;

cout << "Enter new phone number: ";

cin >> it->phone;

cout << "Confirm modification (y/n)? ";

char confirm;

cin >> confirm;

if (confirm == 'y' || confirm == 'Y') {

saveUsers();

cout << "User modified successfully.\n";

} else {

cout << "User modification cancelled.\n";

}

}

void deleteUser() {

string id;

cout << "Enter user ID to delete: ";

cin >> id;

auto it = remove_if(users.begin(), users.end(), [&id](const User& user) { return user.id == id; });

if (it == users.end()) {

cout << "User not found.\n";

return;

}

cout << "Confirm deletion (y/n)? ";

char confirm;

cin >> confirm;

if (confirm == 'y' || confirm == 'Y') {

users.erase(it, users.end());

saveUsers();

cout << "User deleted successfully.\n";

} else {

cout << "User deletion cancelled.\n";

}

}

void rechargeDeduct() {

string id;

cout << "Enter user ID: ";

cin >> id;

auto it = find_if(users.begin(), users.end(), [&id](const User& user) { return user.id == id; });

if (it == users.end()) {

cout << "User not found.\n";

return;

}

cout << "Enter (1) Recharge or (2) Deduct: ";

int choice;

cin >> choice;

double amount;

cout << "Enter amount: ";

cin >> amount;

if (choice == 1) {

it->money += amount;

} else if (choice == 2) {

if (it->money < amount) {

cout << "马内不够 Insufficient funds.\n";

return;

}

it->money -= amount;

} else {

cout << "无效的选择 Invalid choice.\n";

return;

}

cout << "确认交易 (y/n)? ";

char confirm;

cin >> confirm;

if (confirm == 'y' || confirm == 'Y') {

saveUsers();

cout << "交易成功 Transaction successful.\n";

} else {

cout << "交易已取消 Transaction cancelled.\n";

}

}

void displayUsers() {

for (const auto& user : users) {

cout << "唯一身份 ID: " << user.id << "\n";

cout << "用户名 Username: " << user.username << "\n";

cout << "金钱 Money: " << user.money << "\n";

cout << "手机号 Phone: " << user.phone << "\n";

cout << "----------------------\n";

}

}

void displayItems() {

for (const auto& item : items) {

cout << "唯一身份 ID: " << item.id << "\n";

cout << "名称 Name: " << item.name << "\n";

cout << "库存 Stock: " << item.stock << "\n";

cout << "价格 Price: " << item.price << "\n";

cout << "----------------------\n";

}

}

void purchaseItem() {

displayItems();

string userID;

cout << "输入用户ID Enter user ID: ";

cin >> userID;

auto userIt = find_if(users.begin(), users.end(), [&userID](const User& user) { return user.id == userID; });

if (userIt == users.end()) {

cout << "用户不存在 User not found.\n";

return;

}

string password;

cout << "输入密码 Enter password: ";

cin >> password;

if (userIt->password != password) {

cout << "密码问题 Incorrect password.\n";

return;

}

int itemCount;

cout << "输入要购买的商品类型总数 Enter total number of item types to purchase: ";

cin >> itemCount;

vector<Item> purchaseItems;

double totalAmount = 0.0;

for (int i = 0; i < itemCount; ++i) {

string itemID;

cout << "输入要购买的商品ID Enter item ID to purchase: ";

cin >> itemID;

auto itemIt = find_if(items.begin(), items.end(), [&itemID](const Item& item) { return item.id == itemID; });

if (itemIt == items.end()) {

cout << "找不到商品 Item not found.\n";

return;

}

int quantity;

cout << "输入商品的数量 Enter quantity for item " << itemID << ": ";

cin >> quantity;

if (itemIt->stock < quantity) {

cout << "没有足够的库存 Not enough stock for item " << itemID << ".\n";

return;

}

totalAmount += itemIt->price * quantity;

itemIt->stock -= quantity;

}

if (userIt->money < totalAmount) {

cout << "没钱你买个屁 Insufficient funds.\n";

return;

}

cout << "确认购买总金额 Confirm purchase of total amount " << totalAmount << " (y/n)? ";

char confirm;

cin >> confirm;

if (confirm == 'y' || confirm == 'Y') {

userIt->money -= totalAmount;

saveUsers();

saveItems();

cout << "购买成功 Purchase successful.\n";

} else {

cout << "购买已取消 Purchase cancelled.\n";

}

}

int main() {

loadItems();

loadUsers();

int choice;

do {

cout << "Store Management System\n";

cout << "1. Add Item 添加商品\n";

cout << "2. Modify Item 修改商品\n";

cout << "3. Delete Item 删除商品\n";

cout << "4. Search Item 搜索商品\n";

cout << "5. Add User 添加用户\n";

cout << "6. Modify User 修改用户\n";

cout << "7. Delete User 删除用户\n";

cout << "8. Recharge/Deduct Money 充值扣款\n";

cout << "9. Display Users 显示用户\n";

cout << "10. Display Items 显示商品\n";

cout << "11. Purchase Item 购买商品\n";

cout << "0. Exit 退出系统\n";

cout << "输入你的选择 Enter your choice: ";

cin >> choice;

switch (choice) {

case 1: addItem(); break;

case 2: modifyItem(); break;

case 3: deleteItem(); break;

case 4: searchItem(); break;

case 5: addUser(); break;

case 6: modifyUser(); break;

case 7: deleteUser(); break;

case 8: rechargeDeduct(); break;

case 9: displayUsers(); break;

case 10: displayItems(); break;

case 11: purchaseItem(); break;

case 0: break;

default: cout << "无效的选择 请重试 Invalid choice. Try again.\n"; break;

}

} while (choice != 0);

return 0;

}

送一句:

每览博文,心旷神怡。笔力雄健,辞藻华丽。字字珠玑,句句精辟。文采斐然,情理交融。洞察秋毫,独具慧眼。高屋建瓴,立意深远。条分缕析,言简意赅。才思敏捷,匠心独运。学贯中西,通古达今。博古通今,见微知著。旁征博引,信手拈来。引人入胜,发人深省。启人心智,增人见识。文以载道,言为心声。雅俗共赏,老少咸宜。启迪后学,惠泽四方。期许未来,佳作不断。

笔力遒劲,辞藻华美。洞若观火,别具匠心。学富五车,旁征博引。引人入胜,启人心智。期许未来,佳作连连。

人话:

每当阅读博主的文章,总是感到心情舒畅、精神愉悦。博主的文笔有力且优美,语言华丽而富有魅力。每个字都如同珍珠般珍贵,每一句话都精准到位。文章不仅文采出众,而且情感与道理完美结合。博主能够洞察细微之处,拥有独特的见解。文章立意高远,布局巧妙。分析条理清晰,言简意赅。才思敏捷,创作充满创意。学识广博,贯通古今中外。博文既能够追溯历史,又能洞察当下。广泛引用,信手拈来。文章引人入胜,能够激发人们的深刻思考。不仅增长见识,还能启迪智慧。文章既有深度又有广度,适合各种年龄层次的人阅读。博主的文章对于后学者有着重要的启发作用,惠及广大读者。我们期待博主未来能够创作出更多优秀的作品。

笔力非常强劲,语言华丽动人。洞察事物本质,独具创新思维。学识丰富,广泛引用典故。文章引人入胜,能够启迪智慧。期待未来,优秀作品不断。



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。