Electricity Bill Calculation using Java - CODEMUB

Introduction


Electricity bills play a crucial role in our lives as they provide insights into our power consumption and energy usage patterns. In this blog, we will explore how to calculate electricity bills using Java, which is a widely-used and versatile programming language. By developing a simple program, we can gain hands-on experience with Java and understand the logic behind electricity bill calculations.

Concepts Used in Java

Before diving into the code, let's briefly discuss some of the Java concepts that are employed in our program:

  • Scanner Class: This class is utilized for taking input from the user.
  • Variables: Variables are used to store and manipulate data such as consumer number, name, and unit readings.
  • Switch Statement: The switch statement is employed to handle different connection types, whether domestic or commercial.
  • Conditional Statements (if-else): These statements are used to determine the rate per unit and calculate the total amount based on the consumed units.

Logic for Calculating Bill

The logic behind calculating an electricity bill can be summarized as follows:
  • Input Data: Collect consumer details including consumer number, name, and current and previous month readings.
  • Calculate Units Consumed: Find the difference between the current and previous readings to determine the units consumed.
  • Connection Type: Identify whether the connection is domestic or commercial.
  • Rate Per Unit: Based on the consumed units and connection type, determine the rate per unit.
  • Calculate Amount: Multiply the consumed units by the rate per unit to compute the total amount payable.

Coding

Let's take a look at the Java code that implements the aforementioned logic:


import java.util.Scanner;

public class CurrentBill {

    public static void main(String args[]) {

        int unit, previous_reading, current_reading, connectionType = 0;
        String cons_number, cons_name;
        double amount = 0;
        double perunit = 0;

        Scanner input = new Scanner(System.in);

        System.out.println("\n\n");

        System.out.print("Enter Consumer Number : ");
        cons_number = input.next();

        System.out.print("Enter Consumer Name : ");
        cons_name = input.next();

        System.out.print("Enter Previous month reading : ");
        previous_reading = input.nextInt();

        System.out.print("Enter Current month reading : ");
        current_reading = input.nextInt();

        unit = current_reading - previous_reading;

        System.out.print("Enter Connection Type 1.Domestic 2.Commercial : ");
        connectionType = input.nextInt();

        switch (connectionType) {

            case 1:
                if (unit < 100) {
                    perunit = 1;
                    amount = unit * perunit;
                } else if (unit > 100 && unit < 200) {
                    perunit = 2.50;
                    amount = 100 + (unit - 100) * perunit;
                } else if (unit > 200 && unit < 500) {
                    perunit = 4;
                    amount = 350 + (unit - 200) * perunit;
                } else if (unit > 500) {
                    perunit = 6;
                    amount = 1550 + (unit - 500) * perunit;
                }
                break;

            case 2:
                if (unit < 100) {
                    perunit = 2;
                    amount = unit * perunit;
                } else if (unit > 100 && unit < 200) {
                    perunit = 4;
                    amount = 200 + (unit - 100) * perunit;
                } else if (unit > 200 && unit < 500) {
                    perunit = 6;
                    amount = 600 + (unit - 200) * perunit;
                } else if (unit > 500) {
                    perunit = 7;
                    amount = 2400 + (unit - 500) * perunit;
                }
                break;

            default:
                System.out.println("Invalid Connection Type");
                System.exit(0); // Terminate the program if an invalid connection type is entered
        }

        System.out.println("\nElectricity Bill");
        System.out.println("Consumer Number: " + cons_number);
        System.out.println("Consumer Name: " + cons_name);
        System.out.println("Units Consumed: " + unit);
        System.out.println("Connection Type: " + (connectionType == 1 ? "Domestic" : "Commercial"));
        System.out.println("Amount Payable: Rs. " + amount);

    }
}

In this code, we use the Scanner class to take user input for consumer details and readings. The program then calculates the consumed units and uses a switch statement to handle different connection types. Based on the consumed units and connection type, it calculates the rate per unit and the total amount payable.


Output

The program displays detailed information about the electricity bill, including the consumer number, name, consumed units, connection type, and the amount payable. This output provides a clear overview of the user's electricity consumption and associated costs.

In conclusion, this blog explored the process of calculating an electricity bill using Java. We discussed the key concepts used in Java programming, explained the logic behind the code, provided a complete Java program, and highlighted the expected output. By applying programming languages like Java to real-world problems, we can enhance our programming skills and gain a deeper understanding of both programming and the domains in which we apply it

Input


Enter Consumer Number : 1426
Enter Consumer Name : CodeMub
Enter Previous month reading : 500
Enter Current month reading : 700
Enter Connection Type 1.Domestic 2.Commercial : 1

Output

Electricity Bill
Consumer Number: 1426
Consumer Name: CodeMub
Units Consumed: 200
Connection Type: Domestic
Amount Payable: Rs. 450.0
Next Post Previous Post
No Comment
Add Comment
comment url