Reference no: EM13304900
Subject:Computer Science, Operating Systems - Year 1Description:java validationProblem:
Using NetBeans 7.2
Please run the code to check for errors.
ValidatedInvoiceApp.java compiles
Application accepts only r and c customer type codes
Display error message on invalid code and request re-entry
static getValidCustomerType method performs validation
getValidCustomerType has Scanner parameter
getValidCustomerType returns valid customer code
try-catch block for subtotal entry with error message and continue statement in main
static getValidSubtotal method performs validation
getValidSubtotal has Scanner parameter
getValidSubtotal returns valid subtotal
getValidSubtotal limits valid subtotal to range > 0 and
getValidSubtotal prevents exception in main
Discard multiple entries when asked to continue
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) + "n" + "Discount amount: " + currency.format(discountAmount) + "n" + "Total: " + currency.format(total) + "n");
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
}
}