-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomerBillingSystem.c
300 lines (264 loc) · 6.16 KB
/
CustomerBillingSystem.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
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#include<dir.h>
#include<dos.h>
#include<math.h>
#define RECORD "CustomerRecord.dat"
typedef struct
{
int customerNo;
long long int phoneNo;
char name[28];
char address[36];
float balance;
} Customer;
void setColor(int ForgC)
{
WORD wColor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi; //We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F); //Mask out all but the background attribute, and add in the forgournd color
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
void input(Customer *x)
{
fflush(stdin);
printf("\n\tEnter the name of Customer: ");
gets(x->name);
printf("\tEnter the Address : ");
gets(x->address); fflush(stdin);
printf("\tEnter the Phone No. : ");
scanf("%lld", &x->phoneNo);
printf("\tEnter the Amount to be Paid : ");
scanf("%f", &x->balance);
x->balance = -x->balance;
}
void output(Customer x, float amt)
{
printf("\n\tCustomer Name :"); setColor(14); printf(" %s\t", x.name); setColor(15);
printf("\n\tCustomer No. :"); setColor(11); printf(" %d\t", x.customerNo); setColor(15);
printf("\n\tCustomer Phone No. : %lld\t", x.phoneNo);
printf("\n\tCustomer Address : %s\t", x.address);
printf("\n\tBill Amount :"); setColor(10); printf(" %.2f\t", amt - x.balance); setColor(15);
printf("\n\tAmount Paid :"); setColor(10); printf(" %.2f\t",amt); setColor(15);
printf("\n\tRemaining Balance:"); setColor(12); printf(" %.2f\t", x.balance); setColor(15);
}
Customer setCustomerNo()
{
FILE *fp;
Customer x;
int k, totalRecords;
fp = fopen(RECORD, "rb+");
fseek(fp, 0, SEEK_END);
totalRecords = ftell(fp) / sizeof(Customer);
if(totalRecords == 1)
{
rewind(fp);
fread(&x, sizeof(Customer), 1, fp);
x.customerNo = 1;
fseek(fp, -sizeof(Customer), SEEK_END);
fwrite(&x, sizeof(Customer), 1 , fp);
}
else
{
fseek(fp, -2 * sizeof(Customer), SEEK_END);
fread(&x, sizeof(Customer), 1, fp);
k = x.customerNo + 1;
fread(&x, sizeof(Customer), 1, fp);
x.customerNo = k;
fseek(fp, -sizeof(Customer), SEEK_END);
fwrite(&x, sizeof(Customer), 1, fp);
}
fclose(fp);
return x;
}
void update(Customer x)
{
FILE *fp; Customer y;
fp = fopen(RECORD, "rb+");
while(fread(&x, sizeof(Customer), 1, fp))
{
if(y.customerNo == x.customerNo)
{
fseek(fp, -sizeof(Customer), SEEK_CUR);
fwrite(&x, sizeof(Customer), 1, fp);
break;
}
}
fclose(fp);
}
void writefile(Customer x)
{
FILE *fp;
fp = fopen("CustomerRecord.dat", "ab");
fwrite(&x, sizeof(Customer), 1, fp);
fclose(fp);
}
Customer searchByName(char *name)
{
FILE *fp;
fp = fopen(RECORD, "rb");
Customer x;
while(fread(&x, sizeof(Customer), 1, fp))
{
if(strcmp(x.name, name) == 0)
{
printf("\n\tCustomer Record Found \n");
fclose(fp);
return x;
}
}
printf("\n\tRecord not Found \n");
x.customerNo = -1;
fclose(fp);
return x;
}
Customer searchByNo(int n)
{
FILE *fp;
fp = fopen(RECORD, "rb");
Customer x;
while(fread(&x, sizeof(Customer), 1, fp))
{
if(x.customerNo == n)
{
printf("\n\t Customer Record Found \n");
fclose(fp);
return x;
}
}
printf("\tRecord not Found");
x.customerNo = -1;
fclose(fp);
return x;
}
Customer match(Customer x)
{
FILE *fp; Customer y; char choice;
fflush(stdin);
printf("\n\n\tDo you want to EDIT Customer Details ? (Y/N) : ");
scanf("%c", &choice);
if(choice == 'Y' || choice == 'y')
{
fflush(stdin);
printf("\n\t Enter Name : ");
gets(x.name);
printf("\n\t Enter Address : ");
gets(x.address);
printf("\n\t Enter Phone No. (Enter Zero '0' in Case of No Change): ");
scanf("%lld", &x.phoneNo);
fp = fopen(RECORD, "rb+");
while(fread(&y, sizeof(Customer), 1, fp))
{
if(x.customerNo == y.customerNo)
{
if(strcmp(x.address, y.address) != 0 && strlen(x.address))
strcpy(y.address, x.address);
if(strcmp(x.name, y.name) != 0 && strlen(x.name))
strcpy(y.name, x.name);
if((x.phoneNo != y.phoneNo) && x.phoneNo > 999999999)
y.phoneNo = x.phoneNo;
fseek(fp, -sizeof(Customer), SEEK_CUR);
fwrite(&y, sizeof(Customer), 1, fp);
break;
}
}
fclose(fp);
}
else
y = x;
return y;
}
Customer search(Customer x)
{
int choice;
printf("\n\tEnter 1 to Search By Name \n\tEnter 2 to Search by Customer No.");
printf("\n\n\tEnter Choice : ");
scanf("%d", &choice);
if(choice == 1)
{
printf("\tEnter the Name : ");
fflush(stdin); gets(x.name);
x = searchByName(x.name);
}
else if(choice == 2)
{
printf("\tEnter the Customer No. : ");
scanf("%d", &x.customerNo);
x = searchByNo(x.customerNo);
}
else
{
printf("\tWRONG CHOICE. Try Again!");
search(x);
}
return x;
}
float payBill(Customer *x)
{
char c; float amt = 0.0; fflush(stdin);
printf("\tDo you want to pay the bill? (Y/N) : ");
scanf("%c", &c);
if(c == 'Y' || c == 'y')
{
printf("\tEnter Amount Paid : ");
scanf("%f", &amt);
x->balance = amt + x->balance;
}
else
x->balance = x->balance - amt;
return amt;
}
void displayMenu()
{
system("cls"); //clrscr() is deprecated. So we are using cls command with the system() function to clear screen initially.
printf("\n\t\tCOPYRIGHT 2019 : DEVELOPED BY KALPADIPTYA ROY\n");
printf("\n\t\t\tWELCOME TO THE CUSTOMER BILLING SYSTEM\n\n");
printf("\n\t\tEnter 1 to Add Account");
printf("\n\t\tEnter 2 to Search Account");
printf("\n\t\tEnter 3 to Exit");
}
void run()
{
int choice; Customer x; float amt;
x.balance = 0; x.phoneNo = 0;
printf("\n\n \t\t\tEnter your Choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1: input(&x);
amt = payBill(&x);
writefile(x);
x = setCustomerNo();
output(x, amt);
run();
break;
case 2: x = search(x);
if(x.customerNo != -1)
{
amt = payBill(&x);
update(x);
x = match(x);
output(x, amt);
}
run();
break;
case 3: break;
default: printf("WRONG CHOICE !"); run();
}
}
int main()
{
displayMenu();
run();
printf("\n\n \tThank you for using the our Billing System! \n\n");
getch();
return 0;
}