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