Java Program to Generate Electricity Bill

Aim:

To develop a Java application to generate Electricity Bill.

Procedure:

  1. Create a class with the following members
    • Consumer no., consumer name, previous month reading, current month reading, type of EB connection (i.e domestic or commercial)
  2. Compute the bill amount using the following tariff.
  3. If the type of the EB connection is domestic, calculate the amount to be paid as follows:
    • First 100 units – Rs. 1 per unit
    • 101-200 units – Rs. 2.50 per unit
    • 201 -500 units – Rs. 4 per unit
    • > 501 units – Rs. 6 per unit
  4. If the type of the EB connection is commercial, calculate the amount to be paid as follows:
    • First 100 units – Rs. 2 per unit
    • 101-200 units – Rs. 4.50 per unit
    • 201 -500 units – Rs. 6 per unit
    • > 501 units – Rs. 7 per unit
  5. Create the object for the created class .Invoke the methods to get the input from the consumer and display the consumer information with the generated electricity bill.

Program:

import java.util.*;
class ebill
{
public static void main (String args[])
{
customerdata ob = new customerdata();
ob.getdata();
ob.calc();
ob.display();
}
}
class customerdata
{
Scanner in = new Scanner(System.in);
Scanner ins = new Scanner(System.in);
String cname,type;
int bn;
double current,previous,tbill,units;
void getdata()
{
System.out.print ("\n\t Enter consumer number  ");
bn = in.nextInt();
System.out.print ("\n\t Enter Type of connection  (D for Domestic or C for Commercial) ");
type = ins.nextLine();
System.out.print ("\n\t Enter consumer name  ");
cname = ins.nextLine();
System.out.print ("\n\t Enter previous month reading  ");
previous= in.nextDouble();
System.out.print ("\n\t Enter current month reading  ");
current= in.nextDouble();
}
void calc()
{
units=current-previous;
if(type.equals("D"))
{
if (units<=100)
tbill=1 * units;
else if (units>100 && units<=200)
tbill=2.50*units;
else if(units>200 && units<=500)
tbill= 4*units;
else
tbill= 6*units;
}
else 
{
if (units<=100)
tbill= 2 * units;
else if(units>100 && units<=200)
tbill=4.50*units;
else if(units>200 && units<=500)
tbill= 6*units;
else
tbill= 7*units;
}
}
void display()
{
System.out.println("\n\t Consumer number = "+bn);
System.out.println ("\n\t Consumer name = "+cname);
if(type.equals("D"))
System.out.println ("\n\t type of connection  = DOMESTIC ");
else
System.out.println ("\n\t type of connection  = COMMERCIAL ");
System.out.println ("\n\t Current Month  Reading = "+current);
System.out.println ("\n\t Previous Month  Reading = "+previous);
System.out.println ("\n\t Total units = "+units);
System.out.println ("\n\t Total bill = RS "+tbill);
}
}

Output:

Result:

Thus the java program to generate electricity bill was implemented and executed successfully

Leave a Comment