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 assert state == START;
49 customerNumber = number;
50 state = PIN;
51 }
52
53 /**
54 Finds customer in bank.
55 If found sets state to ACCOUNT, else to START.
56 (Precondition: state is PIN)
57 @param pin the PIN of the current customer
58 */
59 public void selectCustomer(int pin)
60 {
61 assert state == PIN;
62 currentCustomer
63 = theBank.findCustomer(customerNumber, pin);
64 if (currentCustomer == null)
65 state = START;
66 else
67 state = ACCOUNT;
68 }
69
70 /**
71 Sets current account to checking or savings. Sets
72 state to TRANSACT.
73 (Precondition: state is ACCOUNT or TRANSACT)
74 @param account one of CHECKING or SAVINGS
75 */
76 public void selectAccount(int account)
77 {
78 assert state == ACCOUNT || state == TRANSACT;
79 if (account == CHECKING)
80 currentAccount = currentCustomer.getCheckingAccount();
81 else
82 currentAccount = currentCustomer.getSavingsAccount();
83 state = TRANSACT;
84 }
85
86 /**
87 Withdraws amount from current account.
88 (Precondition: state is TRANSACT)
89 @param value the amount to withdraw
90 */
91 public void withdraw(double value)
92 {
93 assert state == TRANSACT;
94 currentAccount.withdraw(value);
95 }
96
97 /**
98 Deposits amount to current account.
99 (Precondition: state is TRANSACT)
100 @param value the amount to deposit
101 */
102 public void deposit(double value)
103 {
104 assert state == TRANSACT;
105 currentAccount.deposit(value);
106 }
107
108 /**
109 Gets the balance of the current account.
110 (Precondition: state is TRANSACT)
111 @return the balance
112 */
113 public double getBalance()
114 {
115 assert state == TRANSACT;
116 return currentAccount.getBalance();
117 }
118
119 /**
120 Moves back to the previous state.
121 */
122 public void back()
123 {
124 if (state == TRANSACT)
125 state = ACCOUNT;
126 else if (state == ACCOUNT)
127 state = PIN;
128 else if (state == PIN)
129 state = START;
130 }
131
132 /**
133 Gets the current state of this ATM.
134 @return the current state
135 */
136 public int getState()
137 {
138 return state;
139 }
140 }