-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.c
500 lines (396 loc) · 12.8 KB
/
project.c
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include <stdio.h>
#include <string.h>
//function declaration
double atof(); //to convert char value to float
void add_details();
void selling_mode();
void shop_details();
void cashier_details();
void header_bill();
void footer_bill();
void list();
int main()
{
int select;
int choice;
//select shop management or customer selling mode
printf("\n\nEnter 1 to manage shop detais--->\n");
printf("Enter 2 to selling mode--------->\n\n");
printf("Enter your choice:\n");
scanf("%d", &select);
switch (select)
{
case 1://shop management
printf("Press 0 to display item details-------------->\n");
printf("Press 1 to edit shop details----------------->\n");
printf("Press 2 to change cashier details------------>\n");
printf("Press 3 to add new items to store------------>\n");
printf("Press 4 to back------------------------------>\n");
printf("Select your choice:\n");
scanf("%d", &choice);
switch (choice)
{
case 0:
//show item list in store to cashier
list();
main();
case 1:
//edit shop details
shop_details();
main();
break;
case 2:
//edit cashier name and date
cashier_details();
main();
break;
case 3:
//add new items to store
add_details();
main();
break;
case 4:
//back to main menu
main();
break;
}
break;
case 2: //when selling items to customer(selling mode)
list();
selling_mode();
break;
}
return 0;
}
void add_details() //add new items
{
int item_count = 0;
char item_name[50];
char item_code[50];
char item_price[50];
FILE *fptr;
char filename[50];
strcpy(filename, "item_details");
fptr = fopen(filename, "a"); //open item details file to add items
getchar();
{
fflush(stdin);
printf("\nEnter item Name : ");
scanf("%[^\n]s", item_name); //enter item name
printf("\nEnter item price:");
scanf("%s", item_price); //enter item price
printf("\nEnter item code : ");
scanf("%s", item_code); //enter item code
fprintf(fptr, "%s,%s,%s\n", item_code, item_name, item_price); //print to file
fclose(fptr); //close file to save
}
main();
}
void selling_mode()
{
char copy1[20];
int count = 0;
const char s[2] = ",";
char *token;
char item_code_id[50];
double x;
int qunt;
double add;
double add2 = 0;
FILE *bill;
float cash;
float cash2;
while (1)
{
//if item code is correct item will add to bill & if enter 0 bill will be created
printf("\n\nEnter item code to add more items to bill/Enter 0 to get bill: ");
scanf("%s", item_code_id);
//Enter "0" to create bill
if (!strcmp(item_code_id, "0")) //compare the item code id and if it is "0" create the bill
{
printf("Total Amount: Rs.%.2f", add2);
printf("\nEnter cash amount: Rs."); //enter customer cash amount
scanf("%f", &cash);
if (cash < add2) //if cash not enough
{
printf("The cash amount is not enough to pay bill !!!!\n");
printf("Please enter new cash amount :Rs.");
scanf("%f", &cash2); // new cash amount
header_bill(); //header bill will print here
printf("\n================================================================================================\n");
printf("%-40s%15s%17s%17s\n", "Item", "Unit Price", "Qty", "Amount");
printf("%55s%34s\n", "(Rs.)", "(Rs.)");
printf("================================================================================================\n\n");
const char filename[] = "bill";
FILE *bill = fopen(filename, "r"); //open bill file to print bill
if (bill != NULL)
{
char line[128];
while (fgets(line, sizeof line, bill) != NULL)
{
//set count to zero each row,
count = 0;
token = strtok(line, s); //breaks string by "," (s=",")
while (token != NULL)
{
switch (count)
{
case 0:
//item name
printf("%-40s", token);
break;
case 1:
//unit price
printf("%15s", token);
break;
case 2:
//qty
printf("%17s", token);
break;
case 3:
// amount
printf("%18s\n", token);
break;
}
count++;
token = strtok(NULL, s);
}
}
fclose(bill);
}
printf("================================================================================================\n");
printf("Total Amount :%74.2f", add2); //print total amount on footer bill
printf("\nCash Amount :%74.2f", cash2);
printf("\nBalance Amount:%74.2f", cash2 - add2);
printf("\n================================================================================================\n");
remove("bill"); //delete bill
footer_bill();
break;
}
else //if customer cash amount is equal or higher than total amount
{
header_bill();
printf("\n================================================================================================\n");
printf("%-40s%15s%17s%17s\n", "Item", "Unit Price", "Qty", "Amount");
printf("%55s%34s\n", "(Rs.)", "(Rs.)");
printf("================================================================================================\n\n");
const char filename[] = "bill";
FILE *bill = fopen(filename, "r");
if (bill != NULL)
{
char line[128];
while (fgets(line, sizeof line, bill) != NULL)
{
//set count to zero each row,
count = 0;
token = strtok(line, s);
while (token != NULL)
{
switch (count)
{
case 0:
//item name
printf("%-40s", token);
break;
case 1:
//unit price
printf("%15s", token);
break;
case 2:
//qty
printf("%17s", token);
break;
case 3:
//total amount
printf("%18s\n", token);
break;
}
count++;
token = strtok(NULL, s);
}
}
fclose(bill);
}
printf("================================================================================================\n");
printf("Total Amount :%74.2f", add2);
printf("\nCash Amount :%74.2f", cash);
printf("\nBalance Amount:%74.2f", cash - add2);
printf("\n================================================================================================\n");
remove("bill");
footer_bill();
break;
}
}
printf("Enter quentity of item:"); //if customer enter a correct item code then quentityof item
scanf("%d", &qunt);
const char filename[] = "item_details";
FILE *file = fopen(filename, "r");
if (file != NULL)
{
char line[128]; //or other suitable maximum line size
while (fgets(line, sizeof line, file) != NULL) // read a line
{
//set count to zero each row,
count = 0;
//get first token item code
token = strtok(line, s);
//if both strings are same,
if (!strcmp(token, item_code_id))
{
//then go to other tokens
while (token != NULL)
{
switch (count)
{
case 1:
//item name
bill = fopen("bill", "a");
fprintf(bill, "%s,", token); // enter item name to bill file
fclose(bill);
printf("\nname:%s", token);
break;
case 2:
strcpy(copy1,token);
// item price
double x = atof(copy1);
bill = fopen("bill", "a");
fprintf(bill, "%.2f,", x); //enter unit price to bill file
fclose(bill);
printf("\nUnit price : %67.2f", x);
add = x * qunt;
bill = fopen("bill", "a");
fprintf(bill, "%d,", qunt); //enter quentityto bill file
fclose(bill);
add2 = add2 + add;
bill = fopen("bill", "a");
fprintf(bill, "%.2f\n", add); // enter amount to bill file
fclose(bill);
printf("\nqty : %67d", qunt);
printf("\n................................................................................................");
printf("\nAmount(Rs) : %67.2f\n", add);
printf("................................................................................................");
break;
}
count++;
token = strtok(NULL, s);
}
}
}
fclose(file);
}
}
main();
}
void shop_details()
{
//edit shop details
char shop_name[100];
char address[100];
char tel[15];
FILE *shop_details;
shop_details = fopen("shop_details", "w"); // open shop details file as writting mode
printf("Enter shop name :");
scanf("%s", shop_name);
printf("\nEnter shop address :");
scanf("%s", address);
printf("\nEnter shop Tel_No :");
scanf("%s", tel);
// print to file
fprintf(shop_details, "%s\n", shop_name);
fprintf(shop_details, "%s\n", address);
fprintf(shop_details, "%s\n", tel);
fclose(shop_details);
main();
}
void cashier_details()
{
//cashier details
char cashier_name[100];
int date;
int month;
int year;
FILE *cashier;
cashier = fopen("cashier", "w"); // open cashier file writting mode
printf("Enter cashier name:");
scanf("%s", cashier_name);
fprintf(cashier, "%s\n", cashier_name);
// getting date
printf("\nSet date-->");
printf("\nEnter date :");
scanf("%d", &date);
printf("\nEnter month:");
scanf("%d", &month);
printf("\nEnter year :");
scanf("%d", &year);
fprintf(cashier, "%d\n %d\n %d\n", date, month, year);
fclose(cashier);
main();
}
void header_bill() // bill header(shop name,address,t.p)
{
char shop_name[100];
char address[100];
char tel[15];
FILE *shop_details;
shop_details = fopen("shop_details", "r");
fscanf(shop_details, "%s%s%s", shop_name, address, tel);
printf("\n\n\n%96s\n%96s\n%96s\n", shop_name, address, tel);
}
void footer_bill() // bill footer (cashier name,date,greeting)
{
char cashier_name[100];
int date;
int month;
int year;
FILE *cashier;
cashier = fopen("cashier", "r");
fscanf(cashier, "%s%d%d%d", cashier_name, &date, &month, &year);
printf("Cashier :%s\nDate :%d/%d/%d\n\nTHANK YOU,COME AGAIN !!!\n\n\n\n", cashier_name, date, month, year);
}
void list() //show item list and price
{
int count = 0;
const char s[2] = ",";
char *token;
double x;
printf("\n---------------------------------------- OUR PRICE ----------------------------------------\n");
printf("\n*******************************************************************************************\n");
printf("%-4s%60s%18s\n", "Item Code", "Item", "Unit Price");
printf("%87s\n", "(Rs.)");
printf("*******************************************************************************************\n\n");
while (1)
{
const char filename[] = "item_details";
FILE *flist = fopen(filename, "r");
if (flist != NULL)
{
char line[128];
while (fgets(line, sizeof line, flist) != NULL)
{
count = 0;
token = strtok(line, s);
while (token != NULL)
{
switch (count)
{
case 0:
printf("%-4s", token); // item code
break;
case 1:
printf("%65s", token); // item name
break;
case 2:
x = atof(token);
printf("%18.2f\n", x); // unit price
break;
}
count++;
token = strtok(NULL, s);
}
printf("___________________________________________________________________________________________\n");
}
fclose(flist);
break;
}
}
}