1  /**
  2     An ATM that accesses a bank.
  3  */
  4  public class ATM 
  5  {  
  6     public static final int CHECKING = 1;
  7     public static final int SAVINGS = 2;
  8  
  9     private int state;
 10     private int customerNumber;
 11     private Customer currentCustomer;
 12     private BankAccount currentAccount;
 13     private Bank theBank;
 14     
 15     public static final int START = 1;
 16     public static final int PIN = 2;
 17     public static final int ACCOUNT = 3;
 18     public static final int TRANSACT = 4;
 19  
 20     /**
 21        Constructs an ATM for a given bank.
 22        @param aBank the bank to which this ATM connects
 23     */    
 24     public ATM(Bank aBank)
 25     {
 26        theBank = aBank;
 27        reset();
 28     }
 29  
 30     /**
 31        Resets the ATM to the initial state.
 32     */
 33     public void reset()
 34     {
 35        customerNumber = -1;
 36        currentAccount = null;
 37        state = START;             
 38     }
 39  
 40     /** 
 41        Sets the current customer number 
 42        and sets state to PIN. 
 43        (Precondition: state is START)
 44        @param number the customer number.
 45     */
 46     public void setCustomerNumber(int number) 
 47     {
 48        customerNumber = number;
 49        state = PIN;
 50     }
 51  
 52     /** 
 53        Finds customer in bank.
 54        If found sets state to ACCOUNT, else to START.
 55        (Precondition: state is PIN)
 56        @param pin the PIN of the current customer
 57     */
 58     public void selectCustomer(int pin)
 59     {  
 60        currentCustomer 
 61           = theBank.findCustomer(customerNumber, pin);
 62        if (currentCustomer == null) 
 63        {
 64           state = START;
 65        }
 66        else 
 67        {
 68           state = ACCOUNT;
 69        }
 70     }
 71     
 72     /** 
 73        Sets current account to checking or savings. Sets 
 74        state to TRANSACT. 
 75        (Precondition: state is ACCOUNT or TRANSACT)
 76        @param account one of CHECKING or SAVINGS
 77     */
 78     public void selectAccount(int account)
 79     {
 80        if (account == CHECKING)
 81        {
 82           currentAccount = currentCustomer.getCheckingAccount();
 83        }
 84        else
 85        {
 86           currentAccount = currentCustomer.getSavingsAccount();
 87        }
 88        state = TRANSACT;
 89     }
 90  
 91     /** 
 92        Withdraws amount from current account. 
 93        (Precondition: state is TRANSACT)
 94        @param value the amount to withdraw
 95     */
 96     public void withdraw(double value)
 97     {  
 98        currentAccount.withdraw(value);
 99     }
100  
101     /** 
102        Deposits amount to current account. 
103        (Precondition: state is TRANSACT)
104        @param value the amount to deposit
105     */
106     public void deposit(double value)
107     {  
108        currentAccount.deposit(value);
109     }
110  
111     /** 
112        Gets the balance of the current account. 
113        (Precondition: state is TRANSACT)
114        @return the balance
115     */
116     public double getBalance()
117     {  
118        return currentAccount.getBalance();
119     }
120  
121     /**
122        Moves back to the previous state.
123     */
124     public void back()
125     {
126        if (state == TRANSACT)
127        {
128           state = ACCOUNT;
129        }
130        else if (state == ACCOUNT)
131        {
132           state = PIN;
133        }
134        else if (state == PIN)
135        {
136           state = START;
137        }
138     }
139  
140     /**
141        Gets the current state of this ATM.
142        @return the current state
143     */
144     public int getState()
145     {
146        return state;
147     }
148  }