-
Notifications
You must be signed in to change notification settings - Fork 151
/
ATM.java
422 lines (340 loc) · 13.8 KB
/
ATM.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
// ATM.java
// Represents an automated teller machine
public class ATM
{
private boolean userAuthenticated; // whether user is authenticated
private int currentAccountNumber; // current user's account number
private Screen screen; // ATM's screen
private Keypad keypad; // ATM's keypad
private CashDispenser cashDispenser; // ATM's cash dispenser
private DepositSlot depositSlot; // ATM's deposit slot
private BankDatabase bankDatabase; // account information database
private int AdminCheck;
private String userinput = "";
private int position = 0;
private static ATM uniqueinstance;
Iterator Users = BankDatabase.createIterator();
// constants corresponding to main menu options
private static final int BALANCE_INQUIRY = 1;
private static final int WITHDRAWAL = 2;
private static final int DEPOSIT = 3;
private static final int EXIT = 4;
// no-argument ATM constructor initializes instance variables
public ATM()
{
userAuthenticated = false; // user is not authenticated to start
currentAccountNumber = 0; // no current account number to start
screen = new Screen(); // create screen
keypad = new Keypad(); // create keypad
cashDispenser = new CashDispenser(); // create cash dispenser
depositSlot = new DepositSlot(); // create deposit slot
bankDatabase = new BankDatabase(); // create acct info database
} // end no-argument ATM constructor
// start ATM
public void run()
{
// welcome and authenticate user; perform transactions
// loop while user is not yet authenticated
startlogin(); // authenticate user
} // end while
//else
//performTransactions(); // user is now authenticated
//userAuthenticated = false; // reset before next ATM session
//currentAccountNumber = 0; // reset before next ATM session
//screen.displayMessageLine("\nThank you! Goodbye!");
// end while
// end method run
// attempts to authenticate user against database
void startlogin()
{
position = 0;
screen.createlogin();
userinput = "";
authenticate check = new authenticate();
screen.Mainframe.revalidate();
screen.Inputfield2.setText("");
keypad.setbuttons();
addkeypadlisteners();
screen.Mainframe.add( keypad.addkeypad(), BorderLayout.CENTER);
screen.Mainframe.revalidate();
keypad.BEnter.addActionListener( check );
screen.Mainframe.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
screen.Mainframe.setSize( 400, 280 ); // set frame size
screen.Mainframe.setVisible( true );
screen.Mainframe.revalidate();
}
// set userAuthenticated to boolean value returned by database
public void authenticateuser(int pin){
userAuthenticated =
bankDatabase.authenticateUser(pin);
// check whether authentication succeeded
if (userAuthenticated)
{
int accountNumber = bankDatabase.getaccpin(pin);
AdminCheck = bankDatabase.getadmin(pin);
if (AdminCheck == 0){
currentAccountNumber = accountNumber;
screen.Mainframe.getContentPane().removeAll();
screen.Mainframe.revalidate();
createmenu();
screen.Mainframe.add( keypad.addkeypad(), BorderLayout.CENTER);
screen.Mainframe.revalidate();
}
else
createAdminGUI();
Iterator UserIterator = BankDatabase.createIterator();
Addcheck check = new Addcheck();
Deletecheck check2 = new Deletecheck();
screen.button2.addActionListener(check);
screen.button3.addActionListener(check2);
//currentAccountNumber = accountNumber; // save user's account #
} // end if
else
screen.messageJLabel3.setText(
"Invalid account number or PIN. Please try again.");
} // end method authenticateUser
private class authenticate implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
//int accnum = Integer.parseInt( screen.Inputfield1.getText() );
int PIN = Integer.parseInt( screen.Inputfield2.getText() );
//Get the PIN from the GUI text field.
authenticateuser(PIN);
}
}
private class Addcheck implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
//Action Listener for adding user.
BankDatabase.Adduser();
}
}
private class Deletecheck implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
//Action Listener for deleting a user.
BankDatabase.Deleteuser(position);
position = position - 1;
}
}
//creating the main menu GUI
public void createmenu(){
screen.setSize( 300, 150 );
balancecheck check1 = new balancecheck();
Depositcheck check2 = new Depositcheck();
Withdrawcheck check3 = new Withdrawcheck();
Exitcheck check4 = new Exitcheck();
screen.Mainframe.getContentPane().removeAll();
screen.Mainframe.revalidate();
//Add the keypad panel to the GUI
screen.Mainframe.add( keypad.addkeypad(), BorderLayout.CENTER);
screen.createmenu();
Account Account1 = bankDatabase.getAccount(currentAccountNumber);
screen.messageJLabel.setText("Welcome " + Account1.getUsername() + " ");
keypad.B1.addActionListener( check1 );
keypad.B2.addActionListener(check3);
keypad.B3.addActionListener(check2);
keypad.B4.addActionListener(check4);
screen.Mainframe.revalidate();
}
// display the main menu and perform transactions
//the four below actionlisters are the main menu options.
private class balancecheck implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
userinput = "";
performTransactions(1);
}
}
private class Depositcheck implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
userinput = "";
performTransactions(3);
}
}
private class Withdrawcheck implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
userinput = "";
performTransactions(2);
}
}
private class Exitcheck implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
startlogin();
}
}
private void performTransactions(int a)
{
// local variable to store transaction currently being processed
Transaction currentTransaction = null;
currentTransaction =
createTransaction(a);
keypad.setbuttons();
addkeypadlisteners();
userinput = "";
screen.Inputfield2.setText(userinput);
currentTransaction.execute();
Backcheck Back = new Backcheck();
screen.Exit.addActionListener(Back);
screen.Mainframe.revalidate();
}
public class Backcheck implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
//This takes the user back to the main menu.
createmenu();
screen.Mainframe.add( keypad.addkeypad(), BorderLayout.CENTER);
screen.Mainframe.revalidate();
userinput="";
screen.Inputfield2.setText(userinput);
}
}
private Transaction createTransaction(int type)
{
Transaction temp = null; // temporary Transaction variable
screen.getContentPane().removeAll();
screen.revalidate();
// determine which type of Transaction to create
if(type == 1) // create new BalanceInquiry transaction
temp = new BalanceInquiry(
currentAccountNumber, screen, bankDatabase);
else if(type == 2)// create new Withdrawal transaction
temp = new Withdrawal(currentAccountNumber, screen,
bankDatabase, keypad, cashDispenser);
else if(type == 3){ // create new Deposit transaction
screen.setSize( 400, 250 );
temp = new Deposit(currentAccountNumber, screen,
bankDatabase, keypad, depositSlot);}
// end switch
return temp; // return the newly created object
}
// end method createTransaction
//This creates the 'admin' screen if the Isadmin field is set to 1.
public void createAdminGUI(){
screen.Mainframe.getContentPane().removeAll();
Nextcheck Ncheck = new Nextcheck();
Prevcheck Pcheck = new Prevcheck();
Exitcheck check4 = new Exitcheck();
screen.Mainframe.revalidate();
screen.createAdminpage();
screen.button1.addActionListener(Ncheck);
screen.button4.addActionListener(Pcheck);
screen.Exit.addActionListener(check4);
screen.Mainframe.revalidate();
}
//This code adds action listeners to the keypad.
public void addkeypadlisteners(){
BCheck BC = new BCheck();
BClear BC1 = new BClear();
keypad.B1.addActionListener(BC);
keypad.B2.addActionListener(BC);
keypad.B3.addActionListener(BC);
keypad.B4.addActionListener(BC);
keypad.B5.addActionListener(BC);
keypad.B6.addActionListener(BC);
keypad.B7.addActionListener(BC);
keypad.B8.addActionListener(BC);
keypad.B9.addActionListener(BC);
keypad.B0.addActionListener(BC);
keypad.BClear.addActionListener(BC1);
}
//This code checks what button was pressed on the keypad.
public class BCheck implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
JButton b = (JButton)e.getSource();
String label = b.getLabel();
userinput = userinput + label;
//update the text field using the user's input.
screen.Inputfield2.setText(userinput);
}
}
public class BClear implements ActionListener
{
public void actionPerformed( ActionEvent e)
{
//Clear the input field.
userinput = "";
screen.Inputfield2.setText(userinput);
}
}
//Action listener used for the literator pattern
public class Nextcheck implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
IterateUser(BankDatabase.createIterator());
}
}
//Action listener used for the literator pattern
public class Prevcheck implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
prevIterateUser(BankDatabase.createIterator());
}
}
//Action listener used for the literator pattern
public void IterateUser(Iterator Iterator){
if(Iterator.hasNext(position) == true) {
position = position + 1;
//Display the current user in the GUI message labels.
Account AccountItem = (Account)Iterator.next(position);
screen.messageJLabel2.setText("Username: " + AccountItem.getUsername());
screen.messageJLabel3.setText("Avaliable Balance: " + AccountItem.getAvailableBalance());
screen.messageJLabel4.setText("Avaliable Balance: " + AccountItem.getTotalBalance());
}
}
//Action listener used for the literator pattern
public void prevIterateUser(Iterator Iterator){
if(Iterator.hasPrev(position) == true) {
position = position - 1;
Account AccountItem = (Account)Iterator.next(position);
screen.messageJLabel2.setText("Username: " + AccountItem.getUsername());
screen.messageJLabel3.setText("Avaliable Balance: " + AccountItem.getAvailableBalance());
screen.messageJLabel4.setText("Avaliable Balance: " + AccountItem.getTotalBalance());
}
}
//Code used for the Singleton pattern.
public static ATM getinstance() {
if (uniqueinstance == null) {
uniqueinstance = new ATM();
}
return uniqueinstance;
}
}
// end method actionPerformed
// end i
// end class ATM
/**************************************************************************
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/