0% found this document useful (0 votes)
7 views5 pages

Exp (2) - 1

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

Exp (2) - 1

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

EXP NO : 2

QUESTION :

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,
and 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 – Free 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 – Free 101-200 units - Rs. 4.50 per unit 201 -500 units - Rs. 6 per unit 501 units -
Rs. 7 per unit

CODE:

import java.util.Scanner;

class ElectricityBill {

private String consumerNo;

private String consumerName;

private double previousReading;

private double currentReading;

private String connectionType;

public ElectricityBill(String consumerNo, String consumerName, double previousReading,


double currentReading, String connectionType) {

this.consumerNo = consumerNo;

this.consumerName = consumerName;
this.previousReading = previousReading;

this.currentReading = currentReading;

this.connectionType = connectionType;

public double calculateBill() {

double unitsConsumed = currentReading - previousReading;

double amount = 0;

if (connectionType.equalsIgnoreCase("domestic")) {

if (unitsConsumed > 500) {

amount += (unitsConsumed - 500) * 6;

unitsConsumed = 500;

if (unitsConsumed > 200) {

amount += (unitsConsumed - 200) * 4;

unitsConsumed = 200;

if (unitsConsumed > 100) {

amount += (unitsConsumed - 100) * 2.5;

} else if (connectionType.equalsIgnoreCase("commercial")) {

if (unitsConsumed > 500) {

amount += (unitsConsumed - 500) * 7;

unitsConsumed = 500;
}

if (unitsConsumed > 200) {

amount += (unitsConsumed - 200) * 6;

unitsConsumed = 200;

if (unitsConsumed > 100) {

amount += (unitsConsumed - 100) * 4.5;

return amount;

public void displayBill() {

System.out.println("Electricity Bill Details:");

System.out.println("Consumer No: " + consumerNo);

System.out.println("Consumer Name: " + consumerName);

System.out.println("Previous Month Reading: " + previousReading + " units");

System.out.println("Current Month Reading: " + currentReading + " units");

System.out.println("Connection Type: " + connectionType);

System.out.printf("Total Bill Amount: Rs. %.2f%n", calculateBill());

public class ElectricityBillApp {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Consumer No: ");

String consumerNo = scanner.nextLine();

System.out.print("Enter Consumer Name: ");

String consumerName = scanner.nextLine();

System.out.print("Enter Previous Month Reading (in units): ");

double previousReading = scanner.nextDouble();

System.out.print("Enter Current Month Reading (in units): ");

double currentReading = scanner.nextDouble();

scanner.nextLine(); // Consume newline

System.out.print("Enter Type of Connection (domestic/commercial): ");

String connectionType = scanner.nextLine();

ElectricityBill bill = new ElectricityBill(consumerNo, consumerName, previousReading,


currentReading, connectionType);

bill.displayBill();

scanner.close();

INPUT :

Enter Consumer No: C123

Enter Consumer Name: John Doe


Enter Previous Month Reading (in units): 150

Enter Current Month Reading (in units): 350

Enter Type of Connection (domestic/commercial): domestic

OUTPUT:

Electricity Bill Details:

Consumer No: C123

Consumer Name: John Doe

Previous Month Reading: 150.0 units

Current Month Reading: 350.0 units

Connection Type: domestic

Total Bill Amount: Rs. 700.00

You might also like