-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfood.cpp
162 lines (131 loc) · 3 KB
/
food.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
class FoodItem {
private:
string name;
string category;
string serving_amount;
double price;
public:
friend class FoodMenu;
void input() {
cout << "Food name: ";
getline(cin >> ws, name);
cout << "Category:" << endl;
category = choice_based_input({
"Fast food",
"Appetizer",
"Lunch/Dinner",
"Dessert",
"Drink",
"Other"
});
cout << "Serving amount: ";
getline(cin >> ws, serving_amount);
cout << "Price: ";
cin >> price;
}
// void edit_price() {
// cout << "New price: ";
// cin >> price;
// }
//
// bool show_actions_menu() {
// Menu menu("", {
// "Edit price",
// "Delete this item"
// }, 1);
//
// menu.display();
// switch (menu.get_choice()) {
// case 1: edit_price(); break;
// case 2: return true;
// }
//
// return false;
// }
string to_string() {
ostringstream sout;
sout << left << setw(30) << name << setw(20) << right
<< serving_amount << setw(10) << price << " Taka";
return sout.str();
}
void write(ofstream& fout) {
fout << name << endl
<< serving_amount << endl
<< price << endl;
}
void read(ifstream& fin, string category) {
this->category = category;
getline(fin >> ws, name);
getline(fin >> ws, serving_amount);
fin >> price;
}
friend bool operator<(const FoodItem& a, const FoodItem& b);
};
bool operator<(const FoodItem& a, const FoodItem& b) {
return a.name < b.name;
}
class FoodMenu {
private:
map<string, set<FoodItem>> foods;
void show_actions_menu() {
Menu menu("Manage Food Menu", {
"Show foods",
"Add food",
"Remove food",
"Go back"
}, 1);
while (true) {
menu.display();
switch (menu.get_choice()) {
case 1: show_foods(); break;
case 2: add_food(); break;
case 3: break;
case 4: return;
}
}
}
void add_food() {
FoodItem food;
food.input();
foods[food.category].insert(food);
User::save_data();
}
void write(ofstream& fout) {
fout << foods.size() << endl;
for (auto& [category, food_set] : foods) {
fout << category << endl
<< food_set.size() << endl;
for (FoodItem food : food_set)
food.write(fout);
}
}
void read(ifstream& fin) {
int n_categories;
fin >> n_categories;
while (n_categories--) {
string category;
int n_items;
getline(fin >> ws, category);
fin >> n_items;
while (n_items--) {
FoodItem food;
food.read(fin, category);
foods[category].insert(food);
}
}
}
public:
friend class Restaurant;
void show_foods() {
cout << " # " << setw(30) << left << "Name" << setw(20) << right
<< "Serving amount" << setw(15) << right << "Price" << endl;
for (auto& [category, food_set] : foods) {
vector<string> options;
for (FoodItem food : food_set)
options.push_back(food.to_string());
Menu menu(category, options, 1);
menu.display();
//menu.get_choice();
}
}
};