์ค๊ฐ์ ๋ฌธ์ ๊ฐ ์์ด์ ์์ ํด์ผ ํจ... ์์ ๋๋ฉด ํ์ํ๊ฒ ์
์ ์ฒด ์์คํ ๊ณผ ๋ฉ๋ด๋ฅผ ์ํด class๋ฅผ ๋ ๊ฐ ์ฌ์ฉํ๋ค. ์ ์ฐจ ํด๋์ค์ ๊ฐ์ฒด์ ๊ฐ๋ ์ ์ต์ํด์ง๊ณ ์๋ ๊ฒ ๊ฐ๋ค... ํ์ง๋ง ์์ง๋ ๋ช ํํ ์ค๋ช ํด๋ณด๋ผ๋ฉด ๋ชจํธํ ๊ฐ์ด ์๋ ๊ฒ ๊ฐ์.
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
class MenuItem {
public:
string name;
int price;
int num;
MenuItem(string n, int p, int num) : name(n), price(p), num(num) {}
};
class Mcdonald {
private:
string userid;
int password, balance, total;
vector<MenuItem> menu = {
{"Hamburger", 1000, 0},
{"Cheeseburger", 1500, 0},
{"BigMac", 3000, 0},
{"Double Quarter Cheese", 5000, 0},
{"Fries", 1500, 0},
{"Chicken McNuggets", 2000, 0},
{"Quarter Pounder Cheese", 4000, 0},
{"Coke", 1500, 0}
};
vector<MenuItem> usermenu{};
public:
Mcdonald(string id = "", int bal = 0, int pass = 0, int t = 0) {
userid = id;
balance = bal;
password = pass;
total = t;
}
void SignUp() {
cout << "Input your ID(0-to main menu): ";
cin >> userid;
cout << "Input your password: ";
cin >> password;
cout << "Input your Initial Money: ";
cin >> balance;
cout << "Welcome, " << userid << endl;
}
bool IdCheck() {
string inputid;
int inputpass(0);
cout << "Input your ID(0-to main menu): ";
cin >> inputid;
cout << "Input your password: ";
cin >> inputpass;
if (inputpass == password && (userid.compare(inputid) == 0)) {
return true;
}
else return false;
}
void ShowMenu() {
cout << "-------McDonalds Menu-------" << endl;
for (int i = 0; i < menu.size(); ++i) {
cout << setw(2) << right << i + 1 << ". ";
cout << setw(25) << left << menu[i].name;
cout << right << "-" << menu[i].price << endl;
}
}
void Order() { //๋์ผํ ๋ฉ๋ด N๊ฐ ์ฃผ๋ฌธ์ ๊ฐ๋ณ์ ์ผ๋ก ์ง๊ณ๋จ
int getmenu(0), menunum(0);
if (IdCheck() == true) {
while (true) {
cout << "Select menu number(0-to main menu): ";
cin >> getmenu;
if (getmenu == 0) break;
else if (getmenu < 1 || getmenu > menu.size()) {
cout << "Invalid menu number" << endl;
continue;
}
cout << "How many " << menu[getmenu - 1].name << " do you want: ";
cin >> menunum;
for (int i = 0; i < menunum; ++i) {
usermenu.push_back(MenuItem(
menu[getmenu - 1].name,
menu[getmenu - 1].price,
menunum
));
}
total += menunum * menu[getmenu - 1].price;
}
if (balance - total < 0) {
cout << "Error" << endl;
usermenu.clear();
return;
}
else { balance -= total;
OrderCondition();
}
}
else cout << "Wrong User Information";
}
void OrderCondition() { //๋ฉ๋ด ๊ตฌ๋งค ์ํ์ ์ ์์ก ์ถ๋ ฅ์ด ์๋จ
if(usermenu.size() >= 1) {
for (int i = 0; i < usermenu.size(); ++i) {
cout << i + 1 << ". " << usermenu[i].name << ": " << usermenu[i].num << endl;
}
cout << "your total purchase amount is " << total << endl;
cout << "The balance is " << balance << "\nThanks" << endl;
}
}
void Refund() { //๊ตฌ๋งคํ์ง ์์ ๋ฉ๋ด ์ ํ์ ์ค๋ฅ ๋ฐ์
int i(0), refund(0);
if (IdCheck() == true) {
OrderCondition();
while (true) {
cout << "Choose number(0- to main menu): ";
cin >> i;
if (i == 0) break;
refund = (usermenu[i - 1].price) * (usermenu[i - 1].num);
total -= refund;
balance += refund;
cout << "Succesfully refunded" << endl;
cout << "your total purchase amount is " << total << endl;
cout << "The balance is " << balance << endl;
}
}
}
};
int main() {
cout << "Mcdonalds Delivery System" << endl;
int num(0);
Mcdonald user("name", 0, 0);
while (1) {
cout << "1. Sign up\n2. Show Menu\n3. Order\n4. Order condition\n5. Refund\n0.Exit\nSelect ->" << endl;
cin >> num;
if (num == 1) user.SignUp();
else if (num == 2) user.ShowMenu();
else if (num == 3) user.Order();
else if (num == 4) {
if (user.IdCheck() == true) user.OrderCondition();
else continue;
}
else if (num == 5) user.Refund();
else if (num == 0) break;
else cout << "Wrong number" << endl;
}
return 0;
}'๊ณต๋ถ > ์ฝ๋ฉ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [C++] ๊ทธ์น๋ง ์ด๋ ๊ฒ๋ผ๋ ํ์ง ์์ผ๋ฉด iostream์ด ์๋ ๋์๊ฒ ๊ด์ฌ๋ ์ฃผ์ง ์๋๊ฑธ(๋ถ๋ฆฌ ์ปดํ์ผ) (4) | 2025.08.11 |
|---|---|
| [C++] new์ malloc์ ํ๋๋ธ๋ ๋๊ฐ ๋จน๊ณ ์ถ์๋ ์ฌ์์น๊ตฌ์ ๊ฐ๋ค (3) | 2025.08.03 |
| [c++] ๋ฐฑ์ค 10828 ์คํ (0) | 2025.08.02 |
| [c++] ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ (3) | 2025.07.27 |
| [c++] ์บก์ํ์ ์ ๊ทผ์ ๋ฉ์๋ ์ฐ์ต (3) | 2025.07.27 |