-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBillingSystem.c
78 lines (70 loc) · 2.17 KB
/
BillingSystem.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
#include <stdio.h>
#include <string.h>
#define MAX_ITEMS 50
struct Item {
char name[50];
int quantity;
float price;
};
void calculate_bill(struct Item items[], int num_items, float *subtotal, float *tax, float *total) {
*subtotal = 0;
for (int i = 0; i < num_items; i++) {
*subtotal += items[i].quantity * items[i].price;
}
*tax = *subtotal * 0.08; // Assuming 8% tax
*total = *subtotal + *tax;
}
void print_bill(struct Item items[], int num_items, float subtotal, float tax, float total) {
printf("** Bill **\n\n");
printf("Items\t\tQuantity\tPrice\tTotal Price\n");
printf("------\t\t--------\t-------\t------------\n");
for (int i = 0; i < num_items; i++) {
printf("%-25s\t%d\t%.2f\t%.2f\n", items[i].name, items[i].quantity, items[i].price, items[i].quantity * items[i].price);
}
printf("Subtotal:\t\t\t%.2f\n", subtotal);
printf("Tax (8%%):\t\t\t%.2f\n", tax);
printf("Total:\t\t\t\t%.2f\n", total);
}
int main() {
struct Item items[MAX_ITEMS];
int num_items = 0;
char choice;
do {
printf("\nBilling System\n");
printf("1. Add Item\n");
printf("2. Generate Bill\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case '1':
if (num_items == MAX_ITEMS) {
printf("Maximum number of items reached!\n");
} else {
printf("Enter item name: ");
scanf(" %s", items[num_items].name);
printf("Enter quantity: ");
scanf("%d", &items[num_items].quantity);
printf("Enter price: ");
scanf("%f", &items[num_items].price);
num_items++;
}
break;
case '2':
if (num_items == 0) {
printf("No items added yet!\n");
} else {
float subtotal, tax, total;
calculate_bill(items, num_items, &subtotal, &tax, &total);
print_bill(items, num_items, subtotal, tax, total);
}
break;
case '3':
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != '3');
return 0;
}