본문 바로가기
C++

[C++/클래스] 클래스를 이용한 자판기 프로그램

by cod1ng 2023. 11. 9.

C++ 클래스

클래스를 이용한  기초적이지만 생성자,소멸자,get메서드,set메서드를 활용한 자판기 프로그램이다

나에게는 다소 어려웠게 느껴지기에 복습하는 중 이다 

수업시간 에 연습한 코드이다

#include <iostream>
using namespace std;

class Drink
{
private:
string title;
int price;
int count;

public:
Drink(string t, int p, int c)
{
title = t;
price = p;
count = c;
}
Drink(string t, int c)
{
title = t;
price = 0;
count = c;
}
~Drink() {}
public:
string get_title() { return title; }
int get_price() { return price; }
    int get_count() { return count; }
void set_price(int p) { price = p; }
void set_count(int c) { price = c; }

public:
bool sel(int _count)
{
if (_count > count)
return false;
count = count - _count;
return true;
}
void Print()
{
cout << title << "\t";
cout << price << "\t";
cout << count << endl;
}
};
class DrinkMachine
{
Drink* drinks[5];
int max;
int money;

public:
DrinkMachine()
{
drinks[0] = new Drink("콜라", 800, 5);
drinks[1] = new Drink("사이다", 1000, 3);
drinks[2] = new Drink("포카리", 1200, 2);
drinks[3] = new Drink("비타500", 500, 1);
drinks[4] = new Drink("커피", 800, 7);

max = 5;
money = 0;
}
~DrinkMachine()
{
for (int i = 0; i < 5; i++)
{
delete drinks[i];
}
}
public:
bool input_money(int _money)
{
money = money + _money;
return true;
}
Drink* select_drink(int idx)
{
Drink* drink = drinks[idx];

if(money <drink->get_price())
return NULL;
if (drink->sel(1) == false)
return NULL;

money = money - drink->get_price();
return drink;
}
int return_money()
{
int temp = money;
money = 0;

return temp;
}
void print()
{
cout << "===============================================================" << endl;
for (int i = 0; i < max; i++)
{
Drink* p = drinks[i];
cout << "[" << i << "]";
p->print();
}
cout << "===============================================================" << endl;
cout << "투입된 금액 :"<< money << "원" << endl;
cout << "===============================================================" << endl;
}
};
int main()
{
DrinkMachine ma;
ma.print();

int money; 
cin >> money;  ma.input_money(money); ma.print();
cin >> money;  ma.input_money(money); ma.print();

int idx;
cout << ">> 제품선택 :"; cin >> idx;
Drink* p = ma.select_drink(idx);
if (p != NULL)
{
cout << p->get_title() << " 제품 구매 완료" << endl;
}
else
{
cout << "금액이나 수량이 부족 " << endl;
}
ma.print();

int dummy; cin >> dummy;
int retmoney = ma.return_money();
cout << "반한된 금액 :" << retmoney;
ma.print();
return 0;
}

#include <iostream>
using namespace std;

class Drink
{
private:
	string title;
	int price;
	int count;

public:
	Drink(string t, int p, int c)
	{
		title = t;
		price = p;
		count = c;
	}
	Drink(string t, int c)
	{
		title = t;
		price = 0;
		count = c;
	}
	~Drink() {}
public:
	string get_title() { return title; }
	int get_price() { return price; }
    int get_count() { return count; }
	void set_price(int p) { price = p; }
	void set_count(int c) { price = c; }

public:
	bool sel(int _count)
	{
		if (_count > count)
			return false;
		count = count - _count;
		return true;
	}
	void Print()
	{
		cout << title << "\t";
		cout << price << "\t";
		cout << count << endl;
	}
};
class DrinkMachine
{
	Drink* drinks[5];
	int max;
	int money;

public:
	DrinkMachine()
	{
		drinks[0] = new Drink("콜라", 800, 5);
		drinks[1] = new Drink("사이다", 1000, 3);
		drinks[2] = new Drink("포카리", 1200, 2);
		drinks[3] = new Drink("비타500", 500, 1);
		drinks[4] = new Drink("커피", 800, 7);

		max = 5;
		money = 0;
	}
	~DrinkMachine()
	{
		for (int i = 0; i < 5; i++)
		{
			delete drinks[i];
		}
	}
public:
	bool input_money(int _money)
	{
		money = money + _money;
		return true;
	}
	Drink* select_drink(int idx)
	{
		Drink* drink = drinks[idx];

		if(money <drink->get_price())
			return NULL;
		if (drink->sel(1) == false)
			return NULL;

		money = money - drink->get_price();
		return drink;
	}
	int return_money()
	{
		int temp = money;
		money = 0;

		return temp;
	}
	void print()
	{
		cout << "===============================================================" << endl;
		for (int i = 0; i < max; i++)
		{
			Drink* p = drinks[i];
			cout << "[" << i << "]";
			p->print();
		}
		cout << "===============================================================" << endl;
		cout << "투입된 금액 :"<< money << "원" << endl;
		cout << "===============================================================" << endl;
	}
};
int main()
{
	DrinkMachine ma;
	ma.print();

	int money; 
	cin >> money;  ma.input_money(money); ma.print();
	cin >> money;  ma.input_money(money); ma.print();

	int idx;
	cout << ">> 제품선택 :"; cin >> idx;
	Drink* p = ma.select_drink(idx);
	if (p != NULL)
	{
		cout << p->get_title() << " 제품 구매 완료" << endl;
	}
	else
	{
		cout << "금액이나 수량이 부족 " << endl;
	}
	ma.print();

	int dummy; cin >> dummy;
	int retmoney = ma.return_money();
	cout << "반한된 금액 :" << retmoney;
	ma.print();
	return 0;
}

Drink라는 클래스와, DrinkMachine 이라는 클래스로 구성되었다