-
Notifications
You must be signed in to change notification settings - Fork 69
/
restaurant_bill.c
207 lines (190 loc) · 5.96 KB
/
restaurant_bill.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
// restaurant bill system by Aayush TC
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
struct items
{
char item[20];
float price;
int qty;
};
struct order
{
char costomer[50]; // name of the costomer
char date[50]; //will fetch date form __DATE__
int numOfItems;
struct items itm[50];
};
// function to generate bills
int generateBillHeader(char name[50], char date[30])
{
printf("\n\n");
printf("\t ADV. Restaurant");
printf("\n\t --------------------");
printf("\nDate:%s", date);
printf("\nInvoice to: %s", name);
printf("\n");
printf("---------------------------------------------\n");
printf("Items\t\t");
printf("Qty\t\t");
printf("Total\t\t");
printf("\n---------------------------------------------");
printf("\n\n");
}
int generateBillBody(char item[30], int qty, float price)
{
printf("%s\t\t", item);
printf("%d\t\t", qty);
printf("%.2f\t\t", qty * price);
printf("\n");
}
int generateBillFooter(float total)
{
printf("\n");
float dis = 0.1 * total;
float net_total = total - dis;
float cgst = 0.09 * net_total;
float grand_total = net_total + 2 * cgst;
printf("-------------------------------\n");
printf("Sub Total\t\t\t%.2f", total);
printf("\nDiscount @10%s\t\t\t%.2f", "%", dis);
printf("\n\t\t\t\t-------");
printf("\n Net Total\t\t\t%.2f", net_total);
printf("\n CGST @9\t\t\t%.2f", cgst);
printf("\n SGST @9\t\t\t%.2f", cgst);
printf("\n-------------------------------------");
printf("\nGrand Total\t\t\t%.2f", grand_total);
printf("\n---------------------------------------\n");
}
int main(int argc, char const *argv[])
{
float total;
int opt, n;
struct order ord; // particularly for file write
struct order ord1; // particularly for file read
char savebill = 'y';
char contFlag = 'y';
char name[50];
FILE *fp;
// dashboard
while(contFlag == 'y'){
float total = 0;
int invoiceFound = 0;
system("CLS");
printf("\n\nPlease select ypur prefered option: ");
printf("\n\n\t=============ADV. RESTAURANT=============");
printf("\n\n1. Generate Invoice");
printf("\n2. Show all Invoices");
printf("\n3. Search Invoice");
printf("\n4. Exit");
printf("\n\nPLease select your choice here:\t");
scanf("%d", &opt);
fgetc(stdin); // to remove mixture error of scanf and fgets...
switch (opt)
{
case 1:
system("CLS");
printf("\nPlease enter the name of a costomer:\t");
fgets(ord.costomer, 50, stdin);
ord.costomer[strlen(ord.costomer) - 1] = 0; // to esc extra \n created by scanf
strcpy(ord.date, __DATE__);
printf("\nPlease enter the number of Items:\t");
scanf("%d", &n);
ord.numOfItems = n;
for (int i = 0; i < n; i++)
{
fgetc(stdin);
printf("\n\n");
printf("Please enter the item %d:\t", i + 1);
fgets(ord.itm[i].item, 20, stdin); // items liine jati number halyo teti
ord.itm[i].item[strlen(ord.itm[i].item) - 1] = 0;
printf("Please enter the quantity:\t"); // quantity of particular item
scanf("%d", &ord.itm[i].qty);
printf("Please enter the unit price:\t"); // single price of a particular item
scanf("%f", &ord.itm[i].price);
total += ord.itm[i].qty * ord.itm[i].price;
}
generateBillHeader(ord.costomer, ord.date);
for (int i = 0; i < ord.numOfItems; i++)
{
generateBillBody(ord.itm[i].item, ord.itm[i].qty, ord.itm[i].price);
}
generateBillFooter(total);
printf("\nDO you want to save the invoice [y/n]:\t");
scanf("%s", &savebill);
if (savebill == 'y')
{
fp = fopen("invoices.txt", "a+");
fwrite(&ord, sizeof(struct order), 1, fp);
if (fwrite != 0)
{
printf("\nSuccessfully saved");
}
else
{
printf("\nError saving");
}
fclose(fp);
}
break;
case 2:
system("CLS");
fp = fopen("invoices.txt", "r");
printf("\n\n ****Your Previous Invoices****\n");
while (fread(&ord1, sizeof(struct order), 1, fp))
{
float tot = 0;
generateBillHeader(ord1.costomer, ord1.date);
for (int i = 0; i < ord1.numOfItems; i++)
{
generateBillBody(ord1.itm[i].item, ord1.itm[i].qty, ord1.itm[i].price);
tot += ord1.itm[i].qty * ord1.itm[i].price;
}
generateBillFooter(tot);
}
fclose(fp);
break;
case 3:
system("CLS");
printf("\nPlease Enter the Name of the costomer:\t");
// fgetc(stdin);
fgets(name, 50, stdin);
name[strlen(name) - 1] = 0;
fp = fopen("invoices.txt", "r");
printf("\n ****Invoices Of %s****\n", name);
while (fread(&ord1, sizeof(struct order), 1, fp))
{
float tot = 0;
if (strcmp(ord1.costomer, name) == 0)
{
generateBillHeader(ord1.costomer, ord1.date);
for (int i = 0; i < ord1.numOfItems; i++)
{
generateBillBody(ord1.itm[i].item, ord1.itm[i].qty, ord1.itm[i].price);
tot += ord1.itm[i].qty * ord1.itm[i].price;
}
generateBillFooter(tot);
invoiceFound = 1;
}
}
if (!invoiceFound)
{
printf("\nSorry the invoice for %s doesn't exists", name);
}
fclose(fp);
break;
case 4:
system("CLS");
system("exit");
break;
default:
printf("\nError");
break;
}
printf("\nDo you want to perform another operation [y/n]:\t");
scanf("%s", &contFlag);
}
printf("\n\n");
return 0;
}