In this tutorial, you will learn to create an Electricity Bill Generator Using some basic concepts in Java with an easy example.
Before going into this project, Let me explain few basic concepts used in this project. In this project, we used looping statements, switch case, Scanner class from the java. util package.
First Let's see Looping Statements.
In Looping, there are three important and most frequently used Looping statements. They are for loop, if-else loop, while loop.
In this project, I used the if-else loop. Use if to specify a block of code to be executed, If a specified condition is true. Use else to specify a block of code to be executed If a specified condition is false. Use else-if to specify a new condition to test, If the first condition is False.
// Program to find a person who has eligibility to vote
class VoterEigibility
{
public static void main(String[] args)
{
int age=19;
if(age>18) // If the Condition is true, The statements in if block will Execute.
{
System.out.println("Yeah, You are eligible to vote");
}
else // If the Condition is False, Else block will Execute.
{
System.out.println("You are not Eligibile to vote");
}
}
}
Output :
Yeah, You are Eligible to vote
Next, Let's see Switch case Statement.
It is also a Looping Statement. But, this is quite different from others. Use the switch statement to select one of many code blocks to be executed. The switch expression is evaluated once. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed. It has two optional Keywords, they are Break and default.
For Example,
// Program to find the name of the month with its order number
class MonthFinder
{
public static void main(String[] args)
{
int month = 7;
switch(month)
{
case 1:
{
System.out.println("Its January");break;
}
case 2:
{
System.out.println("Its february");break;
}
case 3:
{
System.out.println("Its March");break;
}
case 4:
{
System.out.println("Its April");break;
}
case 5:
{
System.out.println("Its May");break;
}
case 6:
{
System.out.println("Its June");break;
}
case 7:
{
System.out.println("Its July");break;
}
case 8:
{
System.out.println("Its August");break;
}
case 9:
{
System.out.println("Its September");break;
}
case 10:
{
System.out.println("Its October");break;
}
case 11:
{
System.out.println("Its November");break;
}
case 12:
{
System.out.println("Its December");break;
}
}
}
}
Output :
Its July
Next, Let's see an important topic Scanner.
It is a class of java. util package. The Scanner class is used to get User Input, and it is found in java. util package
To use the Scanner class, create an object of the class, and use any of the available methods found in the Scanner class. The methods are nextInt(), nextFloat(), nextLine(), nextByte(), etc...
import java.util.Scanner; // here, we are importing the Scanner Class
class ScannerDemo
{
public static void main(String[] args)
{
int UserInput;
Scanner sc = new Scanner(System.in); // Here, we are creating the object of Scanner Class
System.out.println("Enter Your age");
UserInput = sc.nextInt();
System.out.println("Your age is : "+UserInput);
}
}
Output :
Enter Your age
19
Your age is: 19
As we are completely clear with the basics of this Project, It will be very much easy for you to understand this project.
Before go to the Source code, Let me explain to you the overview of this project.
First, we need user input such as consumer id, consumer name, previous readings, current readings, Type of EB Connection. This can be achieved using the Scanner class. Then, we will use the If-Else loop to choose the type of EB Connection. The price will differ according to the type of Connection and the value of Readings.
For example,
for the first 100 units, 1 rupee per unit. 101-200 units - 2.5 rupees per unit. 201-500 units - 4 rupees per unit. above 501 units - 6 rupees per unit for Domestic Connection.
for the first 100 units, 2 rupee per unit. 101-200 units - 4.5 rupees per unit. 201-500 units - 6 rupees per unit. above 501 units - 7 rupees per unit for Commercial Connection.
Then, we use a switch case. Case 1 will contain the code for Domestic Connection and the Second case will contain the code for Commercial Connection. Inside each case, we have an If-Elseif-Else loop to select the Pricing category according to the current Readings as mention above.
Let's Start the Code
// Program to create Electricity Bill Generator
/*
Rupees per unit for domestic connection
1-100 - 1Rs per unit
101-200 - 2.5Rs per unit
201-500 - 4Rs per unit
501 above - 6Rs per unit
*/
/*
Rupees per unit for commercial connection
1-100 - 2Rs per unit
101-200 - 4.5Rs per unit
201-500 - 6Rs per unit
501 above - 7Rs per unit
*/
import java.util.Scanner;
public class ElectricityBill
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter The Consumer ID : ");
int consumer_no = sc.nextInt();
System.out.println("Enter The Consumer Name : "):
String consumer_name = sc.next();
System.out.println("ENter The Prev Reading : "):
int previous_reading = sc.nextInt();
System.out.println("Enter the Current Reading : ");
int current_reading - sc.nextInt();
System.out.println("ENter The Type of EB connection in Numbers ( 1) Domestic / 2) Commerial ) "):
int connection_type = sc.nextInt();
int choice;
double bill_amount = 0;
if(connection_type == 1)
{
choice = 1;
}
else
{
choice = 2;
}
switch(choice)
{
case 1:
if(current_reading>=0 && current_reading<=100)
{
bill_amount = current_reading*1;
}
else if(current_reading>100 && current_reading<=200)
{
bill_amount = ((100*1)+(current_reading-100)*2.5);
}
else if(current_reading>200 && current_reading<=500)
{
bill_amount = ((100*1)+(100*2.5)+(current_reading-200)*4);
}
else
{
bill_amount = ((100*1)+(100*2.5)+(100*4)+(current_reading-500)*6);
}
break;
case 2:
if(current_reading>=0 && current_reading<=100)
{
bill_amount = current_reading*2;
}
else if(current_reading>100 && current_reading<=200)
{
bill_amount = ((100*2)+(current_reading-100)*4.5);
}
else if(current_reading>200 && current_reading<=500)
{
bill_amount = ((100*2)+(100*4.5)+(current_reading-200)*6);
}
else
{
bill_amount = ((100*2)+(100*4.5)+(100*6)+(current_reading-500)*7);
}
break;
}
String total = Double.toString(bill_amount);
System.out.println("--------------------------------");
System.out.println(" Electricity Bill ");
System.out.println("--------------------------------");
System.out.println("Consumer No : "+consumer_no);
System.out.println("Consumer Name : "+consumer_name);
System.out.println("Previous reading : "+previous_reading);
System.out.println("Current Reading : "+current_reading);
System.out.println("Connection Type : "+connection_type);
System.out.println("*****************************************");
System.out.println("The Bill Amount : "+total);
}
}
Output :
Enter The Consumer ID :
101
Enter The Consumer Name :
Jahn
Enter The Prev Reading :
310
Enter the Current Reading :
480
Enter The Type of EB connection in Numbers ( 1) Domestic / 2) Commercial )
1
--------------------------------------
Electricity Bill
--------------------------------------
Consumer No: 101
Consumer_Name: Jahn
Previous reading: 310
current reading: 480
Connection Type: 1
******************************
The Bill Amount: 1470.0
It is one of the easiest ways of creating an Electricity Bill generator using Java. It doesn't include any complicated methods or keywords or packages. It is a very Beginner Level Project in Java.
Submitted by Praveen Kumar. S (PraveenKumarS)
Download packets of source code on Coders Packet
Comments