Electricity Bill Generator Project In C++

Electricity Bill Generator Project In C++(Source Code)

Electricity Bill Generator Project In C++(Source Code)

Electricity Bill Generator Project In C++

 

Introduction

Hello, learners. Today we will create a new project in C++. An electricity bill generator. The main concepts that we will be using are structures and files of C++.

In this electricity bill generator, We can define the parameters of the bill, store the customer details, and many more.

Let’s discuss the User Interface of our project.

User Interface and Functioning.

As we can see in the main picture above introduction, in this project we offer four choices, 

  • Adding a customer
  • Generating a Bill
  • Display the bill
  • Delete the Bill

When we run the file, we are asked for the username and password. 

The username is “admin” and the password is 12345. It is given in the code.

Let’s see each functionality.

1)  Adding a new customer

Before we generate the bill, we need a few necessary details. 

Name of the customer, price of each unit used by them.

Once we generate those, the system assigns a customer id unique from other users.

2) Bill Generation

Now, we have a customer database with their ID and the price of each unit.

Using these details, we enter the total units spent in this option which then calculates the bill internally.

The user is shown a message that the bill is generated if all the parameters are given correctly.

We also assign a billing ID for each customer.

3) Display the bill

From options 1 & 2, we enter the customer ID and the respective Billing ID to display the details of the Bill.

In this we display,

  • The Bill ID
  • Name
  • Customer ID
  • Price per unit
  • Units Consumed
  • Total Amount

4) Delete the Bill

 

Once the user pays the bill we can delete the bill and generate a new bill with updated details when needed.

We have seen the User Interface and functioning of the project. Let us see the code part and its explanation.

Code

1:
2: #include<stdlib.h>
3: #include<stdio.h>
4: #include<conio.h>
5: #include<string.h>
6:
7: struct customer
8: {
9: char c_name[20];
10: int c_id;
11: float price;
12: };
13:
14: struct bill
15: {
16: int b_id;
17: int cid;
18: char name[20];
19: float price;
20: int unit;
21: };
22:
23: int customer_id()
24: {
25: int a;
26: FILE *fp;
27: fp=fopen("cus.txt","r");
28: if(fp==NULL)
29: a=1;
30: else
31: {
32: fscanf(fp,"%d",&a);
33: a++;
34: return a;
35: }
36: fclose(fp);
37:
38: fp=fopen("cus.txt","w");
39: fprintf(fp,"%d",a);
40: fclose(fp);
41:
42: }
43:
44: int bill_id()
45: {
46: int y;
47: FILE *fp;
48: fp=fopen("bi.txt","r");
49: if(fp==NULL)
50: y=1;
51: else
52: {
53: fscanf(fp,"%d",&y);
54: y++;
55: }
56: fclose(fp);
57:
58: fp=fopen("bi.txt","w");
59: fprintf(fp,"%d",y);
60: fclose(fp);
61: return y;
62: }
63:
64: void add_customer()
65: {
66: FILE *ptr;
67: int i;
68: struct customer c;
69: ptr=fopen("cust.txt","a");
70: fflush(stdin);
71: printf("n Enter customer details :- n Name - ");
72: gets(c.c_name);
73: fflush(stdin);
74:
75: printf("n Price of each unit - ");
76: scanf("%f",&c.price);
77: c.c_id=customer_id();
78:
79: printf("n Record added successfully. ");
80: printf("n Your customer id is %d",c.c_id);
81:
82: fwrite(&c,sizeof(c),1,ptr);
83: fclose(ptr);
84: }
85:
86: void generate_bill()
87: {
88: int find,flag=0;
89: FILE *ptr,*ptr1;
90: struct customer c;
91: struct bill b;
92: printf("n Enter your customer id. ");
93: scanf("%d",&find);
94:
95: ptr=fopen("cust.txt","r");
96: while(!feof(ptr))
97: {
98: fread(&c,sizeof(c),1,ptr);
99: if(c.c_id==find)
100: { flag=1;
101: ptr1=fopen("bill1.txt","a");
102: b.b_id=bill_id();
103: strcpy(b.name,c.c_name);
104: b.price=c.price;
105: b.cid=c.c_id;
106: printf("n Enter number of unit consumed in this month. ");
107: scanf("%d",&b.unit);
108: fflush(stdin);
109: printf("n Bill has been generated successfully. ");
110: printf("n Your bill id is %d",b.b_id);
111: fwrite(&b,sizeof(b),1,ptr1);
112: fclose(ptr1);
113: break;
114: }
115: }
116: if(flag==0)
117: printf("n Error! No such customer with id no. %d exist.",find);
118: fclose(ptr);
119: }
120:
121: void display_bill()
122: {
123: int flag=0,billid,custid;
124: FILE *ptr1;
125: struct bill b;
126: ptr1=fopen("bill1.txt","r");
127: printf("n Enter your customer id. ");
128: scanf("%d",&custid);
129: fflush(stdin);
130: printf("n Enter bill id. whose bill you want to display. ");
131: scanf("%d",&billid);
132: while(!feof(ptr1))
133: { fflush(stdin);
134:
135: fread(&b,sizeof(b),1,ptr1);
136: if((b.b_id==billid)&&(b.cid==custid))
137: {
138: flag++;
139:
140: printf("n Electricity Bill");
141: printf("n Bill id :- %d",b.b_id);
142: printf("n Name :- %s",b.name);
143: printf("n Customer id :- %d",b.cid);
144: printf("n Price per unit :- %f",b.price);
145: printf("n Unit consumed :- %d",b.unit);
146: printf("n Total amount :- %f",b.price*b.unit);
147: break;
148: }
149: }
150: if(flag==0)
151: printf("n Error! No such customer id no. %d OR bill no. %d exist.",custid,billid);
152: fclose(ptr1);
153: }
154:
155: void delete_bill()
156: {
157:
158: int flag=0,billid,custid;
159: FILE *ptr,*ptr1;
160: struct bill b;
161:
162: ptr=fopen("bill1.txt","r");
163: ptr1=fopen("temp.txt","w");
164: printf("n Enter bill id. whose bill you want to delete. ");
165: scanf("%d",&billid);
166: while(!feof(ptr))
167: {
168: fread(&b,sizeof(b),1,ptr);
169: if(b.b_id==billid)
170: {
171: printf("n Bill with id no. %d DELETED successfully.",b.b_id);
172: flag=1;
173: }
174: else
175: fwrite(&b,sizeof(b),1,ptr1);
176: }
177:
178: fclose(ptr);
179: fclose(ptr1);
180: if(flag==0)
181: printf("n Error! No such bill with id no. %d exist.",billid);
182: remove("bill1.txt");
183: rename("temp.txt","bill1.txt");
184: }
185:
186: main()
187: {
188:
189: char username[20];
190: char password[20];
191: printf("please enter the username:");
192: scanf("%s",username);
193: printf("please enter the password: ");
194: scanf("%s",password);
195: if(strcmp(username,"admin")==0)
196: {
197: if(strcmp(password,"12345")==0)
198: {
199: printf("nwelcome _____login successfull");
200: }
201: else
202: {
203: printf("n invalid password");
204: exit(0);
205: }
206: }
207: else
208: {
209: printf("n username is invalid");
210: exit(0);
211: }
212: int choice=1;
213: while(choice!=5)
214: {
215:
216: printf("n ************************************** ");
217: printf("n Electricity Bill Calculator");
218: printf("n ************************************** ");
219:
220:
221: printf("n MAIN MENU ");
222: printf("n 1. Add new customer ");
223: printf("n 2. Bill Generation ");
224: printf("n 3. Display bill ");
225: printf("n 4. Delete bill ");
226: printf("n 5. EXIT ");
227: printf("n Enter your choice ");
228: scanf("%d",&choice);
229: switch(choice)
230: {
231: case 1: add_customer();
232: break;
233: case 2: generate_bill();
234: break;
235: case 3: display_bill();
236: break;
237: case 4: delete_bill();
238: break;
239: case 5: break;
240: default:{
241: printf("nn Invalid Choice...!");
242: printf("nn Press any key to continue..");
243: getch();
244: }
245: }
246: }
247: }
248:
249:

