-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
335 lines (263 loc) · 7.35 KB
/
Copy pathmain.cpp
File metadata and controls
335 lines (263 loc) · 7.35 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* Supermarket Console System
*
* Author: ImEasooon
* License: Education
* GitHub: https://github.com/ImEasooon
*
* Description:
* A simple console-based supermarket system written in C++.
*/
#include <iostream>
#include <iomanip>
#include <cctype>
#include <fstream>
#define Size 20
using namespace std;
struct menu{
string product;
float price, total = 0;
int quantity = 0;
};
bool login();
bool registration();
bool validation(string username);
void cate_menu();
void category(menu cart[], int start, int end, string title);
void invoice(menu cart[]);
void main(){
struct menu cart[Size] = {{"Fish", 12.00, 0, 0}, {"Chicken", 8.50, 0, 0}, {"Beef", 17.00, 0, 0}, {"Pork", 10.50, 0, 0},
{"Potato", 5.00, 0, 0}, {"Cabbage", 7.00, 0, 0}, {"Onion", 3.00, 0, 0}, {"Cucumber", 2.00, 0, 0},
{"Bread", 4.00, 0, 0}, {"Biscuit", 5.60, 0, 0}, {"Noodle", 8.00, 0, 0}, {"Rice", 16.00, 0, 0},
{"Water", 1.00, 0, 0}, {"Milk", 8.50, 0, 0}, {"Coffee", 12.00, 0, 0}, {"Beer", 20.50, 0, 0},
{"Potato Chips", 5.30, 0, 0},{"Chocolate", 15.80, 0, 0}, {"Popcorn", 9.00, 0, 0}, {"Nuts", 6.00, 0, 0}};
char method;
cout<<"Welcome to Supermarket"<<endl;
while(true){
cout<<"Press L to Log in"<<endl;
cout<<"Press S to Sign up"<<endl;
cout<<endl;
cout<<"Enter: ";
cin>>method;
cout<<endl;
method = toupper(method);
if(method == 'L'){
if(login()) break;
}
else if(method == 'S'){
if(registration()){
if(login()) break;
}
}
else{
cout<<"Invalid Option. Please try again!"<<endl;
cout<<endl;
}
}
int selected_cate;
while(true){
cate_menu();
cout<<"Enter 0 to quit the process"<<endl;
cout<<"Enter number to choose the category: ";
cin>>selected_cate;
if(cin.fail()){
cout<<"Invalid input! Please enter a valid number."<<endl;
cin.clear();
cin.ignore(1000, '\n');
continue;
}
if(selected_cate == 0) break;
switch(selected_cate){
case 1: category(cart, 0, 3, "Meat"); break;
case 2: category(cart, 4, 7, "Vegetable"); break;
case 3: category(cart, 8, 11, "Food"); break;
case 4: category(cart, 12, 15, "Drink"); break;
case 5: category(cart, 16, 19, "Snack"); break;
default: cout<<"Invalid category!"<<endl;
}
}
invoice(cart);
}
bool login(){
string username, password, cus_username, cus_password;
int attempt = 3;
bool status = false;
cout<<"Only 3 attempts allowed"<<endl;
while(attempt--){
cout<<"Username: ";
cin>>username;
cout<<"Password: ";
cin>>password;
ifstream log_system("./data/Customer Account.txt");
if(!log_system){
cout<<"File Opening Error!"<<endl;
return false;
}
while(log_system >> cus_username >> cus_password){
if(username == cus_username && password == cus_password){
status = true;
break;
}
}
log_system.close();
if(status){
cout<<"Login successful!"<<endl;
cout<<"Press any key to continue...";
cin.ignore();
cin.get();
cout<<endl;
return true;
}
else{
cout<<"Invalid credentials. Attempts left: "<<attempt<<" time(s)"<<endl;
cout<<endl;
}
}
cout<<"Too many failed attempts. Access denied!"<<endl;
cout<<endl;
return false;
}
bool registration(){
string username, psswrd, cfm_psswrd;
bool isValid = false;
while(!isValid){
cout<<"Username: ";
cin>>username;
isValid = validation(username);
}
while(true){
cout<<"Password: ";
cin>>psswrd;
cout<<"Confirm Password: ";
cin>>cfm_psswrd;
if(psswrd == cfm_psswrd) break;
cout<<"Passwords do not match. Try again!"<<endl;
}
ofstream regis_system("./data/Customer Account.txt", ios::app);
if(!regis_system){
cout<<"File Opening Error!"<<endl;
return false;
}
regis_system<<username<<" "<< psswrd<<endl;
regis_system.close();
cout<<"Account registered successfully!"<<endl;
cout<<"Press any key to continue...";
cin.ignore();
cin.get();
cout<<endl;
return true;
}
bool validation(string username){
ifstream check("./data/Customer Account.txt");
string chk_username, password;
while(check >> chk_username >> password){
if(chk_username == username){
cout<<"Username already exists!\n";
return false;
}
}
check.close();
return true;
}
void cate_menu(){
cout<<endl;
cout<<"Choose the category for products"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
cout<<"No | Category"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
cout<<"1 | Meat"<<endl;
cout<<"2 | Vegetable"<<endl;
cout<<"3 | Food"<<endl;
cout<<"4 | Drink"<<endl;
cout<<"5 | Snack"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
cout<<endl;
}
void category(menu cart[], int start, int end, string title){
int quantity;
char x;
cout<<"--------------------------------------------------------------"<<endl;
cout<<left<<setw(5)<<"No"<<"|"
<<setw(20)<<title<<"|"
<<setw(5)<<"Price($)"<<endl;
cout<<"--------------------------------------------------------------"<<endl;
for(int i = start; i <= end; i++){
cout<<left<<setw(5)<<char('A' + (i - start))<<"|"
<<setw(20)<<cart[i].product<<"|"
<<fixed<<setprecision(2)
<<setw(5)<<cart[i].price<<endl;
}
cout<<"--------------------------------------------------------------"<<endl;
cout<<"Enter Q to quit the process"<<endl;
while(true){
cout<<"Your Option: ";
cin>>x;
x = toupper(x);
if(x == 'Q') break;
// ASCII: A = 65, B = 66, C = 67, D = 68
// E.g. D - A = 3
int index = (x - 'A') + start;
if(index >= start && index <= end){
cout<<"Quantity: ";
cin>>quantity;
if(cin.fail()){
cout<<"Invalid input! Please enter a valid number."<<endl;
cin.clear();
cin.ignore(1000, '\n');
continue;
}
if(quantity <= 0){
cout<<"Invalid quantity!"<<endl;
continue;
}
cart[index].quantity += quantity;
cart[index].total += quantity * cart[index].price;
cout<<quantity<<" "<<cart[index].product<<" added to cart!"<<endl;
cout<<endl;
}
else{
cout<<"Invalid Option. Please try again!"<<endl;
cout<<endl;
}
}
cout<<endl;
}
void invoice(menu cart[]){
int x, y = 1;
float total = 0;
ofstream receipt;
receipt.open("./data/Supermarket Receipt.txt", ios::out);
receipt<<"-----------------------------------------------------"<<endl;
receipt<<" Supermarket Receipt "<<endl;
receipt<<"-----------------------------------------------------"<<endl;
receipt<<left<<setw(10)<<"No"
<<setw(30)<<"Products"
<<setw(15)<<"Qty"
<<"$"<<endl;
receipt<<"-----------------------------------------------------"<<endl;
receipt<<fixed<<setprecision(2);
for(x=0; x<Size; x++){
if(cart[x].quantity > 0){
receipt<<left<<setw(10)<<y
<<setw(30)<<cart[x].product
<<setw(15)<<cart[x].quantity
<<fixed<<setprecision(2)
<<cart[x].total<<endl;
y++;
}
}
receipt<<"-----------------------------------------------------"<<endl;
for(x=0; x<Size; x++){
if(cart[x].total > 0){
total = total + cart[x].total;
}
}
receipt<<"Total Payment : $ " <<total<<endl;
receipt<<"-----------------------------------------------------"<<endl;
receipt.close();
if(y == 1){
cout<<"Cart is empty!"<<endl;
return;
}
cout<<"A receipt has been generated"<<endl;
}