Colindapieman

Lab5-Final

Feb 16th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. // Program Name: Lab05
  2. //  Purpose: Data Validation and error handling
  3. //  Revision History
  4. //  Revision    Date        Author      Comment
  5. //   -         2/16/14    Colin McNeil  Initial Release
  6. //
  7. import java.text.NumberFormat;
  8. import java.util.Scanner;
  9.  
  10. public class InvoiceApp
  11. {
  12.     public static String ValidCustomerType(String value){
  13.         String actualType="";
  14.         if (value.equals("r")||value.equals("c")){
  15.             actualType=value;  
  16.         }
  17.         else{
  18.             System.out.println("Only customer types r or c are accepted. Try again.");
  19.             return "false";
  20.         }
  21.         return actualType;
  22.     }
  23.     public static Double getValidSubtotal(String value){
  24.         double actualType=0;
  25.        
  26.         try{
  27.             actualType=Double.parseDouble(value);
  28.             if(actualType<=0){
  29.                 System.out.println("Subtotal must be a number larger than 0.");
  30.                 return 0.0;
  31.             }
  32.         }
  33.         catch(java.lang.NumberFormatException e){  
  34.             System.out.println("Subtotal must be a number larger than 0.");
  35.             return 0.0;
  36.         }
  37.         return actualType;
  38.     }
  39.     public static void main(String[] args)
  40.     {
  41.         Scanner sc = new Scanner(System.in);
  42.         String choice = "y";
  43.         boolean isValid = true;
  44.        
  45.         while (!choice.equalsIgnoreCase("n")&&isValid==true)
  46.         {          
  47.             // get the customer type from the user
  48.             Double subtotal = 0.0;
  49.             String customerType = "";
  50.             System.out.print("Enter customer type (r/c): ");
  51.             customerType = sc.nextLine();
  52.            
  53.             while(ValidCustomerType(customerType).equals("false")){
  54.                  System.out.print("Enter customer type (r/c): ");
  55.                  customerType = sc.nextLine();
  56.                  isValid= false;                
  57.             }
  58.             if(!ValidCustomerType(customerType).equals("false")){
  59.                 System.out.print("Enter subtotal:   ");  
  60.                 subtotal = getValidSubtotal(sc.nextLine());
  61.                     while (subtotal<=0.0){
  62.                          System.out.print("Enter subtotal:   ");
  63.                          subtotal = getValidSubtotal(sc.nextLine());
  64.                          isValid=false;                      
  65.                     }
  66.                     if (subtotal>0.0){
  67.                         isValid=true;
  68.                     }                  
  69.             }              
  70.             // get the subtotal from the user
  71.             if (isValid==true){                          
  72.                  // get the discount percent
  73.                  double discountPercent = 0;
  74.                  if (customerType.equals("r"))
  75.                  {
  76.                      if (subtotal < 100)
  77.                          discountPercent = 0;
  78.                      else if (subtotal >= 100 && subtotal < 250)
  79.                          discountPercent = .1;
  80.                      else if (subtotal >= 250)
  81.                          discountPercent = .2;
  82.                  }
  83.                  else if (customerType.equals("c"))
  84.                  {
  85.                      if (subtotal < 250)
  86.                          discountPercent = .2;
  87.                      else
  88.                          discountPercent = .3;
  89.                  }
  90.                  else
  91.                  {                          
  92.                  isValid=false;    
  93.                  }    
  94.                  // calculate the discount amount and total
  95.                  double discountAmount = subtotal * discountPercent;
  96.                  double total = subtotal - discountAmount;          
  97.                  // format and display the results
  98.                  NumberFormat currency = NumberFormat.getCurrencyInstance();
  99.                  NumberFormat percent = NumberFormat.getPercentInstance();
  100.                  System.out.println(
  101.                          "Discount percent: " + percent.format(discountPercent) + "\n" +
  102.                          "Discount amount:  " + currency.format(discountAmount) + "\n" +
  103.                          "Total:            " + currency.format(total) + "\n");            
  104.             }      
  105.             // see if the user wants to continue
  106.             System.out.print("Continue? (y/n): ");
  107.             choice = sc.nextLine();
  108.             System.out.println();
  109.             isValid=true;          
  110.         }
  111.         sc.close();
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment