Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program Name: Lab05
- // Purpose: Data Validation and error handling
- // Revision History
- // Revision Date Author Comment
- // - 2/16/14 Colin McNeil Initial Release
- //
- import java.text.NumberFormat;
- import java.util.Scanner;
- public class InvoiceApp
- {
- public static String ValidCustomerType(String value){
- String actualType="";
- if (value.equals("r")||value.equals("c")){
- actualType=value;
- }
- else{
- System.out.println("Only customer types r or c are accepted. Try again.");
- return "false";
- }
- return actualType;
- }
- public static Double getValidSubtotal(String value){
- double actualType=0;
- try{
- actualType=Double.parseDouble(value);
- if(actualType<=0){
- System.out.println("Subtotal must be a number larger than 0.");
- return 0.0;
- }
- }
- catch(java.lang.NumberFormatException e){
- System.out.println("Subtotal must be a number larger than 0.");
- return 0.0;
- }
- return actualType;
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- String choice = "y";
- boolean isValid = true;
- while (!choice.equalsIgnoreCase("n")&&isValid==true)
- {
- // get the customer type from the user
- Double subtotal = 0.0;
- String customerType = "";
- System.out.print("Enter customer type (r/c): ");
- customerType = sc.nextLine();
- while(ValidCustomerType(customerType).equals("false")){
- System.out.print("Enter customer type (r/c): ");
- customerType = sc.nextLine();
- isValid= false;
- }
- if(!ValidCustomerType(customerType).equals("false")){
- System.out.print("Enter subtotal: ");
- subtotal = getValidSubtotal(sc.nextLine());
- while (subtotal<=0.0){
- System.out.print("Enter subtotal: ");
- subtotal = getValidSubtotal(sc.nextLine());
- isValid=false;
- }
- if (subtotal>0.0){
- isValid=true;
- }
- }
- // get the subtotal from the user
- if (isValid==true){
- // get the discount percent
- double discountPercent = 0;
- if (customerType.equals("r"))
- {
- if (subtotal < 100)
- discountPercent = 0;
- else if (subtotal >= 100 && subtotal < 250)
- discountPercent = .1;
- else if (subtotal >= 250)
- discountPercent = .2;
- }
- else if (customerType.equals("c"))
- {
- if (subtotal < 250)
- discountPercent = .2;
- else
- discountPercent = .3;
- }
- else
- {
- isValid=false;
- }
- // 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.nextLine();
- System.out.println();
- isValid=true;
- }
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment