Advertisement
intangibles

Untitled

Jun 27th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. // Bryan Cole
  2. // Summer 2016 CCSF
  3. // Bank Account Driver
  4. // Includes user input and data validation
  5.  
  6. package cole_bank_account;
  7.  
  8. import java.util.Scanner;
  9.  
  10. public class BankDriver
  11. {
  12.  
  13.    public static void main(String[] args)
  14.    {
  15.       String driverName;
  16.       String driverAccountId;
  17.       double driverBalance;
  18.       double driverInterest;
  19.      
  20.       Scanner input = new Scanner(System.in);
  21.      
  22.       // Items are created before the account object.
  23.       System.out.println("Please input your name for the Bank account.");
  24.       while (!input.hasNext("(?i)([a-z]+)")) {
  25.          System.out.println("You must use letter characters only. Please try again.");
  26.          System.out.println("Please input your name.");
  27.          //driverName = input.nextLine();
  28.          input.nextLine();
  29.       }
  30.       driverName = input.nextLine();
  31.      
  32.       System.out.println("Please enter your account id. Any format is acceptable.");
  33.       driverAccountId = input.nextLine();
  34.        
  35.       System.out.println("Please input your initial balance for the Bank account.");
  36.       while (!input.hasNextDouble()) {
  37.          System.out.println("You must use number characters only.");
  38.          System.out.println("Please input your initial balance.");
  39.          //driverName = input.nextLine();
  40.          input.nextLine();
  41.       }
  42.       String testdouble = input.nextLine();
  43.       if (!isDouble(testdouble))
  44.       {
  45.          System.out.println("There is a critical error in your initial balance.");
  46.          System.out.println("Please include numbers only.");
  47.          System.out.println("Program is exiting.");
  48.          System.exit(0);
  49.       }
  50.       driverBalance = Double.parseDouble(testdouble);
  51.      
  52.       System.out.println("Please input your interest rate. Use numbers only. For example, 5% = 5. Or 5.5 = 5.5%.");
  53.       while (!input.hasNextDouble()) {
  54.          System.out.println("You must use number characters only.");
  55.          System.out.println("Please input your interest rate.");
  56.          //driverName = input.nextLine();
  57.          input.nextLine();
  58.       }
  59.       testdouble = input.nextLine();
  60.       if (!isDouble(testdouble))
  61.       {
  62.          System.out.println("There is a critical error in your interest rate.");
  63.          System.out.println("Please include numbers only.");
  64.          System.out.println("Program is exiting.");
  65.          System.exit(0);
  66.       }
  67.       driverInterest = Double.parseDouble(testdouble);
  68.      
  69.       // create initial account object
  70.       Account account1 = new Account(driverName, driverAccountId, driverBalance, driverInterest);
  71.      
  72.       String loopCode;
  73.       while (true)
  74.       {
  75.          System.out.println("");
  76.          System.out.println("Enter 'd' to make a deposit.");
  77.          System.out.println("Enter 'w' to make a withdrawal.");
  78.          System.out.println("Enter 'b' to check the balance.");
  79.          System.out.println("Enter 'i' to add interest.");
  80.          System.out.println("Enter 'q' to quit.");
  81.          loopCode = input.nextLine();
  82.          if (loopCode.equals("q"))
  83.          {
  84.             break;
  85.          }
  86.          else if (loopCode.equals("d"))
  87.          {
  88.             System.out.println("Please input your deposit amount.");
  89.             while (!input.hasNextDouble()) {
  90.                System.out.println("You must use number characters only.");
  91.                System.out.println("Please input your deposit amount.");
  92.                input.nextLine();
  93.             }
  94.             account1.deposit(Double.parseDouble(input.nextLine()));
  95.          }
  96.          else if (loopCode.equals("w"))
  97.          {
  98.             System.out.println("Please input your withdrawal amount.");
  99.             while (!input.hasNextDouble()) {
  100.                System.out.println("You must use number characters only.");
  101.                System.out.println("Please input your withdrawal amount.");
  102.                input.nextLine();
  103.             }
  104.             account1.withdraw(Double.parseDouble(input.nextLine()));
  105.          }
  106.          else if (loopCode.equals("b"))
  107.          {
  108.            account1.getBalance();
  109.          }
  110.          else if (loopCode.equals("i"))
  111.          {
  112.             account1.addInterest();
  113.          }
  114.       }
  115.      
  116.       System.out.println("Exiting, here is your final account information.");
  117.       System.out.println(account1);
  118.    }
  119.  
  120. public static boolean isDouble(String value) {
  121.     try {
  122.         Double.parseDouble(value);
  123.         return true;
  124.     } catch (NumberFormatException e) {
  125.         return false;
  126.     }
  127. }
  128.    
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement