Program Code:
public class EBBill {
int consumerNo;
String consumerName;
double previousMonthlyReading;
double currentMonthReading;
String typeOfConnection;
double currentMonthUsage;
double billAmount;
EBBill(int consumerNo,String consumerName,double previousMonthlyReading,double currentMonthReading,String typeOfConnection){
this.consumerName=consumerName;
this.consumerNo=consumerNo;
this.previousMonthlyReading = previousMonthlyReading;
this.currentMonthReading=currentMonthReading;
this.typeOfConnection=typeOfConnection;
currentMonthUsage = currentMonthReading - previousMonthlyReading;
}
public void computeBill(){
switch (typeOfConnection) {
case "C":
case "c":
billAmount=computeCommercial();
break;
case "D":
case "d":
billAmount= computeDomestic();
break;
default:
break;
}
}
private double computeDomestic() {
if(currentMonthUsage <=100 ){
return 1 * currentMonthUsage;
}else if(currentMonthUsage <=200){
return 2.50 * currentMonthUsage;
}else if(currentMonthUsage <=500){
return 4* currentMonthUsage;
}else{
return 6*currentMonthUsage;
}
}
private double computeCommercial() {
if(currentMonthUsage <=100 ){
return 2 * currentMonthUsage;
}else if(currentMonthUsage <=200){
return 4.50 * currentMonthUsage;
}else if(currentMonthUsage <=500){
return 6* currentMonthUsage;
}else{
return 7*currentMonthUsage;
}
}
public static void main(String[] args) {
if(args.length!=5){
System.out.println("There is no/ missing arguments to program.. \n Please Run the program as follows");
System.out.println("java EBBill consumerNo consumerName PrevMonthReading CurMonthReading C/D");
}else{
EBBill ebBill=new EBBill(Integer.parseInt(args[0]),args[1],Double.parseDouble(args[2]),Double.parseDouble(args[3]),args[4]);
ebBill.computeBill();
System.out.println("EB Bill");
System.out.println("*******");
System.out.println("Consumer Number :"+ebBill.consumerNo);
System.out.println("Consumer Name :"+ebBill.consumerName);
System.out.println("Previous Month Reading :"+ebBill.previousMonthlyReading);
System.out.println("Current Month Reading :"+ebBill.currentMonthReading);
System.out.println("Connection Type :"+ebBill.typeOfConnection);
System.out.println("Amount to be paid :"+ebBill.billAmount);
}
}
}
Program Execution: