~cpp void printMenu () { cout << "\n자판기 입니다\n"; cout << "1.돈을 넣는다\n"; cout << "2.물건을 산다\n"; cout << "3.돈을 거술러 받는다\n"; cout << "4.음료수를 채운다\n"; cout << "0.종료한다\n"; cout << "메뉴를 선택하세요 : "; }
~cpp bool isEndMenu (int choice) { return choice != 0; }
~cpp while(isEndMenu (choice)) { printMenu (); cin >> choice; . .
~cpp enum { MENU_END = 0, };
~cpp bool isEndMenu (int choice) { return choice != MENU_END; }
~cpp enum { MENU_END = 0, MENU_GET_MONEY, MENU_BUY, MENU_TAKEBACK_MONEY, MENU_INSERT_DRINK };
~cpp switch(choice) { case MENU_GET_MONEY: VendingMachine.GetMoney(); break; case MENU_BUY: VendingMachine.Buy(); break; case MENU_TAKEBACK_MONEY: VendingMachine.TakeBackMoney(); break; case MENU_INSERT_DRINK: VendingMachine.InsertDrink(); break; case MENU_END: cout << "자판기를 종료합니다!!\n\n"; break; }
~cpp bool isValidMenu (int choice) { return choice >= 0 && choice <= 4; }
~cpp bool isValidMenu (int choice) { return choice >= MENU_END && choice <= MENU_INSERT_DRINK; }근데.. 좀 명확해 보이진 않는군요. enum 에서 설정된 것을 재정의해주면
~cpp enum { MENU_ENDCODE = 0, MENU_START = 0, MENU_GET_MONEY, MENU_BUY, MENU_TAKEBACK_MONEY, MENU_INSERT_DRINK, MENU_END = MENU_INSERT_DRINK };MENU_END 뜻이 달라졌으므로, 앞에서 MENU_END를 썼었던 다른 것들도 고칩니다.
~cpp case MENU_ENDCODE: cout << "자판기를 종료합니다!!\n\n"; break; }
~cpp bool isEndMenu (int choice) { return choice != MENU_ENDCODE; }그러면,
~cpp isValidMenu
를 다음과 같이 고칠 수 있습니다. (써놓고 보니 그리 맘에 들진 않지만, 일단 이정도만 해두겠습니다. -_-;)~cpp bool isValidMenu (int choice) { return choice >= MENU_START && choice <= MENU_END; }
~cpp case MENU_ENDCODE: VendingMachine.EndMachine(); break; } } else VendingMachine.PrintErrorMessage (); }
~cpp const int DRINKNAME_MAXLENGTH = 255; const int TOTAL_DRINK_TYPE = 5;
~cpp vending_machine::vending_machine() { money = temp_money = 0; select_money = 1; max_num = TOTAL_DRINK_TYPE; char drinkNames[TOTAL_DRINK_TYPE][DRINKNAME_MAXLENGTH] = {"coke", "juice", "tea", "cofee", "milk"}; int price[TOTAL_DRINK_TYPE] = {400, 600, 500, 450, 350}; for (int i=0; i<max_num; i++) { strcpy(s_drink[i].name , drinkNames[i]); s_drink[i].price = price[i]; s_drink[i].amount = 10; } }
~cpp #include <iostream> using namespace std; enum { MENU_ENDCODE = 0, MENU_START = 0, MENU_GET_MONEY, MENU_BUY, MENU_TAKEBACK_MONEY, MENU_INSERT_DRINK, MENU_END = MENU_INSERT_DRINK }; const int DRINKNAME_MAXLENGTH = 255; const int TOTAL_DRINK_TYPE = 5; class vending_machine { private: int money; int temp_money; int select_money, select_drink, insert_amount; struct drink { char name[DRINKNAME_MAXLENGTH]; int price, amount; }; drink s_drink[5]; int max_num; public: vending_machine(); void GetMoney(); void Buy(); void TakeBackMoney(); void InsertDrink(); void EndMachine(); void PrintErrorMessage (); }; vending_machine::vending_machine() { money = temp_money = 0; select_money = 1; max_num = TOTAL_DRINK_TYPE; char drinkNames[TOTAL_DRINK_TYPE][DRINKNAME_MAXLENGTH] = {"coke", "juice", "tea", "cofee", "milk"}; int price[TOTAL_DRINK_TYPE] = {400, 600, 500, 450, 350}; for (int i=0; i<max_num; i++) { strcpy(s_drink[i].name , drinkNames[i]); s_drink[i].price = price[i]; s_drink[i].amount = 10; } } void vending_machine::GetMoney() { cout << "돈을 넣으세요. 10, 50, 100, 500, 1000만 가능 : "; cin >> temp_money; if(temp_money == 10 || temp_money == 50 || temp_money == 100 || temp_money == 500 || temp_money == 1000) money = money + temp_money; else cout << "10, 50, 100, 500, 1000만 가능합니다. 다시 시작해주세요\n"; cout << money << "원을 넣었습니다\n"; } void vending_machine::Buy() { cout << "음료수\t\t가격\t수량\n"; cout << "------------------------------------\n"; for(int i = 0 ; i < max_num ; i++) cout << i + 1 << "." << s_drink[i].name << "\t\t" << s_drink[i].price << "\t" << s_drink[i].amount << "\n"; cout << "\n현재 " << money << "원이 있어요\n"; cout << "원하는 음료수를 선택하세요 : "; cin >> select_drink; switch(select_drink) { case 1: if((money - s_drink[select_drink - 1].price) >= 0 && s_drink[select_drink - 1].amount >= 1) { s_drink[select_drink - 1].amount--; money = money - s_drink[select_drink - 1].price; } else cout << "잔액이 부족하거나 수량이 부족해요\n"; break; case 2: if((money - s_drink[select_drink - 1].price) >= 0 && s_drink[select_drink - 1].amount >= 1) { s_drink[select_drink - 1].amount--; money = money - s_drink[select_drink - 1].price; } else cout << "잔액이 부족하거나 수량이 부족해요\n"; break; case 3: if((money - s_drink[select_drink - 1].price) >= 0 && s_drink[select_drink - 1].amount >= 1) { s_drink[select_drink - 1].amount--; money = money - s_drink[select_drink - 1].price; } else cout << "잔액이 부족하거나 수량이 부족해요\n"; break; case 4: if((money - s_drink[select_drink - 1].price) >= 0 && s_drink[select_drink - 1].amount >= 1) { s_drink[select_drink - 1].amount--; money = money - s_drink[select_drink - 1].price; } else cout << "잔액이 부족하거나 수량이 부족해요\n"; break; case 5: if((money - s_drink[select_drink - 1].price) >= 0 && s_drink[select_drink - 1].amount >= 1) { s_drink[select_drink - 1].amount--; money = money - s_drink[select_drink - 1].price; } else cout << "잔액이 부족하거나 수량이 부족해요\n"; break; } cout << money << "원이 남았어요\n"; } void vending_machine::TakeBackMoney() { cout << "거스름돈" << money << "원을 돌려드립니다\n"; money = 0; } void vending_machine::InsertDrink() { for(int i = 0 ; i < max_num ; i++) cout << i + 1 << "." << s_drink[i].name << "\t" << s_drink[i].amount << "\n"; cout << "채우길 원하는 음료수를 선택하세요 : "; cin >> select_drink; cout << "채우길 원하는 음료수 수량을 입력해주세요 : "; cin >> insert_amount; switch(select_drink) { case 1: s_drink[select_drink - 1].amount += insert_amount; break; case 2: s_drink[select_drink - 1].amount += insert_amount; break; case 3: s_drink[select_drink - 1].amount += insert_amount; break; case 4: s_drink[select_drink - 1].amount += insert_amount; break; case 5: s_drink[select_drink - 1].amount += insert_amount; break; } cout << "음료수를 채웠습니다\n"; for(i = 0 ; i < max_num ; i++) cout << i + 1 << "." << s_drink[i].name << "\t" << s_drink[i].amount << "\n"; } void vending_machine::EndMachine() { cout << "자판기를 종료합니다!!\n\n"; } void vending_machine::PrintErrorMessage () { cout << "잘못된 메뉴 선택입니다. 메뉴를 다시 입력해주세요\n"; } void printMenu () { cout << "\n자판기 입니다\n"; cout << "1.돈을 넣는다\n"; cout << "2.물건을 산다\n"; cout << "3.돈을 거술러 받는다\n"; cout << "4.음료수를 채운다\n"; cout << "0.종료한다\n"; cout << "메뉴를 선택하세요 : "; } bool isEndMenu (int choice) { return choice != MENU_ENDCODE; } bool isValidMenu (int choice) { return choice >= MENU_START && choice <= MENU_END; } int main() { vending_machine VendingMachine; int choice = -1; while(isEndMenu (choice)) { printMenu (); cin >> choice; if(isValidMenu (choice)) { switch(choice) { case MENU_GET_MONEY: VendingMachine.GetMoney(); break; case MENU_BUY: VendingMachine.Buy(); break; case MENU_TAKEBACK_MONEY: VendingMachine.TakeBackMoney(); break; case MENU_INSERT_DRINK: VendingMachine.InsertDrink(); break; case MENU_ENDCODE: VendingMachine.EndMachine(); break; } } else VendingMachine.PrintErrorMessage (); } return 0; }
~cpp bool vending_machine::isValidMoney (int money) { return (temp_money == 10 || temp_money == 50 || temp_money == 100 || temp_money == 500 || temp_money == 1000); } void vending_machine::InsertMoney() { cout << "돈을 넣으세요. 10, 50, 100, 500, 1000만 가능 : "; cin >> temp_money; if (isValidMoney (temp_money)) { money = money + temp_money; } else cout << "10, 50, 100, 500, 1000만 가능합니다. 다시 시작해주세요\n"; cout << money << "원을 넣었습니다\n"; }
~cpp bool vending_machine::isValidMoney (int money) { int validMoneys[5] = {10, 50, 100, 500, 1000}; for (int i=0;i<5;i++) { if (money == validMoney[i]) return true; } return false; }
~cpp const int validMoney[5] = {10, 50, 100, 500, 1000}; void vending_machine::printInsertCoinMenu () { cout << "돈을 넣으세요. 10, 50, 100, 500, 1000만 가능 : "; } void vending_machine::printErrorInvalidCoinMessage () { cout << "10, 50, 100, 500, 1000만 가능합니다. 다시 시작해주세요\n"; } void vending_machine::printCurrentMoney () { cout << money << "원을 넣었습니다\n"; } bool vending_machine::isValidMoney (int money) { for (int i=0;i<5;i++) { if (money == validMoney[i]) return true; } return false; } void vending_machine::InsertMoney() { printInsertCoinMenu (); cin >> temp_money; if (isValidMoney (temp_money)) { money = money + temp_money; printCurrentMoney (); } else printErrorInvalidCoinMessage (); }
~cpp void vending_machine::printInsertCoinMenu () { cout << "돈을 넣으세요. "; for (int i=0;i<4;i++) { cout << validMoneys[i] << "," } cout << valiMoneys[4] << "만 가능 : "; }
~cpp void vending_machine::InsertMoney() { printInsertCoinMenu (); cin >> temp_money; if (isValidMoney (temp_money)) { money = money + temp_money; printCurrentMoney (); } else printErrorInvalidCoinMessage (); }이 이외엔 쓰이지 않지만, private 멤버로 있습니다. 이러한 입력을 받기 위한 임시변수는 그냥 멤버에서 없애주면 됩니다.
~cpp void vending_machine::InsertMoney() { int temp_money = 0; printInsertCoinMenu (); cin >> temp_money; if (isValidMoney (temp_money)) { money = money + temp_money; printCurrentMoney (); } else printErrorInvalidCoinMessage (); }
~cpp void vending_machine::printBuyMenu () { cout << "음료수\t\t가격\t수량\n"; cout << "------------------------------------\n"; for(int drinkIndex = 0 ; drinkIndex < max_num ; drinkIndex++) cout << drinkIndex + 1 << "." << s_drink[drinkIndex].name << "\t\t" << s_drink[drinkIndex].price << "\t" << s_drink[drinkIndex].amount << "\n"; cout << "\n현재 " << money << "원이 있어요\n"; cout << "원하는 음료수를 선택하세요 : "; } void vending_machine::Buy() { printBuyMenu (); . .2단계 - switch & case
~cpp void vending_machine::Buy() { printBuyMenu (); cin >> select_drink; if((money - s_drink[select_drink-1].price) >= 0 && s_drink[select_drink-1].amount >= 1) { s_drink[select_drink-1].amount--; money = money - s_drink[select_drink-1].price; } else cout << "잔액이 부족하거나 수량이 부족해요\n"; cout << money << "원이 남았어요\n"; }
~cpp void vending_machine::InsertDrink() { int select_drink; int insert_amount; printCurrentDrinkStatus (); cout << "채우길 원하는 음료수를 선택하세요 : "; cin >> select_drink; cout << "채우길 원하는 음료수 수량을 입력해주세요 : "; cin >> insert_amount; if (select_drink-1 > 0 && select_drink-1 <=5) s_drink[select_drink-1].amount += insert_amount; cout << "음료수를 채웠습니다\n"; printCurrentDrinkStatus (); }
~cpp #include <iostream> using namespace std; enum { MENU_ENDCODE = 0, MENU_START = 0, MENU_GET_MONEY, MENU_BUY, MENU_TAKEBACK_MONEY, MENU_INSERT_DRINK, MENU_END = MENU_INSERT_DRINK }; const int DRINKNAME_MAXLENGTH = 255; const int TOTAL_DRINK_TYPE = 5; const int validMoney[5] = {10, 50, 100, 500, 1000}; class vending_machine { private: int money; struct drink { char name[DRINKNAME_MAXLENGTH]; int price, amount; }; drink s_drink[TOTAL_DRINK_TYPE]; int max_num; public: vending_machine(); void InsertMoney(); void Buy(); void TakeBackMoney(); void InsertDrink(); void EndMachine(); bool isValidMoney (int money); void PrintErrorMessage (); void PrintErrorInvalidCoinMessage (); void PrintInsertCoinMenu(); void PrintCurrentMoney (); void PrintBuyMenu (); void PrintCurrentDrinkStatus (); }; vending_machine::vending_machine() { money = 0; max_num = TOTAL_DRINK_TYPE; char drinkNames[TOTAL_DRINK_TYPE][DRINKNAME_MAXLENGTH] = {"coke", "juice", "tea", "cofee", "milk"}; int price[TOTAL_DRINK_TYPE] = {400, 600, 500, 450, 350}; for (int i=0; i<max_num; i++) { strcpy(s_drink[i].name , drinkNames[i]); s_drink[i].price = price[i]; s_drink[i].amount = 10; } } void vending_machine::PrintInsertCoinMenu () { cout << "돈을 넣으세요. "; for (int i=0;i<4;i++) cout << validMoney[i] << "," ; cout << validMoney[4] << "만 가능 : "; } void vending_machine::PrintErrorInvalidCoinMessage () { cout << "돈을 넣으세요. "; for (int i=0;i<4;i++) cout << validMoney[i] << ","; cout << validMoney[4] << "만 가능합니다. 다시 시작해주세요\n"; } void vending_machine::PrintCurrentMoney () { cout << money << "원을 넣었습니다\n"; } bool vending_machine::isValidMoney (int money) { for (int i=0;i<5;i++) if (money == validMoney[i]) return true; return false; } void vending_machine::InsertMoney() { int temp_money = 0; PrintInsertCoinMenu (); cin >> temp_money; if (isValidMoney (temp_money)) { money = money + temp_money; PrintCurrentMoney (); } else PrintErrorInvalidCoinMessage (); } void vending_machine::PrintBuyMenu () { cout << "음료수\t\t가격\t수량\n"; cout << "------------------------------------\n"; for(int drinkIndex = 0 ; drinkIndex < max_num ; drinkIndex++) cout << drinkIndex + 1 << "." << s_drink[drinkIndex].name << "\t\t" << s_drink[drinkIndex].price << "\t" << s_drink[drinkIndex].amount << "\n"; cout << "\n현재 " << money << "원이 있어요\n"; cout << "원하는 음료수를 선택하세요 : "; } void vending_machine::Buy() { PrintBuyMenu (); int select_drink; cin >> select_drink; if((money - s_drink[select_drink-1].price) >= 0 && s_drink[select_drink-1].amount >= 1) { s_drink[select_drink-1].amount--; money = money - s_drink[select_drink-1].price; } else cout << "잔액이 부족하거나 수량이 부족해요\n"; cout << money << "원이 남았어요\n"; } void vending_machine::TakeBackMoney() { cout << "거스름돈" << money << "원을 돌려드립니다\n"; money = 0; } void vending_machine::PrintCurrentDrinkStatus () { for(int drinkIndex = 0 ; drinkIndex < max_num ; drinkIndex++) cout << drinkIndex + 1 << "." << s_drink[drinkIndex].name << "\t" << s_drink[drinkIndex].amount << "\n"; } void vending_machine::InsertDrink() { int select_drink; int insert_amount; PrintCurrentDrinkStatus (); cout << "채우길 원하는 음료수를 선택하세요 : "; cin >> select_drink; cout << "채우길 원하는 음료수 수량을 입력해주세요 : "; cin >> insert_amount; if (select_drink-1 > 0 && select_drink-1 <=5) s_drink[select_drink-1].amount += insert_amount; cout << "음료수를 채웠습니다\n"; PrintCurrentDrinkStatus (); } void vending_machine::EndMachine() { cout << "자판기를 종료합니다!!\n\n"; } void vending_machine::PrintErrorMessage () { cout << "잘못된 메뉴 선택입니다. 메뉴를 다시 입력해주세요\n"; } void PrintMenu () { cout << "\n자판기 입니다\n"; cout << "1.돈을 넣는다\n"; cout << "2.물건을 산다\n"; cout << "3.돈을 거술러 받는다\n"; cout << "4.음료수를 채운다\n"; cout << "0.종료한다\n"; cout << "메뉴를 선택하세요 : "; } bool isEndMenu (int choice) { return choice != MENU_ENDCODE; } bool isValidMenu (int choice) { return choice >= MENU_START && choice <= MENU_END; } void vending_machine::Run () { int choice = -1; while(isEndMenu (choice)) { PrintMenu (); cin >> choice; if(isValidMenu (choice)) { switch(choice) { case MENU_GET_MONEY: InsertMoney(); break; case MENU_BUY: Buy(); break; case MENU_TAKEBACK_MONEY: TakeBackMoney(); break; case MENU_INSERT_DRINK: InsertDrink(); break; case MENU_ENDCODE: EndMachine(); break; } } else PrintErrorMessage (); } } int main() { vending_machine VendingMachine; VendingMachine.Run (); return 0; }
~cpp #ifndef _VENDINGMACHINE_H_ #define _VENDINGMACHINE_H_ #include <iostream> #include <string> #include <vector> #include <map> #include <algorithm> using namespace std; class Drink { public: string name; int price; int amount; Drink (string name, int price, int amount) { this->name = name; this->price = price; this->amount = amount; } }; class VendingMachine { public: int money; vector <Drink*> drinks; vector <int> validMoney; VendingMachine() { this->money = 0; string drinkNames[] = {"coke", "juice", "tea", "cofee", "milk"}; int price[] = {400, 600, 500, 450, 350}; int defaultAmount = 10; int TOTAL_DRINK_TYPE = 5; int validMoneyList[] = {10, 50, 100, 500, 1000}; int TOTAL_MONEY_TYPE = 5; Drink* drink; for (int drinkIndex=0; drinkIndex<TOTAL_DRINK_TYPE; drinkIndex++) { drink = new Drink(drinkNames[drinkIndex], price[drinkIndex], defaultAmount); registerDrink (drink); } for (int moneyIndex=0; moneyIndex<TOTAL_MONEY_TYPE; moneyIndex++) { registerMoneyType (validMoneyList[moneyIndex]); } } ~VendingMachine () { for (int i=0; i< drinks.size(); i++) { delete drinks[i]; } drinks.clear(); } void registerMoneyType (int moneyType) { validMoney.push_back(moneyType); } bool isValidDrinkIndex (int drinkIndex) { return (drinkIndex>=0 && drinkIndex <5); } void chargeDrink (int drinkIndex, int amount) { drinks[drinkIndex]->amount += amount; } vector<int> getValidMoneyTypes(void) { return this->validMoney; } bool VendingMachine::isBuyable(int drinkId) { return ((getMoney() - drinks[drinkId]->price >= 0) && (drinks[drinkId]->amount >= 1)); } vector<Drink*> getRegisteredDrinks () { return this->drinks; } void VendingMachine::buy (int drinkId) { drinks[drinkId]->amount--; chargeMoney (drinks[drinkId]->price); } void registerDrink (Drink* drink) { drinks.push_back(drink); } bool isValidMoneyType (int money) { for (int i=0; i<validMoney.size();i++) { if (validMoney[i] == money) return true; } return false; } int takeBackMoney() { int takeBackMoney = this->money; this->money = 0; return takeBackMoney; } void insertMoney (int money) { this->money += money; } void chargeMoney (int drinkPrice) { this->money -= drinkPrice; } int getMoney () { return money; } }; #endif main.cpp #include "VendingMachine.h" #include <iostream> #include <map> using namespace std; enum { MENU_ENDCODE = 0, MENU_START = 0, MENU_GET_MONEY, MENU_BUY, MENU_TAKEBACK_MONEY, MENU_INSERT_DRINK, MENU_END = MENU_INSERT_DRINK }; typedef void(*Func)(VendingMachine&); void InsertMoney(VendingMachine& vendingMachine); void Buy(VendingMachine& vendingMachine); void TakeBackMoneyMenu(VendingMachine& vendingMachine); void InsertDrink(VendingMachine& vendingMachine); void EndMachine(VendingMachine& vendingMachine); void PrintErrorMessage (); void PrintErrorInvalidCoinMessage (VendingMachine& vendingMachine); void PrintInsertCoinMenu(); void PrintCurrentMoney (int money); void PrintBuyMenu (int money); void PrintCurrentDrinkStatus (); void PrintErrorInvalidCoinMessage (VendingMachine& vendingMachine) { cout << "돈을 넣으세요. "; vector<int> validMoney = vendingMachine.getValidMoneyTypes(); for (int i=0;i<validMoney.size()-1;i++) cout << validMoney[i] << ","; cout << validMoney[i] << "만 가능합니다. 다시 시작해주세요\n"; } void PrintCurrentMoney (int money) { cout << money << "원을 넣었습니다\n"; } void PrintBuyMenu (VendingMachine& vendingMachine) { cout << "음료수\t\t가격\t수량\n"; cout << "------------------------------------\n"; vector <Drink*> drinks = vendingMachine.getRegisteredDrinks(); for(int drinkIndex = 0 ; drinkIndex < drinks.size() ; drinkIndex++) { cout << drinkIndex << "." << drinks[drinkIndex]->name << "\t\t" << drinks[drinkIndex]->price << "\t" << drinks[drinkIndex]->amount << "\n"; } PrintCurrentMoney (vendingMachine.getMoney()); cout << "원하는 음료수를 선택하세요 : "; } void Buy(VendingMachine& vendingMachine) { PrintBuyMenu (vendingMachine); int drinkId; cin >> drinkId; if(vendingMachine.isBuyable(drinkId)) { vendingMachine.buy (drinkId); cout << vendingMachine.getMoney() << "원이 남았어요\n"; } else cout << "잔액이 부족하거나 수량이 부족해요\n"; } void TakeBackMoney (VendingMachine& vendingMachine) { int takeBackMoney = vendingMachine.takeBackMoney (); cout << "거스름돈" << takeBackMoney << "원을 돌려드립니다\n"; } void PrintCurrentDrinkStatus (VendingMachine& vendingMachine) { vector<Drink*> drinks = vendingMachine.getRegisteredDrinks(); for(int drinkIndex = 0 ; drinkIndex < drinks.size() ; drinkIndex++) cout << drinkIndex << "." << drinks[drinkIndex]->name << "\t" << drinks[drinkIndex]->amount << "\n"; } void PrintErrorMessage () { cout << "잘못된 메뉴 선택입니다. 메뉴를 다시 입력해주세요\n"; } void InsertMoney(VendingMachine& vendingMachine) { int money = 0; cout << "돈을 넣으세요. "; vector<int> validMoney = vendingMachine.getValidMoneyTypes(); for (int i=0;i<validMoney.size()-1;i++) cout << validMoney[i] << "," ; cout << validMoney[i] << "만 가능 : "; cin >> money; if (vendingMachine.isValidMoneyType (money)) { vendingMachine.insertMoney (money); PrintCurrentMoney (vendingMachine.getMoney ()); } else PrintErrorInvalidCoinMessage (vendingMachine); } void InsertDrink(VendingMachine& vendingMachine) { int selectDrink; int insertAmount; PrintCurrentDrinkStatus (vendingMachine); cout << "채우길 원하는 음료수를 선택하세요 : "; cin >> selectDrink; cout << "채우길 원하는 음료수 수량을 입력해주세요 : "; cin >> insertAmount; if (vendingMachine.isValidDrinkIndex (selectDrink)) { vendingMachine.chargeDrink (selectDrink, insertAmount); cout << "음료수를 채웠습니다\n"; } PrintCurrentDrinkStatus (vendingMachine); } void EndMachine(VendingMachine& vendingMachine) { cout << "자판기를 종료합니다!!\n\n"; } bool isEndMenu (int choice) { return choice != MENU_ENDCODE; } bool isValidMenu (int choice) { return choice >= MENU_START && choice <= MENU_END; } int InputMainMenu () { int choice = -1; cout << "\n자판기 입니다\n" << "1.돈을 넣는다\n" << "2.물건을 산다\n" << "3.돈을 거슬러 받는다\n" << "4.음료수를 채운다\n" << "0.종료한다\n" << "메뉴를 선택하세요 : "; cin >> choice; return choice; } int main() { VendingMachine vendingMachine; int choice = -1; int menuList[] = { MENU_GET_MONEY, MENU_BUY, MENU_TAKEBACK_MONEY, MENU_INSERT_DRINK, MENU_ENDCODE}; Func funcList[] = { InsertMoney, Buy, TakeBackMoney, InsertDrink, EndMachine }; map<int, Func> menuTable; for (int menuIndex=0; menuIndex<5; menuIndex++) { menuTable[menuList[menuIndex]] = funcList[menuIndex]; } while (isEndMenu (choice)) { choice = InputMainMenu (); if (isValidMenu (choice)) { (*menuTable[choice])(vendingMachine); } else PrintErrorMessage (); } return 0; }