Algorithm
Step 1 Start the process
Step 2 Get the user informations [Name, Consumer Number, Reading’s of previous and current
month, Connection Type]
Step 3 Compute units consumed by user [Current Month Reading – Previous Month Reading]
Step 4 If a connection type is domestic goto step 6
Step 5 Else goto step 7
Step 6 Initialize i with 1 and sum as 0
Step 6.1 If check i is less than or equal to 100 then compute sum = sum + 1 and goto
step 6.5
Step 6.2 Else if check i is greater than 100 and less than 200 then compute sum = sum
+ 2.5 and goto step 6.5
Step 6.3 Else if check i is greater than 200 and less than 500 then compute sum = sum
+ 4 and goto step 6.5
Step 6.4 Else compute sum = sum + 6 and goto step 6.5
Step 6.5
Step 7
If i is equal to number of units consumed goto step 8 else return to same step
Initialize i with 1 and sum as 0
Step 7.1 If check i is less than or equal to 100 then compute sum = sum + 2 and goto
step 7.5
Step 7.2 Else if check i is greater than 100 and less than 200 then compute sum = sum
+ 4.5 and goto step 7.5
Step 7.3 Else if check i is greater than 200 and less than 500 then compute sum = sum
+ 6 and goto step 7.5
Step 7.4 Else compute sum = sum + 7 and goto step 7.5
Step 7.5
If i is equal to number of units consumed goto step 8 else return to same step
Step 8 Store the sum
Step 9 Display Bill Details [Name, Consumer Number, No of units consumed]
Step 10 Check the connection type
Step 10.1 If connection type is domestic display domestic tariff slot and goto step 11
Step 10.1 Else display commercial tariff slot and goto step 11
Step 11 Display amount payable using stored sum
Step 12 Stop the Process
Program:
import java.util.Scanner;
class EBConsumer {
int consumer_number;
String consumer_name;
int previous_month_reading;
int current_month_reading;
int units_consumed;
boolean isDomestic = false;
double bill_ammount;
public void displayDomesticFares(){
System.out.println("Domestic Fare Details");
System.out.println("*********************");
System.out.println("First 100 units - Rs. 1 per unit");
System.out.println("101-200 units - Rs. 2.50 per unit");
System.out.println("201 -500 units - Rs. 4 per unit");
System.out.println("> 501 units - Rs. 6 per unit");
}
public void displayCommercialFare() {
System.out.println("Commercial Fare Details");
System.out.println("***********************");
System.out.println("First 100 units - Rs. 2 per unit");
System.out.println("101-200 units - Rs. 4.50 per unit");
System.out.println("201 -500 units - Rs. 6 per unit");
System.out.println("> 501 units - Rs. 7 per unit");
}
public void getDetails() {
Scanner inputs = new Scanner(System.in);
System.out.println("Welcome To EB Calculater\n\n");
System.out.println("Please Enter Your Name : ");
this.consumer_name = inputs.next();
System.out.println("Please Enter Your Consumer Number : ");
this.consumer_number = inputs.nextInt();
System.out.println("Please Enter Your Previous Month Reading : ");
this.previous_month_reading = inputs.nextInt();
System.out.println("Please Enter Your Current Month Reading : ");
this.current_month_reading = inputs.nextInt();
System.out.println("Is this domestic Connection (yes/no) : ");
if(inputs.next().equals("yes"))
this.isDomestic = true;
}
public void generateBill(){
int number_of_units_consumed = this.current_month_reading - this.previous_month_reading;
this.units_consumed = number_of_units_consumed;
double sum = 0;
if(isDomestic == true) {
for (int i = 0; i <= number_of_units_consumed; i++) {
if (i <= 100)
sum = sum + 1;
else if (i > 100 && i <= 200)
sum = sum + 2.5;
else if (i > 200 && i <= 500)
sum = sum + 4;
else
sum = sum + 6;
}
}
else {
for (int i = 0; i <= number_of_units_consumed; i++) {
if (i <= 100)
sum = sum + 2;
else if (i > 100 && i <= 200)
sum = sum + 4.5;
else if (i > 200 && i <= 500)
sum = sum + 6;
else
sum = sum + 7;
}
}
this.bill_ammount = sum;
}
public void displayBill() {
generateBill();
System.out.println("The EB Bill Details");
System.out.println("*******************");
System.out.println("Consumer Number : "+this.consumer_number);
System.out.println("Consumer Name : "+this.consumer_name);
System.out.println("Consumer Units Consumed:"+this.units_consumed);
if(this.isDomestic == true)
System.out.println("Your are an Domestic Consumer\nFare Details ...");
else
System.out.println("You are a Commercial Consumer\nFare Details ...");
System.out.println("\nAmount Payable is \u20B9: "+this.bill_ammount);
}
}
public class Main {
public static void main(String[] args) {
EBConsumer consumer = new EBConsumer();
consumer.getDetails();
consumer.displayBill();
}
}
Output:
Welcome To EB Calculater
Please Enter Your Name :
msl
Please Enter Your Consumer Number :
123
Please Enter Your Previous Month Reading :
22222
Please Enter Your Current Month Reading :
23456
Is this domestic Connection (yes/no) :
yes
The EB Bill Details
*******************
Consumer Number : 123
Consumer Name : msl
Consumer Units Consumed:1234
Your are an Domestic Consumer
Fare Details ...
Amount Payable is ₹: 5955.0