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. 더 고쳐져야 할 부분이 보이지만 미묘해서 그만 둠...-,-;;;
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:09
Processing time 0.0142 sec