~cpp
#include <iostream>
#include <cstring>
using namespace std;
class Customer {
private:
int input;
public:
int mainMenuInput(){
cout << ">> ";
cin >> input;
if (input < 1 || input > 5){
cout << " 하.\n";
input = 0;
}
return input;
}
};
class SuperMarket{
private:
int money, cash, wantProduct, wantNum;
struct goods{
char name[10];
int price;
int num;
};
goods product[3];
public:
SuperMarket() {
money = 0;
strcpy(product[0].name, ""); product[0].price = 1000;
strcpy(product[1].name, ""); product[1].price = 1200;
strcpy(product[2].name, ""); product[2].price = 5000;
for (int i = 0 ; i < 3 ; i++)
product[i].num = 0;
}
void mainMenuView() {
cout << " n1. n2. n3. \n"
<< "4. 하 n5. \n";
}
void moneyView(){
cout << " : " << money << endl;
}
void cashMoney(){
cout << " 하? ";
cin >> cash;
money += cash;
}
void buyGoods(){
cout << " n";
for (int i = 0 ; i < 3 ; i++)
cout << i << ". " << product[i].name << " " << product[i].price << "\n";
cout << " >> ";
cin >> wantProduct;
cout << " 해 >> ";
cin >> wantNum;
if (money >= product[wantProduct].price * wantNum){
money -= product[wantProduct].price * wantNum;
product[wantProduct].num += wantNum;
cout << product[wantProduct].name << " " << wantNum << " .\n";
}
else
cout << " 합.\n";
}
void showBoughtGoods(){
cout << " \n";
for (int i = 0 ; i < 3 ; i++)
if (product[i].num > 0)
cout << i << ". " << product[i].name << " " << product[i].num << "\n";
}
void cancelToBuyProduct(){
cout << "하 호 택해 >> ";
cin >> wantProduct;
cout << "하 해 >> ";
cin >> wantNum;
if (product[wantProduct].num >= wantNum){
money += product[wantProduct].price * wantNum;
product[wantProduct].num -= wantNum;
cout << product[wantProduct].name << " " << wantNum << " 하.\n";
}
else
cout << "한 .\n";
}
};
int main(){
Customer customer;
SuperMarket supermarket;
int input;
while(input != 5)
{
supermarket.mainMenuView();
supermarket.moneyView();
input = customer.mainMenuInput();
if (input == 1)
supermarket.cashMoney();
else if (input == 2)
supermarket.buyGoods();
else if (input == 3)
supermarket.showBoughtGoods();
else if (input == 4){
supermarket.showBoughtGoods();
supermarket.cancelToBuyProduct();
}
else if (input == 5)
break;
cout << endl;
system("pause");
system("cls");
}
return 0;
}