-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuper_market.cpp
More file actions
329 lines (301 loc) · 7.33 KB
/
Copy pathsuper_market.cpp
File metadata and controls
329 lines (301 loc) · 7.33 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
#include<iostream>
#include<fstream>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
class Product {
private:
int p_no;
char name[31];
float amount, tax, discount;
public:
Product(){
p_no = 0;
amount = 0;
tax = 0;
discount = 0;
strcpy(name, "NULL");
}
Product(int p_no, char name[], float amount, float tax, float discount){
this->p_no = p_no;
this->amount = amount;
this->tax = tax;
this->discount = discount;
strcpy(this->name, name);
}
void display(){
cout<<"\n Product No.: "<<p_no;
cout<<"\n Name: "<<name;
cout<<"\n Price: "<<amount;
cout<<"\n Tax: "<<tax;
cout<<"\n Discount: "<<discount;
}
int return_p_no(){
return p_no;
}
float return_amount(){
return amount;
}
float return_discount(){
return discount;
}
char* return_name(){
return name;
}
float return_price(){
return amount + amount * (tax / 100) - amount * (discount / 100);
}
void write_to_file(char* file_name){
fstream fp;
fp.open(file_name, ios::out|ios::app|ios::binary);
fp.write((char*)this, sizeof(Product));
fp.close();
}
void read_from_file(char* file_name){
fstream fp;
fp.open(file_name, ios::in|ios::binary);
fp.read((char*)this, sizeof(Product));
fp.close();
}
};
Product find_product(int p_no, char* file_name){
fstream fp;
fp.open(file_name, ios::in|ios::binary);
Product pr;
while(fp.read((char*)&pr, sizeof(Product))){
if(pr.return_p_no() == p_no){
fp.close();
return pr;
}
}
fp.close();
return Product();
}
int delete_product(int p_no, char* file_name){
fstream fp, temp;
fp.open(file_name, ios::in);
temp.open("Temp.dat", ios::out);
Product pr;
int flag = 0;
while(fp.read((char*)&pr, sizeof(Product))){
if(pr.return_p_no() == p_no){
flag = 1;
continue;
}
temp.write((char*)&pr, sizeof(Product));
}
fp.close();
temp.close();
remove(file_name);
rename("Temp.dat", file_name);
return flag;
}
int update_product(int p_no, char* file_name){
fstream fp;
fp.open(file_name, ios::in|ios::out);
Product pr;
while(fp.read((char*)&pr, sizeof(Product))){
if(pr.return_p_no() == p_no){
int p_no;
float amount, discount, tax;
char name[31];
cout<<"\n Name: ";
gets(name);
cout<<"\n Number: ";
cin>>p_no;
cout<<"\n Price: ";
cin>>amount;
cout<<"\n Tax: ";
cin>>tax;
cout<<"\n Discount: ";
cin>>discount;
Product p(p_no, name, amount, tax, discount);
fp.seekg(-1 * sizeof(Product));
fp.write((char*)&p, sizeof(Product));
fp.close();
return 1;
}
}
fp.close();
return 0;
}
void display_all_products(char* file_name){
fstream fp;
fp.open(file_name, ios::in|ios::out);
Product pr;
while(fp.read((char*)&pr, sizeof(Product))){
pr.display();
cout<<"\n\n==============================\n\n";
}
}
void print_invoice(int prod_nos[], int qty[], int size, char* file_name){
float total = 0;
cout<<"\n\n-------------------------INVOICE-------------------------\n";
cout<<"\tP. No.\t\tName\t\tQty.\t\tAmount"<<endl;
for(int i = 0; i < size; i++){
Product p = find_product(prod_nos[i], file_name);
if(strcmp(p.return_name(), "NULL") == 0){
cout<<"\t"<<prod_nos[i]<<" NOT FOUND------------------"<<endl;
}else{
float amt = p.return_price() * qty[i];
cout<<"\t"<<prod_nos[i]<<"\t"<<p.return_name()<<"\t\t"<<qty[i]<<"\t\t"<<amt<<endl;
total += amt;
}
}
cout<<"\t\t\t\t\t\tTotal Amount: "<<total;
}
void menu(char* file_name){
fstream fp;
fp.open(file_name, ios::in|ios::out);
Product pr;
cout<<"\n\n-------------------------INVOICE-------------------------\n";
cout<<"\tP. No.\t\tName\t\tAmount"<<endl;
while(fp.read((char*)&pr, sizeof(Product))){
cout<<"\t"<<pr.return_p_no()<<"\t"<<pr.return_name()<<"\t\t"<<pr.return_amount()<<endl;
}
}
void place_order(char* file_name){
menu(file_name);
int prod_nos[10], qty[10];
int order_index = 0;
char ch = 'y';
while(ch == 'Y' || ch == 'y'){
cout<<"\n Enter the Product number: ";
cin>>prod_nos[order_index];
cout<<"\n Enter Quantity: ";
cin>>qty[order_index++];
cout<<"\n\n Want to enter more products?(y/n): ";
cin>>ch;
}
print_invoice(prod_nos, qty, order_index, file_name);
}
void admin_menu(char* file_name){
int choice, p_no, deleted, updated;
float amount, tax, discount;
char name[31];
Product p;
while(1){
system("cls");
cout<<"\n\n\t -------------Admin Menu----------------";
cout<<"\n\t\t1. Create Product";
cout<<"\n\t\t2. Delete Product";
cout<<"\n\t\t3. Display All Products";
cout<<"\n\t\t4. Update Product";
cout<<"\n\t\t5. Display Product Menu";
cout<<"\n\t\t6. Search Product";
cout<<"\n\t\t7. Back to Main Menu";
cout<<"\n\t-----------------------------------------";
cout<<"\n\nEnter your choice: ";
cin>>choice;
switch(choice){
case 1: cout<<"\nEnter Product Details";
cout<<"\nNumber: ";
cin>>p_no;
cout<<"\nName: ";
gets(name);
cout<<"\nAmount: ";
cin>>amount;
cout<<"\nTax: ";
cin>>tax;
cout<<"\nDiscount: ";
cin>>discount;
p = Product(p_no, name, amount, tax, discount);
p.write_to_file(file_name);
cout<<"\nProduct Saved";
break;
case 2: cout<<"\nEnter the product Number to delete: ";
cin>>p_no;
deleted = delete_product(p_no, file_name);
if(!deleted){
cout<<"\nProduct Not found!!";
}else{
cout<<"\nProduct Deleted!!";
}
break;
case 3: display_all_products(file_name);
break;
case 4: cout<<"\nEnter the product Number to Update: ";
cin>>p_no;
updated = update_product(p_no, file_name);
if(!updated){
cout<<"\nProduct Not found!!";
}else{
cout<<"\nProduct Updated!!";
}
break;
case 5: menu(file_name);
break;
case 6: cout<<"\nEnter the product Number to Search: ";
cin>>p_no;
p = find_product(p_no, file_name);
if(strcmp(p.return_name(), "NULL") == 0){
cout<<"\nProduct Not found!!";
}else{
cout<<endl;
p.display();
}
case 7: return;
default: cout<<"\nEnter right choice!!";
}
cout<<"\nPress enter to continue!!";
cin.ignore();
cin.get();
}
}
void intro(){
system("cls");
cout<<"\n\n\t====================SUPERMARKET====================";
cout<<"\n\t\t\tBilling Project";
cout<<"\n\t\t\t\t\t\tMade By";
cout<<"\n\t\t\t\t\t\tAdd your Name Here";
cout<<"\n\t===================================================";
cout<<"\n\nPress enter to continue";
cin.get();
}
int main(){
intro();
/*int p_no;
float amount, discount, tax;
char name[31];
gets(name);
cin>>p_no;
cin>>amount;
cin>>tax;
cin>>discount;
Product p(p_no, name, amount, tax, discount);
p.display();
p.write_to_file("Shop.dat");
Product p2 = find_product(12, "Shop.dat");
p2.display();
delete_product(12, "Shop.dat");
p2 = find_product(12, "Shop.dat");
p2.display();
display_all_products("Shop.dat");
return 0;*/
char* file_name = "Shop.dat";
int choice;
while(1){
system("cls");
cout<<"\n\n\t-----------------MAIN MENU-----------------";
cout<<"\n\t\t1. Customer";
cout<<"\n\t\t2. Administrator";
cout<<"\n\t\t3. Exit";
cout<<"\n\t-------------------------------------------";
cout<<"\n\nEnter your Choice: ";
cin>>choice;
switch(choice){
case 1: place_order(file_name);
break;
case 2: admin_menu(file_name);
break;
case 3: return 0;
default: cout<<"\nEnter your choice!!";
}
cout<<"\nPress Enter to continue!!";
cin.ignore();
cin.get();
}
return 0;
}