Question: modify the while loop so it is an infinite loop. then add code to the end of the loop that breaks out of the loop
modify the while loop so it is an infinite loop. then add code to the end of the loop that breaks out of the loop if the user enters "n". JAVA PLEASE
package murach.invoice; //Craig Persad import java.text.NumberFormat; import java.util.Scanner;
public class InvoiceApp {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String choice = "y"; double subtotal = 0; while (!choice.equalsIgnoreCase("n")) { // get the input from the user System.out.print("Enter customer type (r/c/t): "); String customerType = sc.nextLine(); System.out.print("Enter subtotal: "); try { subtotal = Double.parseDouble(sc.nextLine()); } catch (NumberFormatException e) { System.out.println("Error! Invalid number. Try again!"); continue; }
// get the discount percent double discountPercent = .1; switch (customerType){ //if (customerType.equalsIgnoreCase("r")); case "r": if (subtotal < 100) { discountPercent = 0.0; } else if (subtotal >= 100 && subtotal < 250) { discountPercent = .1; } else if (subtotal >= 250 && subtotal <= 500 ) { discountPercent = .25; } else if (subtotal > 500) { discountPercent = .30; } break; //else if (customerType.equalsIgnoreCase("c")) { case "c": discountPercent = .20; break; // else if (customerType.equalsIgnoreCase("t")) { case "t": if (500 > subtotal) { discountPercent = .4; } else if (subtotal > 500) { discountPercent = .5; } break; default: discountPercent = 0; break; } //else { discountPercent = 0; }
// calculate the discount amount and total double discountAmount = subtotal * discountPercent; double total = subtotal - discountAmount;
// format and display the results NumberFormat currency = NumberFormat.getCurrencyInstance(); NumberFormat percent = NumberFormat.getPercentInstance(); System.out.println( "Discount percent: " + percent.format(discountPercent) + " " + "Discount amount: " + currency.format(discountAmount) + " " + "Total: " + currency.format(total) + " ");
// see if the user wants to continue System.out.print("Continue? (y/n): "); choice = sc.nextLine(); System.out.println(); } sc.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
