我同学让我做一个商店
于是:
#include<bits/stdc++.h>
using namespace std;
class Product {
public:
string name;
double price;
int stock;
Product(string n, double p, int s) : name(n), price(p), stock(s) {}
};
void showProducts(const vector<Product>& products) {
cout << "系统:可购买商品列表:" << endl;
for (int i = 0; i < products.size(); ++i) {
cout << i + 1 << ". " << products[i].name << " - 价格: $" << products[i].price << " - 库存: " << products[i].stock << endl;
}
}
void purchaseProducts(vector<Product>& products) {
int choice;
int quantity;
showProducts(products);
cout << "系统:请输入想要购买的商品编号(输入0退出购买):";
cin >> choice;
while (choice > 0 && choice <= products.size()) {
Product& product = products[choice - 1];
cout << "系统:请输入购买数量:";
cin >> quantity;
if (quantity > product.stock) {
cout << "系统:库存不足,无法购买该数量。" << endl;
} else {
product.stock -= quantity;
cout << "系统:您已购买 " << quantity << " 个 " << product.name << ",总价: $" << product.price * quantity << endl;
}
cout << "系统:是否继续购买?(1: 是, 0: 否)";
cin >> choice;
}
}
int main(){
char a;
bool havesentence=0;
vector<Product> products = {
Product("switch", 2000, 100),
Product("卡带", 210, 150),
Product("5090显卡", 1290, 200),
Product("高配电脑",15000,90),
Product("Silk Song",1000,1),
Product("马悦的电脑",1000,1),
Product("马悦的U盘",60,1),
Product("苹果",10,1000),
Product("香蕉",10,1000),
Product("橘子",10,1000),
};
string sentence1="空洞骑士";
string sentence2="丝之歌";
string sentence3="丝之鸽";
string sentence4="樱桃";
cout<<"店主:欢迎来到我的超市!O(∩_∩)O"<<endl;
while(1){
cout<<"系统:聊天按a,买东西按e,退出按b。(温馨提示,店主是斯莱)"<<endl;
cin>>a;
cin.ignore();
if(a=='a'){
cout<<"你说:";
string yousay;
getline(cin,yousay);
size_t found1 = yousay.find(sentence1);
size_t found2 = yousay.find(sentence2);
size_t found3 = yousay.find(sentence3);
size_t found4 = yousay.find(sentence4);
if (found1 != string::npos) {
havesentence=1;
}else if (found2 != string::npos) {
havesentence=1;
}else if (found3 != string::npos) {
havesentence=1;
}else if (found4 != string::npos) {
havesentence=1;
}
if(havesentence==1){
cout<<"店主:我恨樱桃,我要给他吃这个!!!!"<<endl;
cout<<" ()"<<endl;
cout<<" (__)"<<endl;
cout<<" (____)"<<endl;
cout<<" (______)"<<endl;
cout<<" (________)"<<endl;
cout<<" (__________)"<<endl;
cout<<" (____________)"<<endl;
cout<<"(______________)"<<endl;
cout<<"店主:***********************"<<endl;
}else{
cout<<"店主:哦~哦~哦~"<<endl;
}
}else if(a=='e'){
purchaseProducts(products);
}else if(a=='b'){
cout<<"你:我走了!"<<endl;
cout<<"店主:再见\( ̄︶ ̄*\ )"<<endl;
}else{
cout<<"店主:???"<<endl;
}
}
return 0;
}//<<fixed<<setprecision(2)
// <<"店主:"<<;"系统:"
智慧的结晶
哪位dalao给我点建议??