Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Bryan Cole
- // Summer 2016 CCSF
- // Bank Account Driver
- // Includes user input and data validation
- package cole_bank_account;
- import java.util.Scanner;
- public class BankDriver
- {
- public static void main(String[] args)
- {
- String driverName;
- String driverAccountId;
- double driverBalance;
- double driverInterest;
- Scanner input = new Scanner(System.in);
- // Items are created before the account object.
- System.out.println("Please input your name for the Bank account.");
- while (!input.hasNext("(?i)([a-z]+)")) {
- System.out.println("You must use letter characters only. Please try again.");
- System.out.println("Please input your name.");
- //driverName = input.nextLine();
- input.nextLine();
- }
- driverName = input.nextLine();
- System.out.println("Please enter your account id. Any format is acceptable.");
- driverAccountId = input.nextLine();
- System.out.println("Please input your initial balance for the Bank account.");
- while (!input.hasNextDouble()) {
- System.out.println("You must use number characters only.");
- System.out.println("Please input your initial balance.");
- //driverName = input.nextLine();
- input.nextLine();
- }
- String testdouble = input.nextLine();
- if (!isDouble(testdouble))
- {
- System.out.println("There is a critical error in your initial balance.");
- System.out.println("Please include numbers only.");
- System.out.println("Program is exiting.");
- System.exit(0);
- }
- driverBalance = Double.parseDouble(testdouble);
- System.out.println("Please input your interest rate. Use numbers only. For example, 5% = 5. Or 5.5 = 5.5%.");
- while (!input.hasNextDouble()) {
- System.out.println("You must use number characters only.");
- System.out.println("Please input your interest rate.");
- //driverName = input.nextLine();
- input.nextLine();
- }
- testdouble = input.nextLine();
- if (!isDouble(testdouble))
- {
- System.out.println("There is a critical error in your interest rate.");
- System.out.println("Please include numbers only.");
- System.out.println("Program is exiting.");
- System.exit(0);
- }
- driverInterest = Double.parseDouble(testdouble);
- // create initial account object
- Account account1 = new Account(driverName, driverAccountId, driverBalance, driverInterest);
- String loopCode;
- while (true)
- {
- System.out.println("");
- System.out.println("Enter 'd' to make a deposit.");
- System.out.println("Enter 'w' to make a withdrawal.");
- System.out.println("Enter 'b' to check the balance.");
- System.out.println("Enter 'i' to add interest.");
- System.out.println("Enter 'q' to quit.");
- loopCode = input.nextLine();
- if (loopCode.equals("q"))
- {
- break;
- }
- else if (loopCode.equals("d"))
- {
- System.out.println("Please input your deposit amount.");
- while (!input.hasNextDouble()) {
- System.out.println("You must use number characters only.");
- System.out.println("Please input your deposit amount.");
- input.nextLine();
- }
- account1.deposit(Double.parseDouble(input.nextLine()));
- }
- else if (loopCode.equals("w"))
- {
- System.out.println("Please input your withdrawal amount.");
- while (!input.hasNextDouble()) {
- System.out.println("You must use number characters only.");
- System.out.println("Please input your withdrawal amount.");
- input.nextLine();
- }
- account1.withdraw(Double.parseDouble(input.nextLine()));
- }
- else if (loopCode.equals("b"))
- {
- account1.getBalance();
- }
- else if (loopCode.equals("i"))
- {
- account1.addInterest();
- }
- }
- System.out.println("Exiting, here is your final account information.");
- System.out.println(account1);
- }
- public static boolean isDouble(String value) {
- try {
- Double.parseDouble(value);
- return true;
- } catch (NumberFormatException e) {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement