Electricity bill program in Java
In this article, you will learn a Java program to calculate electricity bills based on some conditions using if-else statements. The electricity consumption charge is easy to understand and calculate. It is measured in kWh (kilowatt hours). The rate of electricity bills varies depending on the geographic location and the amount of consumption. Suppose we have the following conditions for rates of electricity calculation-
- First 1 to 100 units– Rs. 8/unit
- Next 100 to 200 units– Rs. 12/unit
- Next 200 to 300 units– Rs. 16/unit
- Above 300 units– Rs. 20/unit
We can apply the following logic step by step to calculate the electricity bill based on the above conditions.
- Step1: If the units of consumption are less than or equal to 100, then the total electricity bill will be:
Total Electricity Bill = units * 8
- Step2: If the units of consumption are less than or equal to 200, then the total electricity bill will be:
Total Electricity Bill = 100 * 8 + (units - 100) * 12
- Step3: Else If the units of consumption are less than or equal to 300, then the total electricity bill will be:
Total Electricity Bill = 100 * 8 + 100 * 12 + (units - 200) * 16
- Step4: Else If the units consumption is greater than 300, then the total electricity bill will be:
Total Electricity Bill = 100 * 8 + 100 * 12 + 100 * 16 + (units - 300) * 20
The given Java program is the implementation of the above logic.
// Java program to calculate the
// electricity bill
import java.util.*;
public class CalculateElectricityBill {
// defining a function to calculate the
// electricity bill
// as per unit cost
public static int calculateBill(int units)
{
int first_unit_cost = 8;
int second_unit_cost = 12;
int third_unit_cost = 16;
int fourth_unit_cost = 20;
if (units <= 100) {
return units * first_unit_cost;
}
else if (units <= 200) {
return (100 * first_unit_cost)
+ (units - 100)
* second_unit_cost;
}
else if (units <= 300) {
return (100 * first_unit_cost)
+ (100 * second_unit_cost)
+ (units - 200)
* third_unit_cost;
}
else if (units > 300) {
return (100 * first_unit_cost)
+ (100 * second_unit_cost)
+ (100 * third_unit_cost)
+ (units - 300)
* fourth_unit_cost;
}
return 0;
}
// Driver Code
public static void main(String args[])
{
int units = 250;
System.out.println("Total amount of "+ units +" units: ");
System.out.println(
calculateBill(units));
}
}
Output of the above code:
Total amount of 250 units:
2800
Related Articles
Java program to find area of triangleArea of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number