forked from Rustam-Z/cpp-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe-commerce.cpp
More file actions
442 lines (355 loc) · 10.5 KB
/
Copy pathe-commerce.cpp
File metadata and controls
442 lines (355 loc) · 10.5 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Rustam Zokirov - (U1910049) - Section 004;
// First Program - Final Assignment;
// BINARY FILES were used to manage the data;
// Temporary files were used for finding the product;
// Validations for quantity done you cannot decrease less than zero;
// Validations for UIC of product;
// You can buy a product with inputing its UIC;
// Billing system is in the Purchase() function case 3:
//
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main();
// class ITEM base class for two clasees
class Item { // class ITEM which includes the name and universal item code
protected:
string name;
string UIC;
public:
Item() { // default constructor
name = "Unknown";
UIC = "Unknown";
}
Item(string name, string UIC) { // parametirized constructor
this->name = name;
this->UIC = UIC;
}
void set_name(string name) { // function for inputing the name of item
this->name = name;
}
void set_UIC(string UIC) {
this->UIC = UIC;
}
string get_name() {
return name;
}
string get_UIC() {
return UIC;
}
void virtual display() {
cout << left << setw(20) << name << setw(15) << UIC;
}
void virtual input() {
cout << "Enter the name of a new product: ";
cin >> name;
cout << "Enter the UIC of a new product: ";
cin >> UIC;
}
};
// class for Packed Products
class PackedGroceries : public Item {
private:
double price;
int quantity;
int quantity_purchased;
public:
PackedGroceries() {
price = 0.0;
quantity = 0;
quantity_purchased = 0;
}
PackedGroceries(string name, string UIC, double price, int quantity, int quantity_purchased) : Item(name, UIC) {
this->price = price;
this->quantity = quantity;
this->quantity_purchased = quantity_purchased;
}
void display() {
Item::display();
cout << left << setw(20) << price << setw(15) << quantity << endl;
}
void input() {
Item::input();
cout << "Enter the price of a new product: ";
cin >> price;
cout << "Enter the quantity of a new product: ";
cin >> quantity;
}
void set_quantity(int quantity) {
this->quantity = quantity;
}
int get_quantity() {
return quantity;
}
double get_price() {
return price;
}
void set_quantity_p(int quantity_purchased) {
this->quantity_purchased = quantity_purchased;
}
int get_quantity_p() {
quantity_purchased++;
quantity--;
return quantity_purchased;
}
};
// classfor Fresh Products
class FreshGroceries : public Item {
private:
double price;
int quantity;
int quantity_purchased;
public:
FreshGroceries() {
price = 0.0;
quantity = 0;
quantity_purchased = 0;
}
FreshGroceries(string name, string UIC, double price, int quantity, int quantity_purchased) : Item(name, UIC) {
this->price = price;
this->quantity = quantity;
this->quantity_purchased = quantity_purchased;
}
void display() {
Item::display();
cout << left << setw(20) << price << setw(15) << quantity << endl;
}
int get_quantity() {
return quantity;
}
double get_price() {
return price;
}
int get_quantity_p() {
quantity--;
quantity_purchased++;
return quantity_purchased;
}
void input() {
Item::input();
cout << "Enter the price of a new product: ";
cin >> price;
cout << "Enter the quantity of a new product: ";
cin >> quantity;
}
};
void Purchase(){
PackedGroceries p; // declaration of object PackedGroceries
FreshGroceries f; // declaration of object for class FreshGroceri
for (int i = 0; i < 1000; i++) {
system("cls");
string UIC; // UIC for serching the product from the file
ofstream outBill("Bill", ios::binary | ios::app);
cout << "Second Main Menu: \n";
cout << "1. Go to Packed Groceries\n";
cout << "2. Go to Fresh Groceries \n";
cout << "3. Bill \n";
cout << "0. Go Back\n";
cout << "Your choice: \n";
switch (_getch()) {
case '1': {
system("cls");
ofstream outTempPacked("TempPacked", ios::binary | ios::app);
// listing the products which exist
ifstream inPacked1("Packed", ios::binary);
cout << left << setw(20) << "Name" << setw(15) << "UIC" << setw(20) << "Price" << setw(15) << "Quantity" << endl;
while (inPacked1.read((char*)&p, sizeof(PackedGroceries))) {
p.display();
}
inPacked1.close(); // closing the files after execution
// searching the product by its code
cout << "\nEnter the UIC of product you want to purchase: ";
cin >> UIC;
bool isFound = false; // bool for cheaking the existance of product
ifstream inPacked("Packed", ios::binary);
while (inPacked.read((char*)&p, sizeof(PackedGroceries))) {
// serching from the file the product
if (p.get_UIC() == UIC) {
isFound = true;
if (p.get_quantity() > 0) { // validation for quantity
p.get_quantity_p(); // functions for calculating the quantities
}
else if (p.get_quantity() < 0) { // quantity cannot be less than zero
cout << "Sorry, product is over!\n";
}
// w
outTempPacked.write((char*)&p, sizeof(PackedGroceries));
outBill.write((char*)&p, sizeof(PackedGroceries));
cout << "Successfully purchased 1 pc.\n";
cout << "Done Great\n";
}
else if (p.get_UIC() != UIC) {
outTempPacked.write((char*)&p, sizeof(PackedGroceries));
}
else {
isFound = false;
}
} // switch
outTempPacked.close(); // closing temp
inPacked.close(); // closing packed
outBill.close();
if (!isFound) {
cout << "Not Found 404 ERROR\n";
}
// removing and renaming temp
remove("Packed");
rename("TempPacked", "Packed");
cout << "\n\nThe new table: \n";
// for redisplaaying the file
ifstream inPacked1_2("Packed", ios::binary);
cout << left << setw(20) << "Name" << setw(15) << "UIC" << setw(20) << "Price" << setw(15) << "Quantity" << endl;
while (inPacked1_2.read((char*)&p, sizeof(PackedGroceries))) {
p.display();
}
inPacked1_2.close(); // closing the files after execution
cout << endl << endl;
system("pause");
}break;
case '2': {
system("cls");
ofstream outTempFresh("TempFresh", ios::binary | ios::app);
ifstream inFresh1("Fresh", ios::binary);
cout << left << setw(20) << "Name" << setw(15) << "UIC" << setw(20) << "Price" << setw(15) << "Quantity" << endl;
while (inFresh1.read((char*)&f, sizeof(FreshGroceries))) {
f.display();
}
inFresh1.close(); // closing the files after execution
// searching the product by its code
cout << "\nEnter the UIC of product you want to purchase: ";
cin >> UIC;
bool isFound2 = false;
ifstream inFresh("Fresh", ios::binary);
while (inFresh.read((char*)&f, sizeof(FreshGroceries))) {
if (f.get_UIC() == UIC) {
isFound2 = true;
if (f.get_quantity() > 0) {
f.get_quantity_p(); // functions for calculating the quantities
}
else if (f.get_quantity() < 0) {
cout << "Sorry, product is over!\n";
}
// storing the found data to files
// first for Billing system
// next for Listing
outBill.write((char*)&f, sizeof(FreshGroceries));
outTempFresh.write((char*)&f, sizeof(FreshGroceries));
cout << "Successfully purchased 1 pc.\n";
cout << "Done Great\n";
}
else if (f.get_UIC() != UIC) {
outTempFresh.write((char*)&f, sizeof(FreshGroceries));
}
else {
isFound2 = false;
}
} // switch
outTempFresh.close(); // closing temp
inFresh.close(); // closing packed
outBill.close();
if (!isFound2) {
cout << "Not Found 404 ERROR\n";
}
// removing and renaming temp
remove("Fresh");
rename("TempFresh", "Fresh");
// for redisplaying the table
cout << "\n\nThe new table: \n";
ifstream inFresh1_2("Fresh", ios::binary);
cout << left << setw(20) << "Name" << setw(15) << "UIC" << setw(20) << "Price" << setw(15) << "Quantity" << endl;
while (inFresh1_2.read((char*)&f, sizeof(FreshGroceries))) {
f.display();
}
inFresh1_2.close(); // closing the files after execution
cout << endl << endl;
system("pause");
}break;
case '3': {
system("cls");
double price_final = 0.0;
// the billing system for Packed
cout << "The billing system:\n";
ifstream inBill("Bill", ios::binary);
cout << left << setw(20) << "Name" << setw(15) << "UIC" <<endl;
while (inBill.read((char*)&p, sizeof(PackedGroceries))) {
p.Item::display();
cout << endl;
// calculating the price
price_final = p.get_quantity_p() * p.get_price();
}
inBill.close(); // closing the files after execution
cout << endl << endl;
cout << "Overall Price: " << price_final << endl << endl;
cout << "Thank you for you purchase!\n";
system("pause");
}break;
case '0': {
main();
}
break;
default: {
cout << "Your choise is not available in menu!\nPlese, try one more time.\n\n";
}
} // swich ends
} // for loop ends
}
int main() {
PackedGroceries p; // declaration of object PackedGroceries
FreshGroceries f; // declaration of object for class FreshGroceries
for (int i = 0; i < 1000; i++) {
system("cls");
cout << "Main Menu: \n";
cout << "1. List all products\n";
cout << "2. Add Packed Groceries \n";
cout << "3. Add Fresh Groceries\n";
cout << "4. Purchase\n";
cout << "Your choice: \n";
switch (_getch()) {
case '1': {
system("cls");
ifstream inPacked("Packed", ios::binary);
ifstream inFresh("Fresh", ios::binary);
// displaying the list of packed products
cout << left << setw(20) << "Name" << setw(15) << "UIC" << setw(20) << "Price" << setw(15) << "Quantity" << endl;
while (inPacked.read((char*)&p, sizeof(PackedGroceries))) {
p.display();
}
// displaying the list of fresh products
while ( inFresh.read((char*)&f, sizeof(FreshGroceries))) {
f.display();
}
// closing the files after execution
inPacked.close();
inFresh.close();
system("pause");
}break;
case '2': {
system("cls");
// inputing the info for the new item
ofstream outPacked("Packed", ios::binary | ios::app);
p.input();
// writing to binary file
outPacked.write((char*)&p, sizeof(PackedGroceries));
outPacked.close();
}break;
case '3': {
system("cls");
ofstream outFresh("Fresh", ios::binary | ios::app);
// inputing the info for the new item
f.input();
// writing to binary file the data inputted by user
outFresh.write((char*)&f, sizeof(FreshGroceries));
outFresh.close();
}break;
case '4': {
Purchase(); // function for customer purchasing
}break;
default: {
cout << "Your choise is not available in menu!\nPlese, try one more time.\n\n";
}
} // swich ends
} // for loop ends
return 0;
}