-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
279 lines (274 loc) · 9.11 KB
/
main.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
#include "dec.h"
#include "def.h"
int main()
{
// DECLARATION
int choice, qtyofitems;
struct orders order;
struct orders ord;
char saveBill = 'y', startAgain = 'y';
char name[50];
int billID;
int DeleteID;
FILE *fp;
while (startAgain == 'y')
{
float total = 0;
int BillFound = 0;
// Get the current time
time_t currentTime;
time(¤tTime);
char *timeString = ctime(¤tTime);
timeString[strlen(timeString) - 1] = '\0'; // Remove the newline character
// START
system("cls");
printf("\n\t----------------");
printf("\n\tHamza Restaurant ");
printf("\n\t----------------");
// Option Show
printf("\nPlease Select Your Prefered Choice");
printf("\n1: Generate Bill.");
printf("\n2: Show All Bills.");
printf("\n3: Search Bill By Name.");
printf("\n4: Search Bill By ID.");
printf("\n5: Delete Bill by Name.");
printf("\n6: Delete Bill by ID.");
printf("\n7: Exit.\n");
printf("\nYour Choice? ");
scanf("%d", &choice);
fgetc(stdin);
playSuccessTone();
switch(choice)
{
// Generate Bills
case 1:
system("cls");
printf("\n-----Generate New Bill-----");
order.billID = generateBillID();
printf("\nPlease Enter Customer Name: ");
fgets(order.customerName, 50, stdin);
// Removing \n from fgets func
order.customerName[strlen(order.customerName) - 1] = 0;
strcpy(order.date, __DATE__);
printf("Please Enter the Number of Items: ");
scanf("%d", &qtyofitems);
order.QuantityofItems = qtyofitems;
for (int i = 0; i < qtyofitems; i++)
{
fgetc(stdin);
printf("\n\n");
printf("Please Enter Item %d: ", i + 1);
fgets(order.itm[i].item, 20, stdin);
order.itm[i].item[strlen(order.itm[i].item) - 1] = 0;
printf("Please Enter the Quantity: ");
scanf("%d", &order.itm[i].qty);
printf("Please Enter the unit price: ");
scanf("%f", &order.itm[i].price);
total += order.itm[i].qty * order.itm[i].price;
}
system("cls");
BillGenerateHead(order.customerName, order.date,timeString,order.billID);
for (int i = 0; i < order.QuantityofItems; i++)
{
BillGenerateBody(order.itm[i].item, order.itm[i].qty, order.itm[i].price);
}
BillGenerateFooter(total);
printf("\nDo You Want to save the Bill(y/n)? ");
scanf("%s", &saveBill);
if (saveBill == 'y')
{
fp = fopen("RestaurantBill.txt", "a+");
fwrite(&order, sizeof(struct orders), 1, fp);
if (fwrite != 0)
{
playSuccessTone();
printf("Successfully Saved!!");
}
else
{
playErrorTone();
printf("\nTrouble in Saving..!");
fclose(fp);
}
fclose(fp);
break;
}
// Show Bills
case 2:
system("cls");
fp = fopen("RestaurantBill.txt", "r");
printf("\n");
printf("\n----------All Bills----------");
while (fread(&ord, sizeof(struct orders), 1, fp))
{
float tot = 0;
BillGenerateHead(ord.customerName, ord.date,timeString,ord.billID);
for (int i = 0; i < ord.QuantityofItems; i++)
{
BillGenerateBody(ord.itm[i].item, ord.itm[i].qty, ord.itm[i].price);
tot += ord.itm[i].qty * ord.itm[i].price;
}
BillGenerateFooter(tot);
}
fclose(fp);
break;
// Search Bill By Name
case 3:
system("cls");
printf("\n-----Search Bill By Name-----");
printf("\nEnter the Name of the Customer ");
fgets(name, 50, stdin);
name[strlen(name) - 1] = 0;
system("cls");
fp = fopen("RestaurantBill.txt", "r");
printf("\n");
printf("********Bill of %s********\n", name);
while(fread(&ord, sizeof(struct orders), 1, fp))
{
float tot = 0;
// strcmp to compare two strings
if (strcmp(ord.customerName, name) == 0)
{
playSuccessTone();
BillGenerateHead(ord.customerName, ord.date,timeString,ord.billID);
for (int i = 0; i < ord.QuantityofItems; i++)
{
BillGenerateBody(ord.itm[i].item, ord.itm[i].qty, ord.itm[i].price);
tot += ord.itm[i].qty * ord.itm[i].price;
}
BillGenerateFooter(tot);
BillFound = 1;
}
}
if (!BillFound)
{
playErrorTone();
printf("\nBill of %s Not Found..!", name);
}
break;
//Search Bill By ID
case 4:
system("cls");
printf("\n-----Search Bill By ID-----");
printf("\nEnter the Bill ID ");
scanf("%d",&DeleteID);
system("cls");
fp = fopen("RestaurantBill.txt", "r");
printf("\n");
printf("***Bill %d***\n", DeleteID);
while(fread(&ord, sizeof(struct orders), 1, fp))
{
float TOT = 0;
if (ord.billID == DeleteID)
{
playSuccessTone();
BillGenerateHead(ord.customerName, ord.date,timeString,ord.billID);
for (int i = 0; i < ord.QuantityofItems; i++)
{
BillGenerateBody(ord.itm[i].item, ord.itm[i].qty, ord.itm[i].price);
TOT += ord.itm[i].qty * ord.itm[i].price;
}
BillGenerateFooter(TOT);
BillFound = 1;
}
}
if (!BillFound)
{
playErrorTone();
printf("\nBill %d Not Found..!", DeleteID);
}
break;
// Delete Bill By Name
case 5:
system("cls");
printf("\n-----Delete Bill By Name-----");
printf("\nEnter the Name of the Customer: ");
fgets(name, 50, stdin);
name[strlen(name) - 1] = '\0';
system("cls");
fp = fopen("RestaurantBill.txt", "r+");
FILE *tempFile = fopen("temp.txt", "w"); // Create a temporary file for writing
struct orders ord;
int found = 0;
while (fread(&ord, sizeof(struct orders), 1, fp))
{
if (strcmp(ord.customerName, name) != 0)
{
// Write the non-matching bill entries to the temporary file
fwrite(&ord, sizeof(struct orders), 1, tempFile);
}
else
{
found = 1;
}
}
fclose(fp);
fclose(tempFile);
remove("RestaurantBill.txt"); // Remove the original file
rename("temp.txt", "RestaurantBill.txt"); // Rename the temporary file to the original file name
if (found)
{
playSuccessTone();
printf("\nBill of %s deleted successfully!", name);
}
else
{
playErrorTone();
printf("\n%sBill Not Found..!", name);
}
break;
// Delete Bill By ID
case 6:
system("cls");
printf("\n-----Delete Bill By ID-----");
printf("\nEnter the ID of the Bill: ");
scanf("%d", &billID);
system("cls");
fp = fopen("RestaurantBill.txt", "r");
FILE *tempFILE = fopen("temp.txt", "w"); // Create a temporary file for writing
int Found = 0;
while (fread(&ord, sizeof(struct orders), 1, fp))
{
if (ord.billID != billID)
{
// Write the non-matching bill entries to the temporary file
fwrite(&ord, sizeof(struct orders), 1, tempFILE);
}
else
{
Found = 1;
}
}
fclose(fp);
fclose(tempFILE);
remove("RestaurantBill.txt"); // Remove the original file
rename("temp.txt", "RestaurantBill.txt"); // Rename the temporary file to the original file name
if (Found)
{
playSuccessTone();
printf("\nBill with ID %d deleted successfully!", billID);
}
else
{
playErrorTone();
printf("\nBill with ID %d not found..!", billID);
}
break;
//Exit
case 7:
playSuccessTone();
printf("\n\t\t Bye Bye.........!");
exit(0);
break;
default:
playErrorTone();
printf("\nInvalid Choice");
break;
}
printf("\nStart Again? [y/n] ");
scanf(" %c", &startAgain);
}
printf("\n\n");
printf("\n\t\tByeee....!");
return 0;
}