I am almost done with this code but am having a problem when
I am almost done with this code but am having a problem when the code is calculating an kw usage. When I enter:
Southwest Power and Light
Billing Statement
Please enter your name > b,b
Customer name:b,b
Meter reading date > 2/15/16
Meter reading date:2/15/16
Electricity used is > 550
Baseline charge $6.25
Over-baseline charge$6.25
Excess charge$6.25
The total amount due is 6.25Calculate another bill? (Y/N)?:
I\'m not getting the right calculations even though the calculation code is correct. The output should show:
Electricity used is > 550
Baseline charge $35.00
Over-baseline charge $16.50
Excess charge $6.25
The total amount due is $57.75
Calculate another bill? (Y/N)?:
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class Ex2
{
public static void main(String[] args)
{
char choice;
do
{
System.out.println(\"Southwest Power and Light\");
System.out.println(\"Billing Statement\");
//Declaring variables
Scanner scan = new Scanner(System.in);;
String input;
NumberFormat currency = NumberFormat.getCurrencyInstance();
double basicRate = 0;
double overBaselineRate = 0;
double excessRate = 0;
double kwUsed;
double baselineCharge = 0;
double overbaselineCharge = 0;
double excessCharge = 0;
double totalBill;
//Entering Name
System.out.print(\"Please enter your name > \");
input = scan.nextLine();
System.out.println(\"Customer name:\"+ input );
// Entering Meter Date
SimpleDateFormat sdf = new SimpleDateFormat(\"MM/dd/yyyy\");
String bDate = null;
boolean valid = false;
while (valid == false) {
try {
Scanner scandate = new Scanner(System.in);
System.out.print(\"Meter reading date > \");
bDate = scandate.nextLine();
sdf.parse(bDate);
valid = true;
Scanner scandate2 = new Scanner(bDate);
scandate2.useDelimiter(\"/\");
// Month Charges
int date = scandate2.nextInt();
if (date == 12 || date == 1 || date == 2) {
basicRate = .10;
overBaselineRate = basicRate * 1.10;
excessRate = basicRate * 1.25;
} else if (date >= 3 && date < 6) {
basicRate = .12;
overBaselineRate = basicRate * 1.10;
excessRate = basicRate * 1.25;
} else
basicRate = .15;
overBaselineRate = basicRate * 1.10;
excessRate = basicRate * 1.25;
} catch (Exception error) {
System.out.print(\"Date entered is not valid try again!\");
}
}
System.out.println(\"Meter reading date:\"+ bDate );
System.out.print(\"Electricity used is > \" );
kwUsed = scan.nextDouble();
//System.out.print(basicRate + \" \" + overBaselineRate + \" \" + excessRate );
// calculations
if (kwUsed >= 0 && kwUsed <= 350)
{
baselineCharge = kwUsed*basicRate;
//System.out.print(\"The Baseline is \");
//System.out.printf(\"%.2f\ \",basicRate);
}
else if (kwUsed >= 351 && kwUsed <= 500)
{
overbaselineCharge = overBaselineRate * (kwUsed-350);
//System.out.print(\"The overbaselineCharge is: \\t \"+ overbaselineCharge);
}
else if (kwUsed > 500)
{
excessCharge = excessRate * (kwUsed-500);
}
String s = currency.format(baselineCharge + overbaselineCharge + excessCharge);
System.out.print(\"\ \" + \"Baseline charge \" + s + \"\ \" + \"Over-baseline charge\" + s + \"\ \"+ \"Excess charge\");
System.out.print(s);
//Total bill and Calculating another bill
totalBill= (baselineCharge + overbaselineCharge + excessCharge);
System.out.print(\" \ The total amount due is \");
System.out.printf(\"%.2f\", totalBill);
scan = new Scanner (System.in);
System.out.printf(\"Calculate another bill? (Y/N)?: \");
choice=scan.next().charAt(0);
}while((choice!=\'n\')&&(choice!=\'N\'));
System.out.println(\"Thank you for letting us serve you!\");
}
}
Solution
The rest of the code is correct, please change the following in the calculations:
// calculations
 
 if (kwUsed >= 0 && kwUsed <= 350)
 
 {
 
 baselineCharge = kwUsed*basicRate;
 
 //System.out.print(\"The Baseline is \");
 
 //System.out.printf(\"%.2f\ \",basicRate);
 
 }
 
 else if (kwUsed >= 351 && kwUsed <= 500)
 
 {
baselineCharge=350*basicRate;
 overbaselineCharge = overBaselineRate * (kwUsed-350);
 
 //System.out.print(\"The overbaselineCharge is: \\t \"+ overbaselineCharge);
 
 }
 
 else if (kwUsed > 500)
 
 {
 baselineCharge=350*basicRate;
 overbaselineCharge=150*overBaselineRate;
 excessCharge = excessRate * (kwUsed-500);
 
 
 }
 
 String s = currency.format(baselineCharge + overbaselineCharge + excessCharge);
 
 
 
 System.out.print(\"\ \" + \"Baseline charge \" + baselineCharge + \"\ \" + \"Over-baseline charge\" + overbaselineCharge + \"\ \"+ \"Excess charge\");
 
 
 System.out.print(excessCharge);
 
 //Total bill and Calculating another bill
 
 totalBill= (baselineCharge + overbaselineCharge + excessCharge);
 
 System.out.print(\" \ The total amount due is \");
 
 System.out.printf(\"%.2f\", totalBill);
 
 scan = new Scanner (System.in);
 
 System.out.printf(\"Calculate another bill? (Y/N)?: \");
 
 choice=scan.next().charAt(0);
 
 }while((choice!=\'n\')&&(choice!=\'N\'));
 
 System.out.println(\"Thank you for letting us serve you!\");





