Question

Java code is calculating the bill wrong and not displaying currency at the end: I've highlighted...

Java code is calculating the bill wrong and not displaying currency at the end: I've highlighted the error in the cosole

Southwest Power and Light

Billing Statement

Please enter your name > J, P

Customer name:J, P

Meter reading date > 2/15/16

Meter reading date:2/15/16

Electricity used: >

225

The basic rate is:22.5

The totalBill is 225.0

Calculate another bill? (Y/N)?:

The correct answer should be baseline charge: $22.50 and total bill: $22.50

The Charges are based on these:

0kw - 350kw: basic rate

351kw - 500kw : 10% above basic rate
500kw > : 25% above basic rate

dates:
Basic rate Charges/ Varies by months       
$0.10 from 12/1 to 2/29
$0.12 from 3/1 to 5/31
$0.15 from 6/1 to 11/30  

import java.text.NumberFormat;

import java.text.SimpleDateFormat;

import java.util.Scanner;

import static javafx.scene.input.KeyCode.N;

import static javafx.scene.input.KeyCode.Y;

public class SouthwestPowerandLight

{

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;

double basicRate = 0.10;

double basicrateTwo = 0.12;

double basicrateThree = 0.15;

double kwUsed;

double overbaselineCharge = 0.10;

double excessCharge = 0.25;

double totalBill;

  

//Initialize variables

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;

else if (date >= 3 && date < 6)

basicrateTwo = .12;

else

basicrateThree = .15;

} catch (Exception error) {

System.out.println("Date entered is not valid try again!");

}

}

System.out.println("Meter reading date:"+ bDate );

  

  

System.out.println("Electricity used: > ");

kwUsed = scan.nextDouble();

  

if (kwUsed >= 0 && kwUsed <= 350)

{

basicRate = kwUsed*0.1;

System.out.println("The basic rate is:"+basicRate);

}

else if (kwUsed >= 351 && kwUsed <= 500)

{

overbaselineCharge = overbaselineCharge * (kwUsed-350);

System.out.println("The overbaselineCharge is:"+ overbaselineCharge +basicRate);

}

else if (kwUsed > 500)

{

excessCharge = excessCharge * (kwUsed-500);

System.out.println("The excessCharge is:"+ excessCharge);

}

  

  

//Get template for formatting

NumberFormat currency = NumberFormat.getCurrencyInstance();      

totalBill= (kwUsed);

System.out.println("The totalBill is " + totalBill);

scan = new Scanner (System.in);

  

System.out.print("Calculate another bill? (Y/N)?: ");

choice=scan.next().charAt(0);

}while((choice!='n')&&(choice!='N'));

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

copyable code

Corrected code

import java.text.NumberFormat;

import java.text.SimpleDateFormat;

import java.util.Scanner;

public class SouthwestPowerandLight

{

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;

double basicRate = 0.10;

double basicrateTwo = 0.12;

double basicrateThree = 0.15;

double kwUsed;

double overbaselineCharge = 0.10;

double excessCharge = 0.25;

double totalBill;

//Initialize variables

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;

else if (date >= 3 && date < 6)

basicrateTwo = .12;

else

basicrateThree = .15;

} catch (Exception error) {

System.out.println("Date entered is not valid try again!");

}

}

System.out.println("Meter reading date:"+ bDate );

System.out.println("Electricity used: > ");

kwUsed = scan.nextDouble();

if (kwUsed >= 0 && kwUsed <= 350)

{

basicRate = kwUsed*0.1;

System.out.println("the basic rate is\n");

System.out.printf("%.2f\n",basicRate);

}

else if (kwUsed >= 351 && kwUsed <= 500)

{

overbaselineCharge = overbaselineCharge * (kwUsed-350);

System.out.println("The overbaselineCharge is:"+ overbaselineCharge +basicRate);

}

else if (kwUsed > 500)

{

excessCharge = excessCharge * (kwUsed-500);

System.out.println("The excessCharge is:"+ excessCharge);

}

//Get template for formatting

NumberFormat currency = NumberFormat.getCurrencyInstance();     

totalBill= (basicRate);

System.out.printf(" the total bill is\n");

System.out.printf("%.2f\n", totalBill);

scan = new Scanner (System.in);

System.out.print("Calculate another bill? (Y/N)?: ");

choice=scan.next().charAt(0);

}while((choice!='n')&&(choice!='N'));

}

}

int date- scandate2.nextInt ):; if (date-12 | date-1 I date-2) basicRate -.10; else if (date >= 3 && date < 6) basicrateTwo =basicRate = kwUsed* 0.1; System.out.println(the basic rate is\n) systern. out. printf (% . 2f\n , basicRate) ; else if kwSystem.out.println(The excessCharge is:+ excessCharge); //Get template for formatting NumberFormat currency = NumberFormat.sh-4.3 javac SouthwestPowerandLight.java sh-4.3 java SouthwestPowerandLight Southwest Power and Light Billing Statement Pleas

Add a comment
Know the answer?
Add Answer to:
Java code is calculating the bill wrong and not displaying currency at the end: I've highlighted...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

  • This is my code but my overtime calculation is wrong and I cant figure out how...

    This is my code but my overtime calculation is wrong and I cant figure out how to round to two decimal places import java.util.Scanner; public class HourlyWage { public static void main(String[] args) { //Variables double totalWage; double totalOvertimePay; double totalPay; String name; double overtimeHours = 0.0; double hoursWorked = 0.0; double hourlyWage = 0.0; Scanner keyboard = new Scanner(System.in); System.out.println("Please enter your name "); name = keyboard.next(); System.out.println("Please enter your hourly wage "); hourlyWage = keyboard.nextDouble(); System.out.println("How many hours...

  • Please make the following code modular. Therefore contained in a package with several classes and not...

    Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q {    public static LinkedList<String> dogs = new LinkedList<String> ();    public static LinkedList<String> cats = new LinkedList<String> ();    public static LinkedList<String> animals = new LinkedList<String> ();    public static void enqueueCats(){        System.out.println("Enter name");        Scanner sc=new Scanner(System.in);        String name = sc.next();        cats.addLast(name);        animals.addLast(name);    }   ...

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • Could you help me pleas , this is my code I want change it to insert...

    Could you help me pleas , this is my code I want change it to insert student by user , and i have problem when i want append name it just one time i can't append or present more one. >>>>>>>>>>>>>>>>>>>. import java.util.Iterator; import java.util.Scanner; public class studentDLLTest { static int getChoice() { Scanner in = new Scanner(System.in); int choice; do { System.out.print("\nYour choice? : "); choice = in.nextInt(); } while (choice < 1 || choice > 9); return choice;...

  • JAVA: Rewrite the following code to where the inputs are from a file. The file name...

    JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; }    public static void main(String[] args) { int n, i, k; System.out.println("Enter...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT