001: import java.io.IOException;
002: import java.sql.SQLException;
003: 
004: /**
005:    An ATM that accesses a bank.
006: */
007: public class ATM 
008: {  
009:    /**
010:       Constructs an ATM for a given bank.
011:       @param aBank the bank to which this ATM connects
012:    */    
013:    public ATM(Bank aBank)
014:    {
015:       theBank = aBank;
016:       reset();
017:    }
018: 
019:    /**
020:       Resets the ATM to the initial state.
021:    */
022:    public void reset()
023:    {
024:       customerNumber = -1;
025:       currentAccount = null;
026:       state = START;             
027:    }
028: 
029:    /** 
030:       Sets the current customer number 
031:       and sets state to PIN. 
032:       (Precondition: state is START)
033:       @param number the customer number.
034:    */
035:    public void setCustomerNumber(int number) 
036:    {
037:       assert state == START;
038:       customerNumber = number;
039:       state = PIN;
040:    }
041: 
042:    /** 
043:       Finds customer in bank.
044:       If found sets state to ACCOUNT, else to START.
045:       (Precondition: state is PIN)
046:       @param pin the PIN of the current customer
047:    */
048:    public void selectCustomer(int pin) 
049:          throws SQLException
050:    {  
051:       assert state == PIN;
052:       currentCustomer 
053:             = theBank.findCustomer(customerNumber, pin);
054:       if (currentCustomer == null) 
055:          state = START;
056:       else 
057:          state = ACCOUNT;
058:    }
059:    
060:    /** 
061:       Sets current account to checking or savings. Sets 
062:       state to TRANSACT. 
063:       (Precondition: state is ACCOUNT or TRANSACT)
064:       @param account one of CHECKING or SAVINGS
065:    */
066:    public void selectAccount(int account)
067:    {
068:       assert state == ACCOUNT || state == TRANSACT;
069:       if (account == CHECKING)
070:          currentAccount = currentCustomer.getCheckingAccount();
071:       else
072:          currentAccount = currentCustomer.getSavingsAccount();
073:       state = TRANSACT;
074:    }
075: 
076:    /** 
077:       Withdraws amount from current account. 
078:       (Precondition: state is TRANSACT)
079:       @param value the amount to withdraw
080:    */
081:    public void withdraw(double value)
082:          throws SQLException
083:    {  
084:       assert state == TRANSACT;
085:       currentAccount.withdraw(value);
086:    }
087: 
088:    /** 
089:       Deposits amount to current account. 
090:       (Precondition: state is TRANSACT)
091:       @param value the amount to deposit
092:    */
093:    public void deposit(double value)
094:          throws SQLException
095:    {  
096:       assert state == TRANSACT;
097:       currentAccount.deposit(value);
098:    }
099: 
100:    /** 
101:       Gets the balance of the current account. 
102:       (Precondition: state is TRANSACT)
103:       @return the balance
104:    */
105:    public double getBalance()
106:          throws SQLException
107:    {  
108:       assert state == TRANSACT;
109:       return currentAccount.getBalance();
110:    }
111: 
112:    /**
113:       Moves back to the previous state.
114:    */
115:    public void back()
116:    {
117:       if (state == TRANSACT)
118:          state = ACCOUNT;
119:       else if (state == ACCOUNT)
120:          state = PIN;
121:       else if (state == PIN)
122:          state = START;
123:    }
124: 
125:    /**
126:       Gets the current state of this ATM.
127:       @return the current state
128:    */
129:    public int getState()
130:    {
131:       return state;
132:    }
133: 
134:    private int state;
135:    private int customerNumber;
136:    private Customer currentCustomer;
137:    private BankAccount currentAccount;
138:    private Bank theBank;
139:    
140:    public static final int START = 1;
141:    public static final int PIN = 2;
142:    public static final int ACCOUNT = 3;
143:    public static final int TRANSACT = 4;
144: 
145:    public static final int CHECKING = 1;
146:    public static final int SAVINGS = 2;
147: }