-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectExample2.cpp
81 lines (69 loc) · 1.76 KB
/
ObjectExample2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <vector>
using namespace std;
class MenuItem {
private:
string name;
double price;
public:
MenuItem(string name, double price) {
this->name = name;
this->price = price;
}
string get_name() {
return name;
}
double get_price() {
return price;
}
};
class Order {
private:
vector<MenuItem> items;
public:
void add_item(MenuItem item) {
items.push_back(item);
}
void print_receipt() {
double total = 0.0;
cout << "----------------------\n" ;
cout << "Receipt:" << endl;
for (int i = 0; i < items.size(); i++) {
cout << "- " << items[i].get_name() << ": " << items[i].get_price() << " $" << endl;
total += items[i].get_price();
}
cout << "Total: " << total << " $" << endl;
}
};
int main() {
MenuItem pizza("Pizza", 25.0);
MenuItem coke("Coke", 5.0);
MenuItem salad("Salad", 12.0);
Order order;
char choice = '0';
while (choice != '3') {
cout << "----------------------";
cout << "\nSelect an option: " << endl;
cout << "1. Add item" << endl;
cout << "2. Print receipt" << endl;
cout << "3. Exit" << endl;
cin >> choice;
if (choice == '1') {
string name;
double price;
cout << "Enter item name: ";
cin >> name;
cout << "Enter item price: ";
cin >> price;
MenuItem item(name, price);
order.add_item(item);
}
else if (choice == '2') {
order.print_receipt();
}
else if (choice != '3') {
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}