U E D R , A S I H C RSS

Cpp Study_2002_2/객체와클래스

DeleteMe) 누구소스인지는 모르겠지만, 다른 소스들처럼 page name 을 바꿔주는것이 좋을듯. --1002

자판기

vending.h

~cpp 
#ifndef _VENDING_H_
#define _VENDING_H_

class Vending
{
private:
	int m_money;
	char m_beverage[3][10];
	int m_price[3];
	int m_quantity[3];
public:
	Vending();
	void insertCoin();
	void extortCoin();
	void mainMenu();
	void buyBeverage();
	void update();
};

#endif

vending.cpp

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

#include "vending.h"

Vending::Vending()
{
	m_money = 0;
	strcpy(m_beverage[0],"사이다");
	strcpy(m_beverage[1],"콜라");
	strcpy(m_beverage[2],"2%");
	m_price[0] = 500;
	m_price[1] = 400;
	m_price[2] = 600;
	m_quantity[0] = 10;
	m_quantity[1] = 10;
	m_quantity[2] = 10;
	
}
void Vending::insertCoin()
{
	int money;
	cout << "돈을 넣으세요\n";
	cout << "10,50,100,500,1000원만 가능\n";
	cout << ">> ";
	cin >> money;
	if(money==10 || money==50 || money==100 || money==500 || money ==1000)
	{
		m_money += money;
	}
	else 
		cout << "제대로 된 동전이나 지폐를 넣어 주세요!\n";
}

void Vending::extortCoin()
{
	m_money = 0;
}

void Vending::mainMenu()
{
	cout << "메인메뉴\n";
	cout << "1. 돈 넣기\n";
	cout << "2. 사기\n";
	cout << "3. 돈 빼기\n";
	cout << "4. 종료\n";
	cout << "5. 음료수 체우기\n";
	cout << "남은 돈: " << m_money << "\n";
	cout << ">> ";
}

void Vending::buyBeverage()
{
	int choice;
	cout << "메뉴\n";
	cout << "1. " << m_beverage[0] << " " << m_price[0] << "원 " << m_quantity[0] << "개\n";
	cout << "2. " << m_beverage[1] << " " << m_price[1] << "원 " << m_quantity[1] << "개\n";
	cout << "3. " << m_beverage[2] << " " << m_price[2] << "원 " << m_quantity[2] << "개\n";
	cout << "음료수를 고르세요 >> ";
	cin >> choice;
	if(choice >0 && choice <4 && m_quantity[choice-1]>0 && ((m_money-m_price[choice-1])>0))
	{
		cout << m_beverage[choice-1] << "을 사셨습니다\n";
		switch(choice)
		{
		case 1:
			m_money -= m_price[0];
			m_quantity[0]--;
			break;
		case 2:
			m_money -= m_price[1];
			m_quantity[1]--;
			break;
		case 3:
			m_money -= m_price[2];
			m_quantity[2]--;
			break;
		}
	}
	else
		cout << "다시 선택 해주세요\n";
}

void Vending::update()
{
	int choice;
	cout << "메뉴\n";
	cout << "1. " << m_beverage[0] << " " << m_quantity[0] << "개\n";
	cout << "2. " << m_beverage[1] << " " << m_quantity[1] << "개\n";
	cout << "3. " << m_beverage[2] << " " << m_quantity[2] << "개\n";
	cout << "음료수를 고르세요 >> ";
	cin >> choice;
	if(choice >0 && choice <4)
	{
		int quantity;
		cout << m_beverage[choice-1] << "을 고르셨습니다\n";
		cout << "체울 양을 입력해주세요 >> ";
		cin >> quantity;
		switch(choice)
		{
		case 1:
			m_quantity[0] += quantity;
			break;
		case 2:
			m_quantity[1] += quantity;
			break;
		case 3:
			m_quantity[2] += quantity;
			break;
		}
	}
	else
		cout << "다시 선택 해주세요\n";
}

useVending.cpp

~cpp 
#include <iostream>
#include "vending.h"
using namespace std;

int main()
{
	int choice;
	Vending vending;

	vending.mainMenu();
	cin >> choice;
	while(choice != 4)
	{
		switch(choice)
		{
		case 1:
			
			vending.insertCoin();
			break;
		case 2:
			vending.buyBeverage();
			break;
		case 3:
			vending.extortCoin();
			break;
		case 5:
			vending.update();
			break;
		}
		vending.mainMenu();
		cin >> choice;
	};
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:02
Processing time 0.0141 sec