1. Modify the application so it will only customer types r and c, it should disc
ID: 3538808 • Letter: 1
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 the user enters a invalid code the app should display and error message and ask the user to enter r or c
2. code a static method named getValidCustomerType that does the validation of step 1. this method should inclide 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 , dissplay an error if it invalid, discard any other user entries wether ot not the the entry is valid. This method should continue untill the user enters a valid one.
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. forthis 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 and continue untill the user enters a valid one.
5. modify the code so the application works right even if the user enters two or more values when asked if he wants to continue. to do that you need to dicard any extra entries,
Heres is the java code that needs to be modified.
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 < 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();
}
}
}
Explanation / Answer
import java.text.NumberFormat;
import java.util.InputMismatchException;
import java.util.Scanner;
public class InvoiceApp
{
public static String getValidCustomerType(Scanner sc)
{
String customerType="";
while(true)
{
System.out.print("Enter customer type (r/c): ");
customerType= sc.next();
if(customerType.equalsIgnoreCase("c") ||customerType.equalsIgnoreCase("r"))
break;
else
System.out.println("invalid code,");
}
return customerType;
}
public static double getValidSubtotal (Scanner sc)
{
double subTotal=0;
String garbagedata;
while(true)
{
System.out.print("Enter subtotal: ");
if( sc.hasNextDouble())
{
subTotal=sc.nextDouble();
if(subTotal>0 && subTotal<1000)
break;
}
else
{
garbagedata=sc.next();
}
System.out.println("invalid subTotal, please enter a double value greater than 0 and less than 1000");
}
return subTotal;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String choice = "y";
double discountPercent = 0;
double subtotal=0;
while (choice.charAt(0)=='y'|| choice.charAt(0)=='Y')
{
try
{
// get the input from the user
String customerType =getValidCustomerType(sc);
subtotal=getValidSubtotal(sc);
// get the discount percent
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;
}
}
catch(InputMismatchException ex)
{
ex.printStackTrace();
}
// 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();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.