Project Solution: Electricity Bill Calculator

Rumman Ansari   Software Engineer   2024-09-07 02:11:38   106  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Code: ElectricityBill


public class ElectricityBill {

    public static void main(String[] args) {
        // Sample data
        String customerNumber = "hg654";
        String customerName = "Rumman Ansari";
        int oldReading = 2562;
        int currentReading = 2892;
        int RENTAL_AMOUNT = 250;
        int unitsUsed;
        double userAmount = 0;
        double tax;
        double bill;

        // Check if the old reading is greater than the current reading
        if (oldReading > currentReading) {
            System.out.println("Old reading cannot be greater than the current reading");
            System.out.println("Exiting...");
            System.exit(0);
        }

        // Calculate the bill amount
        unitsUsed = currentReading - oldReading;
        if (unitsUsed <= 100) {
            userAmount = unitsUsed * 3.25;
        } else {
            userAmount = 325 + ((unitsUsed - 100) * 4.75);
        }

        tax = userAmount * 11.5 / 100;
        bill = RENTAL_AMOUNT + userAmount + tax;

        // Display the bill details
        System.out.println("\t\tWEST BENGAL BIJLI VITRAN LIMITED ");
        System.out.println("\t\tA GOVT. OF HARYANA UNDERTAKING");
        System.out.println("Customer Number: " + customerNumber);
        System.out.println("Customer Name: " + customerName);
        System.out.println("Old Meter Reading: " + oldReading);
        System.out.println("Current Meter Reading: " + currentReading);
        System.out.println("Total Units Consumed: " + unitsUsed);
        System.out.println("Fixed Rental & Line Maintenance Charges: Rs." + RENTAL_AMOUNT);
        System.out.println("Total usage: Rs." + (int) userAmount);
        System.out.println("Tax (11.5%): Rs." + (int) tax);
        System.out.println("-------------------------------------");
        System.out.println("Total Bill Amount Payable: Rs." + (int) bill);
        System.out.println();
    }
}

Explanation of Corrections:

  1. Program Structure: Ensured the program has a main method that serves as the entry point.
  2. Sample Data: Provided sample customer details and meter readings as variables.
  3. Conditions and Calculations:
    • Validated that the old reading is not greater than the current reading.
    • Calculated unitsUsed, userAmount, tax, and bill correctly.
  4. Printing Statements: Corrected the print statements to ensure they display the correct data.

Now, you can compile and run this program to see the output for the given sample data. You can replace the sample data with actual inputs to generate bills for different customers.

Output


                WEST BENGAL BIJLI VITRAN LIMITED
                A GOVT. OF HARYANA UNDERTAKING
Customer Number: hg654
Customer Name: Rumman Ansari
Old Meter Reading: 2562
Current Meter Reading: 2892
Total Units Consumed: 330
Fixed Rental & Line Maintenance Charges: Rs.250
Total usage: Rs.1417
Tax (11.5%): Rs.163
-------------------------------------
Total Bill Amount Payable: Rs.1830