Java Programming | Create a Electricity Bill

Introduction

This is a tutorial for creating a electricity bill in java. The program is given below that calculates bill according to given conditions. The program is extendable. Go enjoy the program. Lets begin……

Instructions / Conditions

Take the units consumed
Accordingly calculate amt
for 1st 100 units @ 1Rs.(any currency)/unit
for next 100 units @ 2 Rs./unit
for next 100 units @ 3 Rs./unit
for next 200 units @ 4 Rs./unit
for next units @ 5 Rs./unit
tax to be added in final amount @ 10%.
Meter charge 50 Rs. extra.
Print the bill….

Program for electricity bill in java.


//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public
public class ElectBill {
//void main
public static void main (String[] args)
{
//declare float
float uc,amt,famt,tax;

//Declare input as scanner
Scanner input = new Scanner(System.in);

//Take inputs
System.out.println("Enter units consumed:");
uc = input.nextFloat();

//calculate
if(uc>=0 && uc<=100)
amt=uc*1;
else
{
if(uc>100 && uc<=200)
amt=((uc-100)*2)+100;
else
{
if(uc>200 && uc<=300)
amt=((uc-200)*3)+300;
else
{
if(uc>300 && uc<=500)
amt=((uc-300)*4)+600;
else
amt=((uc-500)*5)+1400;
}
}
}

//add tax.
tax=(amt*10)/100;
famt=amt+50+tax;

//print
System.out.println("-----------------------");
System.out.println("Electricity Bill");
System.out.println("-----------------------");
System.out.println("Units Charge "+amt);
System.out.println("Meter Charge  50");
System.out.println("Tax          "+tax);
System.out.println("-----------------------");
System.out.println("Total        "+famt);
System.out.println("-----------------------");
}
}

Output

Enter units consumed:
650
———————–
Electricity Bill
———————–
Units Charge 2150.0
Meter Charge 50
Tax 215.0
———————–
Total 2415.0
———————–

How it works

  1. The program prints the message to enter units consumed.
  2. The user enters units.
  3. The bill is calculated and printed.

Extending it

The program can be extended by using more conditions. Go extend it.

Explanation.

  1. Import the Scanner.
  2. Declare the class as public
  3. Add the void main function
  4. Add system.out.println() function with the message to enter units consumed.
  5. Declare input as Scanner.
  6. Take the inputs and save it in variables.
  7. Calculate bill and save it in variable.
  8. Add system.out.println() function to print the bill.

At the end.

You learnt creating the Java program for Creating a Electricity Bill. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Sharing options: