U E D R , A S I H C RSS

Super Market/세연/재동

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

~cpp 
1.  169  141   17% 
2.    buy()함 cancel()함  
3. 클     형
4. getMyMoney() 함 public private 
5.   ...
6.      ...-,-;;;

See Also CppStudy_2002_2

See Also SuperMarket/

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