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