U E D R , A S I H C RSS

Vending Machine/재니

작성�� 페�지

02 장재니 Genie

프로그램 설계


�래스 나누기

  • 먼저 ìž�íŒ�기(VendingMachine)ì�´ 필요할 것ì�´ê³ ,
  • 주문하는 사람(Man)ë�„ 있어야 í•  것ì�´ë‹¤..
  • ìž�íŒ�기는 사용ìž� ì�¸í„°íŽ˜ì�´ìŠ¤ë¥¼ 구현하는ë�° 사용하고, 사람ì�´ 주문할 ì�Œë£Œ(Drink)를 따로 분류하ìž�..
  • 그러면 주문ì�„ í•  때 ë�ˆì�´ 필요하니까 ë�ˆì�„ 세는 계수기 비슷한 것(CoinCounter)ë�„ 필요할 것 같다..^^
�정�면 �래스를 나누는 것� �난 것 같으니� ��� �체(�래스)� 필요한 역할� 나누어보�..^^

역할 분담하기

  • 먼저 ìž�íŒ�기ì�˜ ì—­í• ì�„ ë³´ìž�..... ìž�íŒ�기는 먼저 메뉴를 사람ì—�게 보여줄 수 있어야 ë� êº¼ì–Œ...
  • 그리고 사람ì�€ 메뉴를 ë³´ê³  ê°� 메뉴ì—� 알맞ì�€ 명령ì�„ 내릴 수 있어야 할것ì�´ê³ ...
  • ì�Œë£Œê°€ 해야할 ì�¼ì�€ ë­�ê°€ 있ì�„까?

    먼저 �신� �태(품목,가격,수량)� 기억하고 사람�게 보여줄 수 있어야겠지...

    그리고 사람� 주문� 했� 때 주문받� �품� 가격� 비해 잔액� 충분한지 확� 하는 것 하구..... / 계수기

    � 주문받� �품� 남아있는지� 체�해야 �꺼야!!^^

    � �가지 조건� 만족할 때�는 주문한 �품� 내줄 수 있어야 하겠고, � 때 �품� 수량� 갱신해야겠지!^^

  • í� .. 그리고 계수기ì�˜ ì—­í• ì�„ 한번 볼까? ì�´ë…€ì„�ì�€ 먼저 잔액ì�„ 기억해서 보여주어야 í•  것ì�´ê³ ..

    사람� 투입한 �� 형�� 맞는지(10,50,100,500,1000�) 확�해서 잔액� 늘려주어야 할 것�고,

    사람� �� 반환� 요구하면 �� 꺼내주고 잔액� 초기화 할 수 있어야 하겠지?^^

    아!! 그리고 주문한 �료� 가격� 따� 금액� 지불해주는 역할� 있어야겠군!^^

가만있�.. �거면 역할 분담� 대충 �난 셈�가? 그럼 한번 코딩� 해볼까?*

소스

~cpp 
#include <iostream>
#include <cstring>
using namespace std;

int selection, num;

class Man{
public:
	void select(){
		selection = 0;
		while (selection < 1 || selection > num){
			cout << ">> ";
			cin >> selection;
			if (selection < 1 || selection > num)
				cout << "잘못 입력하셨습니다. n";
		}
	};
};

class CoinCounter{
public:
	int remainders, coin;
	void resetCoins(){
		remainders = 0;
	};
	void showRemainders(){
		cout << "REMAINDERS : " << remainders << endl;
	};
	void insertCoins(){
		cout << "�� 넣으세요.n 1. 10� n 2. 50� n 3. 100� n 4. 500� n 5. 1000� n>> ";
		cin >> coin;
		if (coin == 1)			coin = 10;
		else if (coin == 2)		coin = 50;
		else if (coin == 3)		coin = 100;
		else if (coin == 4)		coin = 500;
		else if (coin == 5)		coin = 1000;
		else coin = 0;
		remainders += coin;
		cout << coin <<"�� 넣으셨습니다. n잔액� " << remainders << "입니다.n";
	};
	void pay(int used){
		cout << used << "�� 사용하셨습니다.n";
		remainders -= used;
	};
};

class VendingMachine{
public:
	void showMenu(){
	num = 4;
	cout << "MAIN MENU n 1. INSERT COIN n 2. BUY n 3. RETURN THE REMAINDERS n 4. EXIT n";
	};
};

