-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
523 lines (455 loc) · 13.4 KB
/
code.c
File metadata and controls
523 lines (455 loc) · 13.4 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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100
/* ---------- STRUCT ---------- */
struct Vehicle {
char id[10];
char type[20];
char model[30];
char brand[30];
int year;
int engineCC;
int seatCount;
int gearCount;
char routeNumber[20];
int bateryCapacity;
int chargingTime;
int loadCapacity;
};
// GLOBAL DATA STORE
struct Vehicle vehicles[MAX];
struct Vehicle backup[MAX];
int count = 0;
int backupCount = 0;
int autoID = 1;
int canUndo = 0;
// FUNCTION DECLARATIONS
void showMenu();
void refreshScreen();
void calculateServiceCost(struct Vehicle vehicles[], int count);
/* core */
void addNewVehicle();
void showVehicles();
void searchVehicle();
void updateVehicle();
void deleteVehicle();
void filterVehicles();
/* advanced */
void cloneVehicle();
void duplicateDetection();
void statisticsReport();
void sortVehicles();
void bulkDeleteOld();
void undoLast();
/* helpers */
void toLowerCase(char str[]);
void clearInputBuffer();
void generateID(char id[]);
int findIndexByID(char id[]);
void saveBackup();
// MAIN FUNCTION
int main() {
int choice;
while (1) {
showMenu();
printf("Enter choice: ");
if (scanf("%d", &choice) != 1) {
clearInputBuffer();
continue;
}
clearInputBuffer();
switch (choice) {
case 1: saveBackup(); addNewVehicle(); refreshScreen(); break;
case 2: showVehicles(); break;
case 3: searchVehicle(); break;
case 4: saveBackup(); updateVehicle(); refreshScreen(); break;
case 5: saveBackup(); deleteVehicle(); refreshScreen(); break;
case 6: filterVehicles(); break;
case 7: sortVehicles(); refreshScreen(); break;
case 8: duplicateDetection(); break;
case 9: saveBackup(); cloneVehicle(); refreshScreen(); break;
case 10: statisticsReport(); break;
case 11: saveBackup(); bulkDeleteOld(); refreshScreen(); break;
case 12: undoLast(); break;
case 13: calculateServiceCost(vehicles, count); break;
case 0:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice!\n");
}
}
}
/* ---------- UI ---------- */
void showMenu() {
printf("\n===== VEHICLE MANAGEMENT SYSTEM =====\n");
printf("1. Add Vehicle\n");
printf("2. Show All Vehicles\n");
printf("3. Search Vehicle\n");
printf("4. Update Vehicle\n");
printf("5. Delete Vehicle\n");
printf("6. Filter Vehicles\n");
printf("7. Sort by Year (Newest)\n");
printf("8. Duplicate Detection\n");
printf("9. Clone Vehicle\n");
printf("10. Statistics Report\n");
printf("11. Bulk Delete (Old under 2010)\n");
printf("12. Undo Last Action\n");
printf("13. Calculate Service Cost\n");
printf("0. Exit\n");
}
/* UPDATED refreshScreen */
void refreshScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
printf("\n--- Current Vehicles ---\n");
showVehicles();
}
/* ---------- BACKUP / UNDO ---------- */
void saveBackup() {
backupCount = count;
for (int i = 0; i < count; i++) {
backup[i] = vehicles[i];
}
canUndo = 1;
}
void undoLast() {
if (!canUndo) {
printf("Nothing to undo!\n");
return;
}
count = backupCount;
for (int i = 0; i < backupCount; i++)
vehicles[i] = backup[i];
autoID = count + 1;
canUndo = 0;
printf("Undo successful!\n");
refreshScreen();
}
// HELPERS FUNCTIONS
void generateID(char id[]) {
sprintf(id, "V%03d", autoID++);
}
int findIndexByID(char id[]) {
for (int i = 0; i < count; i++)
if (strcmp(vehicles[i].id, id) == 0)
return i;
return -1;
}
void toLowerCase(char str[]) {
for (int i = 0; str[i]; i++)
str[i] = tolower(str[i]);
}
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
// ADD NEW VEHICLE
void addNewVehicle() {
struct Vehicle v;
if (count >= MAX) {
printf("Limit reached!\n");
return;
}
v.seatCount = 0;
v.gearCount = 0;
v.bateryCapacity = 0;
v.chargingTime = 0;
v.loadCapacity = 0;
strcpy(v.routeNumber, "N/A");
generateID(v.id);
printf("Generated ID: %s\n", v.id);
/* Type validation */
printf("Type (car/bike/bus/truck/electric_car): ");
scanf("%19s", v.type);
clearInputBuffer();
toLowerCase(v.type);
if (strcmp(v.type,"car")!=0 &&
strcmp(v.type,"bike")!=0 &&
strcmp(v.type,"bus")!=0 &&
strcmp(v.type,"truck")!=0 &&
strcmp(v.type,"electric_car")!=0) {
printf("Invalid vehicle type!\n");
return;
}
printf("Model: ");
scanf(" %[^\n]", v.model);
clearInputBuffer();
printf("Brand: ");
scanf(" %[^\n]", v.brand);
clearInputBuffer();
printf("Year: ");
scanf("%d", &v.year);
clearInputBuffer();
printf("Engine CC: ");
scanf("%d", &v.engineCC);
clearInputBuffer();
if (strcmp(v.type, "car") == 0) {
printf("Seat Count: ");
scanf("%d", &v.seatCount);
clearInputBuffer();
}
else if (strcmp(v.type, "bike") == 0) {
printf("Gear Count: ");
scanf("%d", &v.gearCount);
clearInputBuffer();
}
else if (strcmp(v.type, "bus") == 0) {
printf("Seat Count: ");
scanf("%d", &v.seatCount);
clearInputBuffer();
printf("Route Number: ");
scanf(" %[^\n]", v.routeNumber);
clearInputBuffer();
}
else if (strcmp(v.type, "truck") == 0) {
printf("Load Capacity (in tons): ");
scanf("%d", &v.loadCapacity);
clearInputBuffer();
}
else if (strcmp(v.type, "electric_car") == 0) {
printf("Enter charging time (in hours):");
scanf(" %d",&v.chargingTime);
clearInputBuffer();
printf("Enter batery capacity: (in kWh):");
scanf(" %d",&v.bateryCapacity);
clearInputBuffer();
}
vehicles[count++] = v;
printf("Vehicle added successfully!\n");
}
/* ---------- SHOW ---------- */
void showVehicles() {
if (count == 0) {
printf("No vehicles available.\n");
return;
}
for (int i = 0; i < count; i++) {
printf("[%s] %s %s %s (%d)\n",
vehicles[i].id,
vehicles[i].type,
vehicles[i].brand,
vehicles[i].model,
vehicles[i].year);
if (strcmp(vehicles[i].type, "car") == 0) {
printf(" Seat Count: %d\n", vehicles[i].seatCount);
} else if (strcmp(vehicles[i].type, "bike") == 0) {
printf(" Gear Count: %d\n", vehicles[i].gearCount);
} else if (strcmp(vehicles[i].type, "bus") == 0) {
printf(" Route Number: %s\n", vehicles[i].routeNumber);
} else if (strcmp(vehicles[i].type, "electric_car") == 0) {
printf(" Batery Capacity: %d\n", vehicles[i].bateryCapacity);
printf(" Charging Time: %d\n", vehicles[i].chargingTime);
}
else if (strcmp(vehicles[i].type, "truck") == 0) {
printf(" Load Capacity: %d tons\n", vehicles[i].loadCapacity);
}
}
}
/* ---------- SEARCH ---------- */
void searchVehicle() {
char key[30];
printf("Search keyword(like model): ");
scanf(" %[^\n]", key);
clearInputBuffer();
toLowerCase(key);
int found = 0;
for (int i = 0; i < count; i++) {
char m[31];
strcpy(m, vehicles[i].model);
toLowerCase(m);
if (strstr(m, key)) {
printf("%s | %s\n", vehicles[i].id, vehicles[i].model);
found = 1;
}
}
if (!found)
printf("No matching vehicle found.\n");
}
/* ---------- UPDATE (REPLACED) ---------- */
void updateVehicle() {
char id[10];
int ch;
printf("Enter Vehicle ID: ");
scanf("%9s", id);
clearInputBuffer();
int idx = findIndexByID(id);
if (idx == -1) {
printf("Vehicle not found!\n");
return;
}
printf("\nUpdate Menu:\n");
printf("1. Brand\n");
printf("2. Model\n");
printf("3. Year\n");
printf("4. Engine CC\n");
printf("Choice: ");
scanf("%d", &ch);
clearInputBuffer();
switch (ch) {
case 1:
printf("New Brand: ");
scanf(" %[^\n]", vehicles[idx].brand);
break;
case 2:
printf("New Model: ");
scanf(" %[^\n]", vehicles[idx].model);
break;
case 3:
printf("New Year: ");
scanf("%d", &vehicles[idx].year);
break;
case 4:
printf("New Engine CC: ");
scanf("%d", &vehicles[idx].engineCC);
break;
default:
printf("Invalid choice!\n");
return;
}
clearInputBuffer();
printf("Updated successfully!\n");
}
// DELETE VEHICLE
void deleteVehicle() {
char id[10];
printf("Enter ID: ");
scanf("%s", id);
clearInputBuffer();
int idx = findIndexByID(id);
if (idx == -1) {
printf("Vehicle not found!\n");
return;
}
for (int i = idx; i < count - 1; i++)
vehicles[i] = vehicles[i + 1];
count--;
printf("Deleted successfully!\n");
}
// FILTER VEHICLES
void filterVehicles() {
char type[20];
printf("Filter by type: ");
scanf("%19s", type);
clearInputBuffer();
toLowerCase(type);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(vehicles[i].type, type) == 0) {
printf("%s | %s | %s\n",
vehicles[i].id,
vehicles[i].brand,
vehicles[i].model);
found = 1;
}
}
if (!found)
printf("No vehicles found for this type.\n");
}
/* ---------- CLONE ---------- */
void cloneVehicle() {
if (count >= MAX) {
printf("Limit reached! Cannot clone.\n");
return;
}
char id[10];
printf("Enter ID to clone: ");
scanf("%s", id);
clearInputBuffer();
int idx = findIndexByID(id);
if (idx == -1) {
printf("Vehicle not found!\n");
return;
}
vehicles[count] = vehicles[idx];
generateID(vehicles[count].id);
count++;
printf("Vehicle cloned successfully!\n");
}
/* ---------- DUPLICATE ---------- */
void duplicateDetection() {
int found = 0;
for (int i = 0; i < count; i++) {
for (int j = i + 1; j < count; j++) {
char model1[30], model2[30];
strcpy(model1, vehicles[i].model);
strcpy(model2, vehicles[j].model);
toLowerCase(model1);
toLowerCase(model2);
if (strcmp(model1, model2) == 0) {
printf("Duplicate Model: %s (%s & %s)\n",
vehicles[i].model,
vehicles[i].id,
vehicles[j].id);
found = 1;
}
}
}
if (!found)
printf("No duplicates found.\n");
}
/* ---------- SORT ---------- */
void sortVehicles() {
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (vehicles[j].year < vehicles[j + 1].year) {
struct Vehicle temp = vehicles[j];
vehicles[j] = vehicles[j + 1];
vehicles[j + 1] = temp;
}
}
}
printf("Sorted successfully.\n");
}
/* ---------- STATISTICS ---------- */
void statisticsReport() {
int car = 0, bike = 0, bus = 0, truck = 0, electric_car = 0;
for (int i = 0; i < count; i++) {
if (strcmp(vehicles[i].type, "car") == 0) car++;
else if (strcmp(vehicles[i].type, "bike") == 0) bike++;
else if (strcmp(vehicles[i].type, "bus") == 0) bus++;
else if (strcmp(vehicles[i].type, "truck") == 0) truck++;
else if (strcmp(vehicles[i].type, "electric_car") == 0) electric_car++;
}
printf("Total: %d | Cars: %d | Bikes: %d | Buses: %d | Trucks: %d | Electric Cars: %d\n",
count, car, bike, bus, truck, electric_car);
}
/* ---------- BULK DELETE ---------- */
void bulkDeleteOld() {
for (int i = 0; i < count;) {
if (vehicles[i].year < 2010) {
for (int j = i; j < count - 1; j++)
vehicles[j] = vehicles[j + 1];
count--;
} else {
i++;
}
}
printf("Old vehicles deleted.\n");
}
void calculateServiceCost(struct Vehicle vehicles[], int count) {
for (int i = 0; i < count; i++) {
float cost = 0.0;
if (strcmp(vehicles[i].type, "car") == 0) {
cost = vehicles[i].engineCC * 0.1;
cost += vehicles[i].seatCount * 5;
} else if (strcmp(vehicles[i].type, "bike") == 0) {
cost = vehicles[i].engineCC * 0.05;
cost += vehicles[i].gearCount * 1;
} else if (strcmp(vehicles[i].type, "bus") == 0) {
cost = vehicles[i].engineCC * 0.2;
cost += vehicles[i].seatCount * 0.2;
} else if (strcmp(vehicles[i].type, "truck") == 0) {
cost = vehicles[i].engineCC * 0.25;
} else if (strcmp(vehicles[i].type, "electric_car") == 0) {
cost = vehicles[i].bateryCapacity * 0.15;
cost += vehicles[i].chargingTime * 0.25;
}
printf("Service cost for %s (%s): $%.2f\n", vehicles[i].id, vehicles[i].type, cost);
}
}