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