Electricity Bill Calculator Prgram in JAVA
Objective: Develop a Java application to generate Electricity bill. 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). Compute the bill amount using the following tariff. 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
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
import java.util.Scanner;
class ElectricityBill {
private int consumerNo;
private String consumerName;
private int prevReading;
private int currReading;
private String ebType;
public ElectricityBill(int consumerNo, String consumerName, int prevReading, int currReading, String ebType) {
this.consumerNo = consumerNo;
this.consumerName = consumerName;
this.prevReading = prevReading;
this.currReading = currReading;
this.ebType = ebType;
}
public double calculateBill() {
int unitsConsumed = currReading - prevReading;
double amount = 0;
if (ebType.equalsIgnoreCase("domestic")) {
if (unitsConsumed <= 100)
amount = unitsConsumed * 1.0;
else if (unitsConsumed <= 200)
amount = 100 * 1.0 + (unitsConsumed - 100) * 2.50;
else if (unitsConsumed <= 500)
amount = 100 * 1.0 + 100 * 2.50 + (unitsConsumed - 200) * 4.0;
else
amount = 100 * 1.0 + 100 * 2.50 + 300 * 4.0 + (unitsConsumed - 500) * 6.0;
}
else if (ebType.equalsIgnoreCase("commercial")) {
if (unitsConsumed <= 100)
amount = unitsConsumed * 2.0;
else if (unitsConsumed <= 200)
amount = 100 * 2.0 + (unitsConsumed - 100) * 4.50;
else if (unitsConsumed <= 500)
amount = 100 * 2.0 + 100 * 4.50 + (unitsConsumed - 200) * 6.0;
else
amount = 100 * 2.0 + 100 * 4.50 + 300 * 6.0 + (unitsConsumed - 500) * 7.0;
} else {
System.out.println("Invalid connection type!");
}
return amount;
}
public void displayBill() {
System.out.println("\nElectricity Bill");
System.out.println("Consumer Number: " + consumerNo);
System.out.println("Consumer Name: " + consumerName);
System.out.println("Units Consumed: " + (currReading - prevReading));
System.out.println("Type of Connection: " + ebType);
System.out.println("Bill Amount: Rs. " + calculateBill());
}
}
public class ElectricityBillCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Consumer Number: ");
int consumerNo = sc.nextInt();
sc.nextLine();
System.out.print("Enter Consumer Name: ");
String consumerName = sc.nextLine();
System.out.print("Enter Previous Month Reading: ");
int prevReading = sc.nextInt();
System.out.print("Enter Current Month Reading: ");
int currReading = sc.nextInt();
sc.nextLine();
System.out.print("Enter Type of EB Connection (Domestic/Commercial): ");
String ebType = sc.nextLine();
ElectricityBill bill = new ElectricityBill(consumerNo, consumerName, prevReading, currReading, ebType);
bill.displayBill();
sc.close();
}
}