class Drink{
public:
	struct drinks{
		char name[10];
		int price;
		int quantity;
	};
	drinks detail[3];
	Drink(){
		strcpy(detail[0].name, "사�다");
		strcpy(detail[1].name, "콜�");
		strcpy(detail[2].name, "2% 부족");
		detail[0].price = 500;
		detail[1].price = 400;
		detail[2].price = 600;
		for (int i = 0 ; i < 3 ; i++)
			detail[i].quantity = 10;
	};
	void showDetails(){
		num = 3;
		cout << " �품명t 가격t 수량n";
		for (int i = 0 ; i < 3; i++)
			cout << i+1 << ". " << detail[i].name << "t"
				<< detail[i].price << "t"
				<< detail[i].quantity << endl;
	};
	int buy(int remainders){
		if (remainders >= detail[selection - 1].price && detail[selection - 1].quantity != 0){
			cout << detail[selection - 1].name << "� 선�하셨습니다.n";
			detail[selection - 1].quantity--;
			return detail[selection - 1].price;
		}
		else if (detail[selection - 1].price > remainders)
			cout << "잔액� 부족합니다.n";
		else if (detail[selection - 1].quantity == 0)
			cout << "매진�었습니다. 다른 �품� 골�주십시오.n";
		return 0;
	};
};

int main(){
	Man man;
	CoinCounter coin_counter;
	VendingMachine vending_machine;
	Drink drink;
	int usedmoney;
	coin_counter.resetCoins();
	while(selection != 4)
	{
		vending_machine.showMenu();
		coin_counter.showRemainders();
		man.select();
		if (selection == 1)
			coin_counter.insertCoins();
		else if (selection == 2){
			drink.showDetails();
			man.select();
			usedmoney = drink.buy(coin_counter.remainders);
			coin_counter.pay(usedmoney);
		}
		else if (selection == 3){
			cout << "잔액� 반환합니다.n";
			coin_counter.resetCoins();
		}
		else if (selection == 4)
			break;
		else cout << "잘못 입력하셨습니다.n";
		system("pause");
		system("cls");
	};
	return 0;
}

만들고나서..

  • í�´ëž˜ìŠ¤ë¥¼ 너무 ë§Žì�´ 만들어서 괜히 복잡해졌다는 재ë�™ì�´ 형ì�˜ ì§€ì �ì�„ 받았습니다.

    아� �래스 사용법� 제대로 모르는지� 서툴기� 하고.. 결론� 역시 공부해야할 것� 많다는 것...^^
  • 지금까지는 안했었지만 대충ì�˜ 구조는 ë§�í•´ë‘�는 게 좋ì�„ 것 같습니다...^^

    배우는 게 많아질수� 코드가 조금씩 복잡해져서 나중�는 못 알아볼지� 모르는 ��� 드는군요..^^
  • ì�´ë²ˆì—” 프로그램 설계를 한글로 했지만 ì˜�어로 í•´ë³´ê³  ì‹ì�€ 욕심ì�´ ìƒ�기네엽..^^

    (그럼 공부할 �역� 하나 � 늘어나는건가...^^∂)
    í�´ëž˜ìФ 수가 많아서 복잡해진건 아닌듯(모 VendingMachine ì�˜ 경우 Requirement 변경ì—� ë”°ë�¼ í�´ëž˜ìŠ¤ê°¯ìˆ˜ê°€ 10ê°œ ì�´ìƒ�ì�´ ë�˜ê¸°ë�„ 함; í�´ëž˜ìФ 수가 중요하다기보다ë�„ 최종 완료ë�œ 소스가 얼마나 명료해졌ëŠ�ëƒ�ê°€ 복잡ë�„를 결정하리ë�¼ ìƒ�ê°�). 단, ì—­í•  분담할때 ê°� í�´ëž˜ìŠ¤ë³„ ì—­í• ì�´ 명료한지 ì‹ ê²½ì�„ 쓰는것ì�´ 좋겠다. CoinCounter ì�˜ 경우 VendingMachine 안ì—� 멤버로 있어ë�„ 좋ì�„듯. CRC 세션ì�„ 할때 í�´ëž˜ìŠ¤ë“¤ì�´ ê°�ê°� 따로 존재하는 것 같지만, 실제론 ê·¸ í�´ëž˜ìŠ¤ë“¤ì�´ 서로를 í�¬í•¨í•˜ê³  있기ë�„ 하거든. ë˜�는 해당 기능ì�„ 구현하기 위해 다른 í�´ëž˜ìŠ¤ë“¤ê³¼ 협ë�™í•˜ê¸°ë�„ 하고 (Collaboration. 실제 구현시엔 다른 í�´ëž˜ìФì�˜ 메소드들ì�„ 호ìœí•´ì„œ 구현한다ë�˜ì§€ ì‹�ìž„). ì—­í• ë¶„ë‹´ì�„ 하고 난 다ì�Œ 모ì�˜ 시나리오를 만든뒤 코딩해나갔다면 ì–´ë– í–ˆì�„까 하는 ìƒ�ê°�ë�„ 해본다. ì�´ 경우ì—�는 UnitTest 를 작성하는게 좋겠지. UnitTest 작성 & ì§„í–‰ì—� 대해선 ScheduledWalk/ì„�천 ì�˜ 중반부분ì�´ëž‘ UnitTest 참조.--1002
----
see also FifteenSecondsRule
----
CppStudy_2002_2 VendingMachine

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:21
Processing time 0.0065 sec