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