Java Program to Calculate Electricity Bill

In the previous article, we have seen Java Program to Calculate Discounted Price

In this article we are going to see how to calculate electricity bill using java programming language.

Java Program to Calculate Electricity Bill

The charge for electricity bill is categorized into different brackets. The more you consume, the more price/unit you will pay.

For the sake of this tutorial consider the following price break-up.

  • For unit less than 50, it is Rs.2.5/unit
  • For unit 51 to 100, it is Rs.4.10/unit
  • For unit 101 to 250, it is Rs.4.7/unit
  • For unit greater than 250, it is Rs.5.10/unit

Let’s see different ways to calculate total electricity bill.

Method-1: Java Program to Calculate Electricity Bill By Using User Input

Approach:

  • Create Scanner class object.
  • Take user input for total units consumed.
  • Calculate the bill price as per the price break-up shown above using if-else statements
  • Print the bill price.

Program:

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // prompt user to enter total units consumed
        System.out.print("Enter total units consumed: ");
        double totalUnitsConsumed = sc.nextDouble();

        //double variable 'cost' declared and initialized to 0
        //this will hold total bill price
        double cost = 0;
        // calculate bill amount
        if (totalUnitsConsumed < 50)
            cost = totalUnitsConsumed * 2.5;
        else if (totalUnitsConsumed < 100)
            cost = 50 * 2.5 + (totalUnitsConsumed - 50) * 4.1;
        else if (totalUnitsConsumed < 250)
            cost = 50 * 2.5 + (totalUnitsConsumed - 50) * 4.1 + (totalUnitsConsumed - 100) * 4.7;
        else
            cost = 50 * 2.5 + (totalUnitsConsumed - 50) * 4.1 + (totalUnitsConsumed - 100) * 4.7
                    + (totalUnitsConsumed - 250) * 5.1;
        // display bill amount
        System.out.println("Bill amount is Rs." + cost);
    }
}
Output:

Enter total units consumed: 206
Bill amount is Rs.1262.8

Method-2: Java Program to Calculate Electricity Bill By Using User Defined Method

Approach:

  • Same approach as the previous method but using a user defined method to calculate the bill price.

Program:

import java.util.Scanner;

class Main 
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // prompt user to enter total units consumed
        System.out.print("Enter total units consumed: ");
        double totalUnitsConsumed = sc.nextDouble();

        //double variable 'cost' declared and initialized to 0
        //this will hold total bill price
        double cost = 0;
        // calculate bill amount by calling the method calcPrice()
        cost = calcPrice(totalUnitsConsumed);
        // display bill amount
        System.out.println("Bill amount is " + cost);
    }
     
    //user defined method calcPrice() to find total bill price based on unit
    private static double calcPrice(double totalUnitsConsumed) 
    {
        double cost;
        if (totalUnitsConsumed < 50)
            cost = totalUnitsConsumed * 2.5;
        else if (totalUnitsConsumed < 100)
            cost = 50 * 2.5 + (totalUnitsConsumed - 50) * 4.1;
        else if (totalUnitsConsumed < 250)
            cost = 50 * 2.5 + (totalUnitsConsumed - 50) * 4.1 + (totalUnitsConsumed - 100) * 4.7;
        else
            cost = 50 * 2.5 + (totalUnitsConsumed - 50) * 4.1 + (totalUnitsConsumed - 100) * 4.7
                    + (totalUnitsConsumed - 250) * 5.1;
        return cost;
    }
}
Output:

Enter total units consumed: 210
Bill amount is 1298.0

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: