0% found this document useful (0 votes)
3 views2 pages

Java Lab Exercises

This Java program calculates electricity bills based on consumer input for previous and current readings, consumer type (domestic or commercial), and applies different rates for each type. It prompts the user for necessary details, computes the total amount based on the units consumed, and displays the bill information. The program includes error handling for invalid connection types.

Uploaded by

leelarani
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)
3 views2 pages

Java Lab Exercises

This Java program calculates electricity bills based on consumer input for previous and current readings, consumer type (domestic or commercial), and applies different rates for each type. It prompts the user for necessary details, computes the total amount based on the units consumed, and displays the bill information. The program includes error handling for invalid connection types.

Uploaded by

leelarani
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/ 2

1.

import java.util.Scanner;

public class SimpleEBBill {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input
System.out.print("Enter Consumer Number: ");
int no = sc.nextInt();
sc.nextLine(); // clear buffer

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


String name = sc.nextLine();

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


int prev = sc.nextInt();

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


int curr = sc.nextInt();
sc.nextLine(); // clear buffer

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


String type = sc.nextLine().toLowerCase();

int units = curr - prev;


double amount = 0;

// Bill Calculation
if (type.equals("domestic")) {
if (units <= 100)
amount = units * 1;
else if (units <= 200)
amount = 100 * 1 + (units - 100) * 2.5;
else if (units <= 500)
amount = 100 * 1 + 100 * 2.5 + (units - 200) * 4;
else
amount = 100 * 1 + 100 * 2.5 + 300 * 4 + (units - 500) * 6;
} else if (type.equals("commercial")) {
if (units <= 100)
amount = units * 2;
else if (units <= 200)
amount = 100 * 2 + (units - 100) * 4.5;
else if (units <= 500)
amount = 100 * 2 + 100 * 4.5 + (units - 200) * 6;
else
amount = 100 * 2 + 100 * 4.5 + 300 * 6 + (units - 500) * 7;
} else {
System.out.println("Invalid Connection Type!");
return;
}

// Output
System.out.println("\n--- Electricity Bill ---");
System.out.println("Consumer No : " + no);
System.out.println("Name : " + name);
System.out.println("Connection : " + type);
System.out.println("Units Used : " + units);
System.out.println("Total Amount : ₹" + amount);
}
}

You might also like