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

package murach.invoice; import java.text.NumberFormat; import java.util.Scanner;

ID: 3597794 • Letter: P

Question

package murach.invoice;

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";

        while (!choice.equalsIgnoreCase("n")) {
            // get the input from the user
            System.out.print("Enter customer type (r/c): ");
            String customerType = sc.nextLine();
            System.out.print("Enter subtotal:   ");
            double subtotal = Double.parseDouble(sc.nextLine());

            // get the discount percent
            double discountPercent = .1;
            if (customerType.equalsIgnoreCase("r")) {
                if (subtotal < 100) {
                    discountPercent = 0.0;
                }
                else if (subtotal >= 100 && subtotal < 250) {
                    discountPercent = .1;
                }
                else if (subtotal >= 250) {
                    discountPercent = .2;
                }
            }
            else if (customerType.equalsIgnoreCase("c")) {
               if (subtotal < 250) {
                    discountPercent = .2;
               }
               else {
                    discountPercent = .3;
               }
            }
            else {
                discountPercent = .1;
            }

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

Modify the Invoice application

1. Modify the if/else statement so customers of type "r" with a subtotal that is greater than or equal to $250 but less than or equal to $500 get a 25% discount and those with a subtotal of $500 or more get a 30% discount. Next, change the if/esle statement so customers of type "c" always get a 20% discount. Then, test the application to make sure this works correctly.

2. Add another customer type to the else if/else statement so customers of type "t" get a 40% discount for subtotals of less than $500, and a 50% discount for subtotals of $500 or more. Then, test the applicaion.

3.Check your code to make sure that no discount is provided for a customer tpe code that isn't "r", "c", or "t". Then, fix this if necessary.

4.Use a switch statement for the customer type instead of using an if/else statement.

5. Add a try/catch statement that catches the NumberFormatException that occurs when the user enters an invalid subtotal. In the catch block, add code that prints an error message and jumps to the top of the while loop.

6. 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" at the "Continue (y/n) prompt.

This project is done using java with netbeans!

Explanation / Answer

InvoiceApp.java

package murach.invoice;

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;
while (true) {
// get the input from the user
System.out.print("Enter customer type (r/c/t): ");
String customerType = sc.nextLine();

try {
System.out.print("Enter subtotal: ");
double subtotal = Double.parseDouble(sc.nextLine());
// get the discount percent
double discountPercent = .1;
switch (customerType) {
case "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;
}
case "C":
case "c":
{
discountPercent = 0.20;
break;
}
case "T":
case "t":
{
if (subtotal < 500) {
discountPercent = 0.40;
} else if (subtotal >= 500) {
discountPercent = 0.50;
}
break;
}
default:
{
System.out.println("** Invalid Choice **");
continue;
}
}

// 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) + " ");
} catch (NumberFormatException e) {
System.out.println(e);
continue;
}

// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();

if (choice.equalsIgnoreCase("y")) {
sc.nextLine();
continue;
} else {
System.out.println(":: Program Exit ::");
break;
}
}
}
}

_________________

Output:

Enter customer type (r/c/t): r
Enter subtotal: 400
Discount percent: 25%
Discount amount: $100.00
Total: $300.00

Continue? (y/n): y
Enter customer type (r/c/t): r
Enter subtotal: 500
Discount percent: 30%
Discount amount: $150.00
Total: $350.00

Continue? (y/n): y
Enter customer type (r/c/t): c
Enter subtotal: 500
Discount percent: 20%
Discount amount: $100.00
Total: $400.00

Continue? (y/n): y
Enter customer type (r/c/t): t
Enter subtotal: 400
Discount percent: 40%
Discount amount: $160.00
Total: $240.00

Continue? (y/n): y
Enter customer type (r/c/t): t
Enter subtotal: 600
Discount percent: 50%
Discount amount: $300.00
Total: $300.00

Continue? (y/n): y
Enter customer type (r/c/t): t
Enter subtotal: hello
java.lang.NumberFormatException: For input string: "hello"
Enter customer type (r/c/t): t
Enter subtotal: 480
Discount percent: 40%
Discount amount: $192.00
Total: $288.00

Continue? (y/n): n
:: Program Exit ::

_____________Could you rate me well.Plz .Thank You