No older revisions available
No older revisions available
소스 ¶
~cpp
#include<iostream.h>
class supermarket
{
private:
int money;
int max_num;
struct product{
char * name;
int cost;
int quanty;
};
product s_product[3];
public:
supermarket();
void Deposit();
void Buy();
void Inventory();
void Cancle();
void Mymoney();
};
supermarket::supermarket()
{
money = 0;
max_num = 3;
s_product[0].name = "캔디";
s_product[0].cost = 1000;
s_product[1].name = "디스켓";
s_product[1].cost = 1200;
s_product[2].name = "마우스";
s_product[2].cost = 5000;
s_product[0].quanty = s_product[1].quanty = s_product[2].quanty = 0;
}
//돈을 입금
void supermarket::Deposit()
{
int temp;
cout << "돈을 입금하게요 : ";
cin >> temp;
money = money + temp;
}
//물건을 산다
void supermarket::Buy()
{
int choice;
int quanty;
for(int i = 0 ; i < max_num ; i++)
cout << i + 1 << ". " << s_product[i].name << "\t" << s_product[i].cost << "\n";
cout << "사실 물건 선택 : ";
cin >> choice;
cout << "수량 입력: ";
cin >> quanty;
if(choice == 1 && (money - (s_product[0].cost * quanty)) >= 0 )
{
s_product[0].quanty += quanty;
money = money - (s_product[0].cost * quanty);
}
else if(choice == 2 && (money - (s_product[1].cost * quanty)) >= 0)
{
s_product[1].quanty += quanty;
money = money - (s_product[1].cost * quanty);
}
else if(choice == 3 && (money - (s_product[2].cost * quanty)) >= 0)
{
s_product[2].quanty += quanty;
money = money - (s_product[2].cost * quanty);
}
else
cout << "돈이 부족합니다\n";
}
//산 물건을 보여준다
void supermarket::Inventory()
{
for(int i = 0 ; i < max_num ; i++)
cout << s_product[i].name << "\t" << s_product[i].quanty << "\n";
}
//산 물건을 취소한다
void supermarket::Cancle()
{
int choice;
int quanty;
cout << "취소할 물건 선택 : ";
cin >> choice;
cout << "수량 입력: ";
cin >> quanty;
if(choice == 1 && (s_product[0].quanty - quanty) >= 0 )
{
s_product[0].quanty = s_product[0].quanty - quanty;
money = money + (s_product[0].cost * quanty);
}
else if(choice == 2 && (s_product[1].quanty - quanty) >= 0 )
{
s_product[1].quanty = s_product[1].quanty - quanty;
money = money + (s_product[1].cost * quanty);
}
else if(choice == 3 && (s_product[2].quanty - quanty) >= 0 )
{
s_product[2].quanty = s_product[2].quanty - quanty;
money = money + (s_product[2].cost * quanty);
}
else
cout << "잘못된 수량입력입니다\n";
}
//남은 돈을 보여준다
void supermarket::Mymoney()
{
cout << "남은 돈: " << money << "\n";
}
int main()
{
int choice = 0;
supermarket market;
cout << "메인메뉴\n";
cout << "1. 돈 예금\n";
cout << "2. 물건 사기\n";
cout << "3. 산 물건 목록 보여주기\n";
cout << "4. 산 물건 취소하기\n";
cout << "5. 가게 나기기\n";
market.Mymoney();
cout << "메뉴선택 : ";
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;
}
cout << "\n\n메인메뉴\n";
cout << "1. 돈 예금\n";
cout << "2. 물건 사기\n";
cout << "3. 산 물건 목록 보여주기\n";
cout << "4. 산 물건 취소하기\n";
cout << "5. 가게 나기기\n";
market.Mymoney();
cout << "메뉴선택 : ";
cin >> choice;
}
return 0;
}