Code Explanation

 

So, the code starts with the creation of a structure containing customer and bill-related variables

ADVERTISEMENT

We store the customer details, bill details, bill ID, and customer ID in separate files. 

ADVERTISEMENT

For those, we create functions to generate the IDs when we need them. Two of those functions are

ADVERTISEMENT

  • int customer_id()
  • int bill_id()

Both are similar in working. It goes like we check if the file content is NULL and if it is, initialize with a default value.

ADVERTISEMENT

else, increment it every time it is called.

ADVERTISEMENT

Let’s get into the main functions of the interface.

1) void add_customer()

 

We store the details of the customer in a file named “cust.txt”. Then we use the structure customer to get the variables and access them for storing respective information.

Here, we call the customer_id() function and assign it to every customer.

2) void generate_bill()

To generate the customer’s bill, we use the customer ID. Then, we open the file and search for the ID entered. 

If we find it, we use the structure bill to access the variables and store the data, just like the add_customer() function.

The bill id is the same as the customer id. We also declare a flag variable, which prints an error message when the id number is not found.

3) void display_bill()

 

We take the bill ID and customer ID from the user and then compare them with the id values in the bill and customer structure.

If both are true, we will then the information we took from the add_customer() and generate_bill(). 

We also give the total bill amount.

We use the flag variable here as well, to check the existence of ids.

4) void delete_bill()

 

We delete the contents of the file whose id matches the bill ID we entered.

If the deletion is done successfully, we print a success message.

In the main() function, we provide the user with a login experience. The username is admin with the password 12345.

We provide the interface only when the username and password match.

The last part of the code is the switch-case part of the main() function, which consists of the different options of interface with the respective function call.

Conclusion

With this project, we can learn the file concept and how to apply the structures too. 

We have a lot more C++ projects coming right in your way. We slowly proceed to advanced projects but, in a way that beginners can understand as well.

 

If you have enjoyed the article and learned something new today, let us know in the comments. Check out some more articles here. 

 

100+ Front-end Projects for Web developers (Source Code)

Happy Reading 🙂



Leave a Reply