U E D R , A S I H C RSS

Vending Machine/세연

~cpp 
#include <iostream>
using namespace std;
#include <string> 
 
 
// 
class VendingMachine 
{ 
private: 
        int _money; 
        int _temp_money; 
        int _select_money, _select_drink, _insert_amount; 
        struct drink 
        { 
                char * name; 
                int price, amount; 
        }; 
        drink s_drink[5]; 
        int _max_num; 
         
public: 
        VendingMachine(); 
        int showMenu();
		void get_money(); 
        void buy(); 
        void takeBack_money(); 
        void insertDrink(); 		
};

int VendingMachine::showMenu()
{
	int choice;
	cout << "\n \n"; 
	cout << "1. \n"; 
	cout << "2. \n"; 
	cout << "3.  \n"; 
	cout << "4. \n"; 
	cout << "0.\n";  
	cout << " 택하 : "; 
	cin >> choice; 
	return choice;
}
 
//  화한 
VendingMachine::VendingMachine() 
{ 
    _money = _temp_money = 0; 
    _max_num = 5; 

	char * drink_name[] = {"coke", "juice", "tea" , "cofee", "milk"};
	int price[] = {400, 600, 500, 450, 350};

	for(int i = 0 ; i < _max_num ; i++)
	{
		s_drink[i].name = drink_name[i];
		s_drink[i].price = price[i];
		s_drink[i].amount = 10;
	}
} 
         
 
//    
void VendingMachine::get_money() 
{ 
    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 VendingMachine::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; 
    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 VendingMachine::takeBack_money() 
{
	cout << "" << _money << " \n"; 
    _money = 0; 
} 
 
//  
void VendingMachine::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; 
	s_drink[_select_drink - 1].amount += _insert_amount; 
    cout << " \n"; 
    for(i = 0 ; i < _max_num ; i++) 
            cout << i + 1 << "." << s_drink[i].name << "\t" << s_drink[i].amount << "\n"; 
} 
 
int main() 
{ 

    VendingMachine VendingMachine; 

    int choice = VendingMachine.showMenu();

	while(choice != 0)
	{
		if( choice >= 0 || choice <= 4)
		{
			switch(choice) 
			{ 
			case 1: 
					VendingMachine.get_money(); 
					break; 
			case 2: 
					VendingMachine.buy(); 
					break; 
			case 3: 
					VendingMachine.takeBack_money(); 
					break; 
			case 4: 
					VendingMachine.insertDrink(); 
					break; 
			case 0: 
					cout << " !!\n\n";  
					break; 
			}
		}			
		else
			cout << " .   : ";

		choice = VendingMachine.showMenu();	
	}

	return 0; 
} 



한 평 ,
  • 편한. - public 해 'how' 'what' . 클 통 ' ' , '' .
  • - , , . Copy & Paste / .

  • , . --1002
  • --




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