0% found this document useful (0 votes)
10 views6 pages

Project Final Term PF

The document is a C++ program for an Electricity Bill Calculator that manages customer records and generates bills. It includes functionalities to add customers, generate bills, display bills, and delete bills, while maintaining customer and bill IDs in text files. The program requires admin login for access and presents a menu-driven interface for user interaction.

Uploaded by

usman ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Project Final Term PF

The document is a C++ program for an Electricity Bill Calculator that manages customer records and generates bills. It includes functionalities to add customers, generate bills, display bills, and delete bills, while maintaining customer and bill IDs in text files. The program requires admin login for access and presents a menu-driven interface for user interaction.

Uploaded by

usman ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <iostream>

#include <fstream>
#include <cstring>

using namespace std;

struct Customer {
char c_name[20];
int c_id;
float price;
};

struct Bill {
int b_id;
int cid;
char name[20];
float price;
int unit;
};

int customer_id() {
int a;
ifstream file("cus.txt");
if (!file) {
a = 1;
} else {
file >> a;
a++;
file.close();
return a;
}
file.close();

ofstream outfile("cus.txt");
outfile << a;
outfile.close();

return a;
}

int bill_id() {
int y;
ifstream file("bi.txt");
if (!file) {
y = 1;
} else {
file >> y;
y++;
file.close();
return y;
}
file.close();

ofstream outfile("bi.txt");
outfile << y;
outfile.close();

return y;
}

void add_customer() {
ofstream file("cust.txt", ios::app);
Customer c;
cout << "\nEnter customer details:\nName: ";
cin.getline(c.c_name, 20);

cout << "Price of each unit: ";


cin >> c.price;
cin.ignore();

c.c_id = customer_id();

cout << "\nRecord added successfully.\nYour customer id is " << c.c_id;

file.write(reinterpret_cast<char*>(&c), sizeof(c));
file.close();
}

void generate_bill() {
int find, flag = 0;
ifstream ptr("cust.txt");
ofstream ptr1("bill1.txt", ios::app);
Customer c;
Bill b;

cout << "\nEnter your customer id: ";


cin >> find;
while (ptr.read(reinterpret_cast<char*>(&c), sizeof(c))) {
if (c.c_id == find) {
flag = 1;
b.b_id = bill_id();
strcpy(b.name, c.c_name);
b.price = c.price;
b.cid = c.c_id;

cout << "\nEnter number of units consumed in this month: ";


cin >> b.unit;

cout << "\nBill has been generated successfully.\nYour bill id is " << b.b_id;

ptr1.write(reinterpret_cast<char*>(&b), sizeof(b));
break;
}
}

if (flag == 0) {
cout << "\nError! No such customer with id no. " << find << " exists.";
}

ptr.close();
ptr1.close();
}

void display_bill() {
int flag = 0, billid, custid;
ifstream ptr1("bill1.txt");
Bill b;

cout << "\nEnter your customer id: ";


cin >> custid;

cout << "\nEnter bill id whose bill you want to display: ";
cin >> billid;

while (ptr1.read(reinterpret_cast<char*>(&b), sizeof(b))) {


if ((b.b_id == billid) && (b.cid == custid)) {
flag++;
cout << "\n Electricity Bill" << endl;
cout << " Bill id :- " << b.b_id << endl;
cout << " Name :- " << b.name << endl;
cout << " Customer id :- " << b.cid << endl;
cout << " Price per unit :- " << b.price << endl;
cout << " Unit consumed :- " << b.unit << endl;
cout << " Total amount :- " << b.price * b.unit << endl;
break;
}
}

if (flag == 0) {
cout << "\nError! No such customer id no. " << custid << " OR bill no. " << billid << "
exists.";
}

ptr1.close();
}

void delete_bill() {
int flag = 0, billid;
ifstream ptr("bill1.txt");
ofstream temp("temp.txt");
Bill b;

cout << "\nEnter bill id whose bill you want to delete: ";
cin >> billid;

while (ptr.read(reinterpret_cast<char*>(&b), sizeof(b))) {


if (b.b_id == billid) {
cout << "\nBill with id no. " << b.b_id << " deleted successfully.";
flag = 1;
} else {
temp.write(reinterpret_cast<char*>(&b), sizeof(b));
}
}

ptr.close();
temp.close();

if (flag == 0) {
cout << "\nError! No such bill with id no. " << billid << " exists.";
}

remove("bill1.txt");
rename("temp.txt", "bill1.txt");
}
int main() {
char username[20], password[20];

cout << "Please enter the username: ";


cin >> username;
cout << "Please enter the password: ";
cin >> password;

if (strcmp(username, "admin") == 0) {
if (strcmp(password, "12345") == 0) {
cout << "\nWelcome _____ Login successful";
} else {
cout << "\nInvalid password";
exit(0);
}
} else {
cout << "\nUsername is invalid";
exit(0);
}

int choice = 1;
while (choice != 5) {
cout << "\n**************************************" << endl;
cout << " Electricity Bill Calculator" << endl;
cout << "**************************************" << endl;

cout << "\nMAIN MENU" << endl;


cout << "1. Add new customer" << endl;
cout << "2. Bill Generation" << endl;
cout << "3. Display bill" << endl;
cout << "4. Delete bill" << endl;
cout << "5. EXIT" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cin.ignore();
add_customer();
break;
case 2:
generate_bill();
break;
case 3:
display_bill();
break;
case 4:
delete_bill();
break;
case 5:
break;
default:
cout << "\nInvalid Choice...!";
cout << "\nPress any key to continue..";
cin.ignore();
break;
}
}

return 0;
}

You might also like