I have the starter file below, and all I need are listed below: a. Validate cust
ID: 3650840 • Letter: I
Question
I have the starter file below, and all I need are listed below:a. Validate customer type code: Modify the application so that it will only use these codes: r/c. If the user enters an invalid code, the application should display an error message and ask user to enter a valid code. Test.
b. Code a static method getValidCustomerType does validation in step b that takes a Scanner object as parameter and return a valid customer type code. Modify application to call this method. Test.
c. Validate the subtotal: Add a try statement so it catches InputMismatchException that the nextDouble method of Scanner class might throw. Catch displays appropriate error message and issue a continue statement to jump to beginning of the loop. Test.
d. Code a static method getValidSubtotal that uses hasDouble method of Scanner class to validate subtotal so InputMismatchException won
Explanation / Answer
The different parts are commented as "//Part A" or "Part B" and so on in the program:
======================================================
import java.text.NumberFormat;
import java.util.InputMismatchException;
import java.util.Scanner;
public class InvoiceApp
{
//Part D
public static Double getValidSubotal(Scanner sc)
{
System.out.print("Enter subtotal: ");
double subtotal=sc.nextDouble();
if(subtotal>=0 && subtotal<=1000)return subtotal;
else
{
System.out.println("Invalid subtotal, enter again");
return -1.0;
}
}
//Part B
public static String getValidCustomerType(Scanner sc)
{
System.out.print("Enter customer type (r/c): ");
String customerType = sc.next();
if(!(customerType.equalsIgnoreCase("r")||customerType.equalsIgnoreCase("c")))
{
System.out.println("Invalid customer type, enter again");
return "i";
}
return customerType;
}
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
//Part A
System.out.print("Enter customer type (r/c): ");
String customerType = sc.next();
if(!(customerType.equalsIgnoreCase("r")||customerType.equalsIgnoreCase("c")))
{
System.out.println("Invalid customer type, enter again");
continue;
}
//Part B
/*
String customerType=getValidCustomerType(sc);
if(customerType.equalsIgnoreCase("i"))continue;
*/
double subtotal;
//Part D
subtotal=getValidSubotal(sc);
if(subtotal==-1.0)continue;
//Part C
/*System.out.print("Enter subtotal: ");
try
{
subtotal = sc.nextDouble();
}
catch(InputMismatchException e)
{
System.out.println("Sub-total not correct,enter details again: ");
continue;
}*/
// get the discount percent
double discountPercent = 0;
if (customerType.equalsIgnoreCase("R"))
{
if (subtotal < 100) {
discountPercent = 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.next();
System.out.println();
}
}
}