This term we will be developing a program that simulates a s

This term we will be developing a program that simulates a simple Banking system. This first assignment represents the basic structure of that program.

The program should include four classes, the InputReader class is already given, so the program should look like
sample output below:
ATM class
Bank class
BankCustomer class
InputReader class

Here is the InputReader class:

import java.util.Scanner;

/**
* Class InputReader reads user input from the keyboard.
*/

/**
* @version 2016.01.20
* @author Bullwinkle Moose
*/
public class InputReader {
   private Scanner scanner;

   /**
   * Default constructor for InputReader class
   * Create a new InputReader to read user input.
   */
   public InputReader() {
       scanner = new Scanner(System.in);
   }

   /**
   * Retrieves a user\'s int input
   * @return the user\'s input as an int
   */
   public int getIntInput() {
       try {
           return scanner.nextInt();
       } catch (java.util.InputMismatchException e) {
           System.out.println(\"Not a number - treating as zero\");
           scanner.nextLine(); // clear the buffer
           return 0;
       }
   }
  
   /**
   * Retrieves a user\'s double input
   * @return the user\'s input as an double
   */
   public double getDoubleInput() {
       try {
           return scanner.nextDouble();
       } catch (java.util.InputMismatchException e) {
           System.out.println(\"Not a number - treating as zero\");
           scanner.nextLine(); // clear the buffer
           return 0.0;
       }
   }

   /**
   * Retrieves a user\'s String input
   * @return the user\'s input as an String
   */
   public String getStringInput() {

       String input = scanner.next();
       if (input.length() > 0) {
           return input;
       } else {
           System.out.println(\"ERROR:Invalid input provided\");
       }
       return null;

   }
}

Sample runtime of the program:

Welcome to Bullwinkle\'s Bank.
Choose one of the following options:
1 - Sign In
2 - Deposit
3 - Withdraw
4 - Display Account Info
5 - Exit
> 2
ERROR: You must LOGIN before you can perform a transaction.
Enter Account Number:
tetet
Enter Passcode:
1212121
ERROR: Either account number or passcode is not correct.
Welcome to Bullwinkle\'s Bank.
Choose one of the following options:
1 - Sign In
2 - Deposit
3 - Withdraw
4 - Display Account Info
5 - Exit
> 1
Enter Account Number:
ST-123
Enter Passcode:
123
Choose one of the following options:
1 - Sign In
2 - Deposit
3 - Withdraw
4 - Display Account Info
5 - Exit
> 2
Enter the amount to deposit:
100.00
Choose one of the following options:
1 - Sign In
2 - Deposit
3 - Withdraw
4 - Display Account Info
5 - Exit
> 3
Enter the amount to withdraw:
25.83
Choose one of the following options:
1 - Sign In
2 - Deposit
3 - Withdraw
4 - Display Account Info
5 - Exit
> 4
Here is your information.
BankCustomer [firstName=Darby, lastName=Dog, accountNumber=ST-123, passcode=123, balance=74.17]
Choose one of the following options:
1 - Sign In
2 - Deposit
3 - Withdraw
4 - Display Account Info
5 - Exit
> 5
Thank you for banking at Bullwinkle\'s Bank
DEBUG: Displaying all the accounts in the bank.
BankCustomer [firstName=Darby, lastName=Dog, accountNumber=ST-123, passcode=123, balance=74.17]
BankCustomer [firstName=Freckle, lastName=Cat, accountNumber=ST-789, passcode=789, balance=0.0]
BankCustomer [firstName=Myia, lastName=Dog, accountNumber=ST-456, passcode=456, balance=0.0]

Solution

import java.util.Scanner;

/**
* Class InputReader reads user input from the keyboard.
*/

/**
* @version 2016.01.20
* @author Bullwinkle Moose
*/
public class InputReader {
   private Scanner scanner;

   /**
   * Default constructor for InputReader class
   * Create a new InputReader to read user input.
   */
   public InputReader() {
       scanner = new Scanner(System.in);
   }

   /**
   * Retrieves a user\'s int input
   * @return the user\'s input as an int
   */
   public int getIntInput() {
       try {
           return scanner.nextInt();
       } catch (java.util.InputMismatchException e) {
           System.out.println(\"Not a number - treating as zero\");
           scanner.nextLine(); // clear the buffer
           return 0;
       }
   }

   /**
   * Retrieves a user\'s double input
   * @return the user\'s input as an double
   */
   public double getDoubleInput() {
       try {
           return scanner.nextDouble();
       } catch (java.util.InputMismatchException e) {
           System.out.println(\"Not a number - treating as zero\");
           scanner.nextLine(); // clear the buffer
           return 0.0;
       }
   }

   /**
   * Retrieves a user\'s String input
   * @return the user\'s input as an String
   */
   public String getStringInput() {

       String input = scanner.next();
       if (input.length() > 0) {
           return input;
       } else {
           System.out.println(\"ERROR:Invalid input provided\");
       }
       return null;

   }
}

----------------------------------------------------------------------------------------------------

//ATM.java
import java.util.ArrayList;
public class ATM {

   //Bank object
   Bank bank=new Bank();

   //Constructor to set bank Object
   public ATM(Bank bankObject) {

       bank=bankObject;
   }

   //Method that retunrs the index of the account for given
   //actNumber and passcode
   public int signIn(String actNumber, int passCode){

       boolean found=false;
       int actIndex=-1;
       ArrayList<BankCustomer>customers=bank.getAccouns();

       for (int i = 0; i < customers.size() && !found; i++)
       {
           if(customers.get(i).getaccountNumber().equals(actNumber)&&
                   customers.get(i).getPasscode()==passCode)
           {
               found=true;
               actIndex=i;
           }
       }

       return actIndex;  
   }

}

----------------------------------------------------------------------------------------------------

//Bank.java
import java.util.ArrayList;
public class Bank {

   //Create an array list of BankCustomers
   private ArrayList<BankCustomer>customers;

   public Bank() {
       customers=new ArrayList<BankCustomer>();
   }

   public void addCustomer(BankCustomer cust){      
       customers.add(cust);
   }

   public void deposit(int actIndex, double amt){

       customers.get(actIndex).deposit(amt);
   }

   public void withdraw(int actIndex, double amt){

       customers.get(actIndex).withdraw(amt);
   }
  
   public BankCustomer getAccount(int actIndex){
       return customers.get(actIndex);
   }

   public ArrayList<BankCustomer> getAccouns(){
       return customers;
   }


   @Override
   public String toString() {

       String allCustomers=\"\";

       for (BankCustomer cust : customers) {
           allCustomers+=cust+\"\ \";
       }

       return allCustomers;
   }

}

----------------------------------------------------------------------------------------------------

//BankCustomer.java
public class BankCustomer {
   private String firstName;
   private String lastName;
   private String accountNumber;
   private int passcode;
   private double balance;
  
  
   public BankCustomer(String firstName,
   String lastName,
   String accountNumber,
   int passcode,
   double balance) {
       this.firstName=firstName;
       this.lastName=lastName;
       this.accountNumber=accountNumber;
       this.passcode=passcode;
       this.balance=balance;      
   }
  
   public String getaccountNumber(){
       return accountNumber;
   }
  
   public void deposit(double amt){
       balance+=amt;
   }
  
   public void withdraw(double amt){
       balance-=amt;
   }
  
   public int getPasscode(){
       return passcode;
   }
  
   @Override
   public String toString() {
       String customer=\"BankCustomer [firstName=\"+firstName
               +\", lastName=\"+lastName
               +\", accountNumber=\"+accountNumber
               +\", passcode=\"+passcode
               +\", balance=\"+balance+\"]\";
       return customer;
   }
  
}

----------------------------------------------------------------------------------------------------

/*
* Simulates the bank operations and prints
* the corresponding results to console.
* */
//Driver.java
public class Driver {  
   static InputReader scanner=new InputReader();
   public static void main(String[] args) {


       Bank bank=new Bank();

       bank.addCustomer(new BankCustomer(\"Darby\", \"Dog\", \"ST-123\", 123, 74.17));
       bank.addCustomer(new BankCustomer(\"Freckle\", \"Cat\", \"ST-789\", 789, 0.0));
       bank.addCustomer(new BankCustomer(\"Myia\", \"Dog\", \"ST-456\", 456, 0.0));


       ATM atm=new ATM(bank);

       String actNumber;
       int passCode;
       double amt=0;
       int actIndex = 0;
       int choice=menu();

       boolean run=true;

       if(choice==1)
       {
           System.out.println(\"Enter Account Number:\");
           actNumber=scanner.getStringInput();
           System.out.println(\"Enter Passcode:\");
           passCode=scanner.getIntInput();
           actIndex=atm.signIn(actNumber, passCode);

           if(actIndex==-1)
               System.out.println(\"ERROR: Either account number or passcode is not correct.\");
           else
           {
               run=false;
               choice=1;
           }
       }

       while(run && choice!=1)
       {
           System.out.println(\"ERROR: You must LOGIN before you can perform a transaction.\");
           System.out.println(\"Enter Account Number:\");
           actNumber=scanner.getStringInput();
           System.out.println(\"Enter Passcode:\");
           passCode=scanner.getIntInput();

           actIndex=atm.signIn(actNumber, passCode);

           if(actIndex==-1)
               System.out.println(\"ERROR: Either account number or passcode is not correct.\");
           else
           {
               run=false;
               choice=1;
           }
       }

       boolean repeat=true;

       while(repeat)
       {
           choice=menu();
           switch (choice) {
           case 2:      
               System.out.println(\"Enter the amount to deposit:\");
               amt=scanner.getDoubleInput();
               bank.deposit(actIndex, amt);
               break;
           case 3:      
               System.out.println(\"Enter the amount to withdraw:\");
               amt=scanner.getDoubleInput();
               bank.withdraw(actIndex, amt);
               break;
           case 4:      
               System.out.println(bank.getAccount(actIndex));
               break;
           case 5:      
               System.out.println(bank);
               System.exit(0);
               break;

           }
       }

   }

   private static int menu() {

       System.out.println(\"Welcome to Bullwinkle\'s Bank.\");
       System.out.println(\"Choose one of the following options:\");
       System.out.println(\"1 - Sign In\");
       System.out.println(\"2 - Deposit\");
       System.out.println(\"3 - Withdraw\");
       System.out.println(\"4 - Display Account Info\");
       System.out.println(\"5 - Exit\");
       System.out.println(\">\");
       int choice=scanner.getIntInput();

       return choice;
   }

}

----------------------------------------------------------------------------------------------------

Output:

Welcome to Bullwinkle\'s Bank.
Choose one of the following options:
1 - Sign In
2 - Deposit
3 - Withdraw
4 - Display Account Info
5 - Exit
>
1
Enter Account Number:
ST-123
Enter Passcode:
123
Welcome to Bullwinkle\'s Bank.
Choose one of the following options:
1 - Sign In
2 - Deposit
3 - Withdraw
4 - Display Account Info
5 - Exit
>
2
Enter the amount to deposit:
100
Welcome to Bullwinkle\'s Bank.
Choose one of the following options:
1 - Sign In
2 - Deposit
3 - Withdraw
4 - Display Account Info
5 - Exit
>
4
BankCustomer [firstName=Darby, lastName=Dog, accountNumber=ST-123, passcode=123, balance=174.17000000000002]
Welcome to Bullwinkle\'s Bank.
Choose one of the following options:
1 - Sign In
2 - Deposit
3 - Withdraw
4 - Display Account Info
5 - Exit
>
5
BankCustomer [firstName=Darby, lastName=Dog, accountNumber=ST-123, passcode=123, balance=174.17000000000002]
BankCustomer [firstName=Freckle, lastName=Cat, accountNumber=ST-789, passcode=789, balance=0.0]
BankCustomer [firstName=Myia, lastName=Dog, accountNumber=ST-456, passcode=456, balance=0.0]

This term we will be developing a program that simulates a simple Banking system. This first assignment represents the basic structure of that program. The prog
This term we will be developing a program that simulates a simple Banking system. This first assignment represents the basic structure of that program. The prog
This term we will be developing a program that simulates a simple Banking system. This first assignment represents the basic structure of that program. The prog
This term we will be developing a program that simulates a simple Banking system. This first assignment represents the basic structure of that program. The prog
This term we will be developing a program that simulates a simple Banking system. This first assignment represents the basic structure of that program. The prog
This term we will be developing a program that simulates a simple Banking system. This first assignment represents the basic structure of that program. The prog
This term we will be developing a program that simulates a simple Banking system. This first assignment represents the basic structure of that program. The prog
This term we will be developing a program that simulates a simple Banking system. This first assignment represents the basic structure of that program. The prog
This term we will be developing a program that simulates a simple Banking system. This first assignment represents the basic structure of that program. The prog

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site