~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;
}