~cpp
#include<iostream>
using namespace std;
struct product {
char * name;
int cost;
int quanty;
};
class Supermarket {
private:
int _money;
int _max_num;
product _product[3];
int getMyMoney() { return _money; }
public:
Supermarket();
void deposit();
void buy();
void inventory();
void cancle();
void showMainMenu();
};
Supermarket::Supermarket() {
_money = 0;
_max_num = 3;
_product[0].name = "캔디";
_product[0].cost = 1000;
_product[1].name = "디스켓";
_product[1].cost = 1200;
_product[2].name = "마우스";
_product[2].cost = 5000;
for(int i=0;i<_max_num;i++)
_product[i].quanty = 0;
}
//돈을 입금
void Supermarket::deposit() {
int tempMoney;
cout << "돈을 입금하세요 : ";
cin >> tempMoney;
if(tempMoney > 0)
_money += tempMoney;
else
cout << "잘못된 돈 입력입니다\n";
}
//물건을 산다
void Supermarket::buy() {
int choice;
int quanty;
for(int i = 0 ; i < _max_num ; i++)
cout << i + 1 << ". " << _product[i].name << "\t" << _product[i].cost << "\n";
cout << "사실 물건 선택 : ";
cin >> choice;
if(choice>0 && choice<4) {
cout << "수량 입력: ";
cin >> quanty;
if((_money - (_product[choice-1].cost * quanty)) >= 0) {
_product[choice-1].quanty += quanty;
_money = _money - (_product[choice-1].cost * quanty);
}
else
cout << "돈이 부족합니다\n";
}
else
cout << "사실 물건을 잘못 입력하셨습니다\n";
}
//산 물건을 보여준다
void Supermarket::inventory() {
for(int i = 0 ; i < _max_num ; i++)
cout << _product[i].name << "\t" << _product[i].quanty << "\n";
}
//산 물건을 취소한다
void Supermarket::cancle() {
int choice;
int quanty;
inventory();
cout << "취소할 물건 선택 : ";
cin >> choice;
if(choice>0 && choice<4) {
cout << "수량 입력: ";
cin >> quanty;
if((_product[choice-1].quanty - quanty) >= 0 ) {
_product[choice-1].quanty -= quanty;
_money += _product[choice-1].cost * quanty;
}
else
cout << "잘못된 수량 입력입니다\n";
}
else
cout << "취소할 물건을 잘못 입력하셨습니다\n";
}
void Supermarket::showMainMenu() {
cout << "메인메뉴\n";
cout << "1. 돈 예금\n";
cout << "2. 물건 사기\n";
cout << "3. 산 물건 목록 보여주기\n";
cout << "4. 산 물건 취소하기\n";
cout << "5. 가게 나기기\n";
cout << "남은 돈: " << getMyMoney() << "\n";
cout << "메뉴선택 : ";
}
int main() {
int choice;
Supermarket market;
market.showMainMenu();
cin >> choice;
while(choice != 5) {
switch(choice) {
case 1:
market.deposit();
break;
case 2:
market.buy();
break;
case 3:
market.inventory();
break;
case 4:
market.cancle();
break;
default:
cout << "메뉴를 잘못 입력하셨습니다\n";
}
market.showMainMenu();
cin >> choice;
}
return 0;
}