Question: 1. Modify the application so it will only customer types r and c, it should discard any extra entries on the customer line type. If
1. Modify the application so it will only customer types r and c, it should discard any extra entries on the customer line type. If the user enters a invalid code the app should display and error message and ask the user to enter a valid code. This should be done before the user enters a subtotal.
2. Code a static method named getValidCustomerType that does the validation of step 1. This method should include one parameter that recieves a Scanner object, and it should return a valid customer type code. The method should get an entry from the user, check it for validity , display an error messageif it's invalid, and discard any other user entries whether or not the the entry is valid. This method should continue untill the user enters a valid one. Easisest way is to add the code for this method is copy the code you wrote in step 1.
3. Add a try statement that catches any InputMismatchException that the nextDouble method of the Scanner class might throw. The catch block should display a error message and issue a continue statement to jump to the beginning of the while loop. It should also discard invalid entry and any other entries on the line. For this to work, you'll need to import the InputMismatchException class, and you'll need to declare the subtotal varible before the try statement so it can be used outside that statement.
4. Code a static method named getValidSubtotal that uses the hasDouble method of the Scanner class to validate the subtotal entry so the InputMismatchException won't occur. This method requires should require one parameter that recieves a Scanner object, and it should return a valid subtotal. This method should get a entry from the user, check that its a valid double value, check that it;s greater then 0 and less then a 10000, display appropraite error messages if it's not valid, and discard any other user entries whether or not the entry is valid. This should continue until the method gets a valid subtotal.
5. Modify the code within the try statement so is uses this method. 6. Lastly, modify the code so the app works right even is the users enter 2 or more values when askes if he wants to continue. To do that, you need to discard any extra entries. Heres is the java code that needs to be modified and I have attached is a .java file too.
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.next(); System.out.print("Enter subtotal: "); double subtotal = sc.nextDouble(); // get the discount percent double discountPercent = 0; if (customerType.equalsIgnoreCase("R")) { if (subtotal discountPercent = 0; else if (subtotal >= 100 && subtotal discountPercent = .1; else if (subtotal >= 250) discountPercent = .2; } else if (customerType.equalsIgnoreCase("C")) { if (subtotal 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.next(); System.out.println(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
