Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

modify the while loop so it is an infinite loop. then add code to the end of the

ID: 3856122 • Letter: M

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 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();
}

}

Explanation / Answer

Modified code is below:

just add do-while loop(for infinite)instead of while loop

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;

//starting of do-while loop so that it is an infinite loop

do

{

Scanner sc = new Scanner(System.in);

// 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();

}

while(!choice.equalsIgnoreCase("n") ); //if user enters n or N program halts looping or stop working(terminates)

}

}