-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMViewGUI.java
More file actions
642 lines (554 loc) · 28.4 KB
/
ATMViewGUI.java
File metadata and controls
642 lines (554 loc) · 28.4 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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import java.awt.*;
import javax.swing.*;
public class ATMViewGUI extends JFrame {
private JLabel screenLabel, accountInfoLabel, customAmountLabel, transferMessageLabel, transferAccountLabel, transferAmountLabel, depositChequeLabel;
private JPanel screenPanel, keypadPanel, menuPanel, balancePanel, withdrawalPanel, withdrawalMessagePanel, customAmountPanel, transferPanel, depositChequePanel;
private StringBuilder inputBuffer; // Stores user input for account and PIN
private boolean isEnteringAccount = false; // Tracks whether the user is entering account or PIN
private boolean isKeypadEnabled = false;
private boolean isBalanceActive = false; // Controls whether the keypad accepts input
private boolean isWaitingForCheque = false; // Track if waiting for cheque insertion
private boolean isExiting = false;
private String enteredAccount = ""; // Stores the entered account number
private double chequeAmount = 0.0;
private BankDatabase bankDatabase; // Reference to BankDatabase
private Transfer transfer; // Reference to Transfer
public ATMViewGUI() {
bankDatabase = new BankDatabase();
inputBuffer = new StringBuilder();
// Set title and locked size for the frame
setTitle("ATM View");
setSize(800, 600); // Set the fixed size
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false); // Lock the size to prevent resizing
setLayout(new BorderLayout(10, 10));
initScreen();
initKeypad();
initBalancePanel();
initWithdrawalPanel();
initWithdrawalMessagePanel();
initCustomAmountPanel();
initTransferPanel();
initDepositChequePanel();
// Add components to the frame
add(accountInfoLabel, BorderLayout.NORTH); // Top-left account information
add(screenPanel, BorderLayout.CENTER); // Center screen
add(keypadPanel, BorderLayout.SOUTH); // Keypad at the bottom
setVisible(true);
}
private void initScreen() {
// Screen panel to contain the screen label with a fixed size and border
screenPanel = new JPanel(new BorderLayout());
screenPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3)); // Outer border
screenPanel.setPreferredSize(new Dimension(800, 300)); // Fixed size for the screen panel
// Screen to display information
screenLabel = new JLabel("Please insert your card.", SwingConstants.CENTER);
screenLabel.setFont(new Font("Arial", Font.BOLD, 24));
screenLabel.setOpaque(true);
screenLabel.setBackground(Color.WHITE);
screenPanel.add(screenLabel, BorderLayout.CENTER);
// Account info on the top-left
accountInfoLabel = new JLabel("", SwingConstants.LEFT);
accountInfoLabel.setFont(new Font("Arial", Font.PLAIN, 18));
}
private void initKeypad() {
keypadPanel = new JPanel(new GridLayout(4, 4, 5, 5));
String[] keys = {
"1", "2", "3", "Enter",
"4", "5", "6", "Cancel",
"7", "8", "9", "Clear",
".", "0", "00", "OK"
};
for (String key : keys) {
JButton button = new JButton(key);
button.setFont(new Font("Arial", Font.BOLD, 25));
if (key.equals("Enter")) {
button.setBackground(Color.GREEN);
button.addActionListener(e -> handleEnter());
} else if (key.equals("Cancel")) {
button.setBackground(Color.RED);
button.addActionListener(e -> handleCancel());
} else if (key.equals("OK")) {
button.setBackground(Color.BLUE);
button.setForeground(Color.WHITE);
button.addActionListener(e -> handleOk());
} else if (key.equals("Clear")) {
button.setBackground(Color.YELLOW);
button.addActionListener(e -> handleClear());
} else {
button.setBackground(Color.LIGHT_GRAY);
button.addActionListener(e -> handleKeypadInput(key));
}
keypadPanel.add(button);
}
}
private void handleKeypadInput(String key) {
if (!isKeypadEnabled) return; // Ignore if keypad is disabled
// Append input to the buffer
inputBuffer.append(key);
// Update the appropriate label based on the current panel
if (getContentPane().isAncestorOf(customAmountPanel)) {
customAmountLabel.setText("Enter custom amount: " + inputBuffer.toString());
} else {
screenLabel.setText(inputBuffer.toString());
}
if (getContentPane().isAncestorOf(transferPanel)) {
if (transferAccountLabel.getText().contains("Please enter")) {
// Update account number input
transferAccountLabel.setText("Please enter the account number: " + inputBuffer.toString());
} else if (transferAmountLabel.getText().contains("Please enter")) {
// Update transaction amount input
transferAmountLabel.setText("Please enter the transaction amount: " + inputBuffer.toString());
}
}
if (getContentPane().isAncestorOf(depositChequePanel)){
depositChequeLabel.setText("Cheque amount: " + inputBuffer.toString());
}
}
private void handleOk() {
if (isExiting) {
// Exit confirmation state
screenLabel.setText("Please insert your card.");
Timer resetTimer = new Timer(2000, e -> resetATMState());
resetTimer.setRepeats(false);
resetTimer.start();
isExiting = false; // Reset the exit state
return;
}
if (screenLabel.getText().equals("Please insert your card.")){
screenLabel.setText("Enter your account number:");
inputBuffer.setLength(0);
isKeypadEnabled = true;
isEnteringAccount = true;
return;
}
if (getContentPane().isAncestorOf(depositChequePanel)) {
// Check if waiting for cheque insertion
if (isWaitingForCheque) {
// Process cheque insertion
depositChequeLabel.setText("Cheque deposited.");
isWaitingForCheque = false;
// Credit the cheque amount to the account
bankDatabase.credit(Integer.parseInt(accountInfoLabel.getText().substring(9)), chequeAmount);
// Return to the main menu after 3 seconds
Timer returnToMenuTimer = new Timer(3000, ev -> {
remove(depositChequePanel);
add(menuPanel, BorderLayout.CENTER);
screenLabel.setText("Main Menu");
revalidate();
repaint();
});
returnToMenuTimer.setRepeats(false);
returnToMenuTimer.start();
}
}
}
private void handleEnter() {
// Exit confirmation workflow
int customAmount = Integer.parseInt(inputBuffer.toString().trim());
String customerAccount = inputBuffer.toString().trim();
if (screenLabel.getText().equals("Please press enter to exit and take the card.")) {
screenLabel.setText("Please take your card.");
Timer timer = new Timer(1500, e -> resetATMState()); // Delay for 5 seconds
timer.setRepeats(false);
timer.start();
return;
}
if (!isKeypadEnabled) return; // Ignore if keypad is disabled
if (isEnteringAccount) {
// Ensure the account number is not empty
if (inputBuffer.length() == 0) {
screenLabel.setText("Please enter your account number.");
return; // Prevent transitioning to the PIN entry view
}
enteredAccount = inputBuffer.toString();
inputBuffer.setLength(0);
screenLabel.setText("Enter your PIN:");
isEnteringAccount = false;
} else {
// Ensure the PIN is not empty
if (inputBuffer.length() == 0) {
screenLabel.setText("Please enter your PIN.");
return; // Prevent transitioning to the next step
}
String enteredPin = inputBuffer.toString();
inputBuffer.setLength(0);
try {
int accountNumber = Integer.parseInt(enteredAccount);
int pinNumber = Integer.parseInt(enteredPin);
if (bankDatabase.authenticateUser(accountNumber, pinNumber)) {
screenLabel.setText("Login successful!");
isKeypadEnabled = false;
showMainMenu(accountNumber); // Display the main menu
} else {
screenLabel.setText("Invalid account or PIN. Please try again.");
isEnteringAccount = true; // Reset to account number entry
}
} catch (NumberFormatException ex) {
screenLabel.setText("Invalid input. Please enter numbers only.");
isEnteringAccount = true; // Reset to account number entry
}
}
if (getContentPane().isAncestorOf(customAmountPanel)) {
try {
// Clear buffer for future inputs
inputBuffer.setLength(0);
// Validate custom amount
if (customAmount % 100 != 0) {
customAmountLabel.setText("Amount must be a multiple of HK$100.");
} else if (customAmount > bankDatabase.getAvailableBalance(Integer.parseInt(enteredAccount))) {
customAmountLabel.setText("Insufficient funds. Please try again.");
} else {
// Deduct amount and transition to withdrawal message
bankDatabase.debit(Integer.parseInt(enteredAccount), customAmount);
customAmountLabel.setText("Please take your cash: HK$" + customAmount);
// Transition to the withdrawal message panel
Timer timer = new Timer(1500, e -> {
remove(customAmountPanel);
add(withdrawalMessagePanel, BorderLayout.CENTER);
JLabel messageLabel = (JLabel) withdrawalMessagePanel.getComponent(0);
messageLabel.setText("Please take your cash.");
revalidate();
repaint();
// Transition back to main menu
Timer returnToMenu = new Timer(1500, ev -> {
remove(withdrawalMessagePanel);
add(menuPanel, BorderLayout.CENTER);
screenLabel.setText("Main Menu");
revalidate();
repaint();
});
returnToMenu.setRepeats(false);
returnToMenu.start();
});
timer.setRepeats(false);
timer.start();
}
} catch (NumberFormatException ex) {
customAmountLabel.setText("Invalid input. Please enter a valid number.");
}
return;
}
if (getContentPane().isAncestorOf(transferPanel)) {
handleEnterInTransfer(customerAccount); // Handle transfer input flow
} else if (getContentPane().isAncestorOf(depositChequePanel)) {
System.out.println("test1");
handleEnterInDepositCheque(customerAccount);}
}
private void resetATMState() {
screenLabel.setText("Please insert your card.");
inputBuffer.setLength(0);
isEnteringAccount = false;
isKeypadEnabled = false;
revalidate();
repaint();
}
private void handleClear() {
// Clear button should only work during input actions
if (!isKeypadEnabled || screenLabel.getText().equals("Please insert your card.")) {
return; // Do nothing if not in an input state or in the insert card view
}
// Clear the input buffer
inputBuffer.setLength(0);
// Update the appropriate label based on the active panel
if (getContentPane().isAncestorOf(customAmountPanel)) {
customAmountLabel.setText("Enter custom amount: ");
} else if (getContentPane().isAncestorOf(transferPanel)) {
if (transferAccountLabel.getText().contains("Please enter")) {
transferAccountLabel.setText("Please enter the account number: ");
} else if (transferAmountLabel.getText().contains("Please enter")) {
transferAmountLabel.setText("Please enter the transaction amount: ");
}
} else if (getContentPane().isAncestorOf(depositChequePanel)) {
depositChequeLabel.setText("Please input the cheque amount.");
} else {
screenLabel.setText(""); // Clear the screen content
}
}
private void handleCancel() {
// Cancel button is active only in specified panels
if (getContentPane().isAncestorOf(balancePanel) ||
getContentPane().isAncestorOf(withdrawalPanel) ||
getContentPane().isAncestorOf(customAmountPanel) ||
getContentPane().isAncestorOf(withdrawalMessagePanel) ||
getContentPane().isAncestorOf(transferPanel) ||
getContentPane().isAncestorOf(depositChequePanel)){
getContentPane().removeAll();
add(menuPanel, BorderLayout.CENTER);
add(accountInfoLabel, BorderLayout.NORTH);
add(keypadPanel, BorderLayout.SOUTH);
screenLabel.setText("Main Menu");
revalidate();
repaint();
}
// Cancel button does nothing if pressed in other panels
}
private void returnToMenu() {
Timer timer = new Timer(3000, e -> {
remove(transferPanel);
add(menuPanel, BorderLayout.CENTER);
screenLabel.setText("Main Menu");
revalidate();
repaint();
});
timer.setRepeats(false);
timer.start();
}
private void initBalancePanel() {
// Initialize the Balance Panel
balancePanel = new JPanel(new BorderLayout());
JLabel balanceInfoLabel = new JLabel("", SwingConstants.CENTER);
balanceInfoLabel.setFont(new Font("Arial", Font.BOLD, 24));
balancePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3)); // Add a border
balancePanel.setPreferredSize(new Dimension(800, 300)); // Match screen size
balancePanel.add(balanceInfoLabel, BorderLayout.CENTER);
}
private void initWithdrawalPanel() {
// Initialize the Withdrawal Panel
withdrawalPanel = new JPanel(new BorderLayout());
JLabel withdrawalLabel = new JLabel("Please choose the withdrawal option.", SwingConstants.CENTER);
withdrawalLabel.setFont(new Font("Arial", Font.BOLD, 24));
withdrawalPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3)); // Add a border
withdrawalPanel.setPreferredSize(new Dimension(800, 300)); // Match screen size
withdrawalPanel.add(withdrawalLabel, BorderLayout.NORTH);
// Add withdrawal option buttons
JPanel buttonPanel = new JPanel(new GridLayout(2, 2, 10, 10)); // 2x2 layout for buttons
JButton button100 = new JButton("HK$100");
JButton button500 = new JButton("HK$500");
JButton button1000 = new JButton("HK$1000");
JButton customButton = new JButton("Custom Amount");
// Add action listeners for buttons
button100.addActionListener(e -> processWithdrawal(100));
button500.addActionListener(e -> processWithdrawal(500));
button1000.addActionListener(e -> processWithdrawal(1000));
customButton.addActionListener(e -> handleCustomAmount());
buttonPanel.add(button100);
buttonPanel.add(button500);
buttonPanel.add(button1000);
buttonPanel.add(customButton);
withdrawalPanel.add(buttonPanel, BorderLayout.CENTER);
// Add Cancel button to return to the Main Menu
}
private void initWithdrawalMessagePanel() {
// Initialize the Withdrawal Message Panel
withdrawalMessagePanel = new JPanel(new BorderLayout());
JLabel messageLabel = new JLabel("", SwingConstants.CENTER);
messageLabel.setFont(new Font("Arial", Font.BOLD, 24));
withdrawalMessagePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3)); // Add a border
withdrawalMessagePanel.setPreferredSize(new Dimension(800, 300)); // Match screen size
withdrawalMessagePanel.add(messageLabel, BorderLayout.CENTER);
}
private void processWithdrawal(int amount) {
double availableBalance = bankDatabase.getAvailableBalance(Integer.parseInt(enteredAccount));
JLabel messageLabel = (JLabel) withdrawalMessagePanel.getComponent(0);
if (amount > availableBalance) {
messageLabel.setText("Insufficient funds. Please try again.");
} else {
bankDatabase.debit(Integer.parseInt(enteredAccount), amount);
messageLabel.setText("Please take your cash: HK$" + amount);
}
remove(withdrawalPanel);
add(withdrawalMessagePanel, BorderLayout.CENTER);
revalidate();
repaint();
}
private void initCustomAmountPanel() {
// Custom Amount Panel for input and message
customAmountPanel = new JPanel(new BorderLayout());
customAmountLabel = new JLabel("Enter custom amount:", SwingConstants.CENTER);
customAmountLabel.setFont(new Font("Arial", Font.BOLD, 24));
customAmountPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3)); // Add a border
customAmountPanel.setPreferredSize(new Dimension(800, 300)); // Match screen size
customAmountPanel.add(customAmountLabel, BorderLayout.CENTER);
}
private void handleCustomAmount() {
remove(withdrawalPanel);
add(customAmountPanel, BorderLayout.CENTER);
revalidate();
repaint();
customAmountLabel.setText("Enter custom amount:");
inputBuffer.setLength(0);
isKeypadEnabled = true;
}
private void initTransferPanel() {
transferPanel = new JPanel(new BorderLayout());
transferMessageLabel = new JLabel("Transfer Funds", SwingConstants.CENTER);
transferMessageLabel.setFont(new Font("Arial", Font.BOLD, 24));
transferPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
transferPanel.setPreferredSize(new Dimension(800, 300));
transferPanel.add(transferMessageLabel, BorderLayout.NORTH);
// Input section
JPanel inputPanel = new JPanel(new GridLayout(2, 1, 10, 10));
transferAccountLabel = new JLabel("Please enter the account number: ", SwingConstants.CENTER);
transferAccountLabel.setFont(new Font("Arial", Font.BOLD, 18));
transferAmountLabel = new JLabel("", SwingConstants.CENTER); // Initially empty
transferAmountLabel.setFont(new Font("Arial", Font.BOLD, 18));
inputPanel.add(transferAccountLabel);
inputPanel.add(transferAmountLabel);
transferPanel.add(inputPanel, BorderLayout.CENTER);
}
private void handleTransfer() {
remove(menuPanel);
add(transferPanel, BorderLayout.CENTER);
add(accountInfoLabel, BorderLayout.NORTH);
add(keypadPanel, BorderLayout.SOUTH);
transferMessageLabel.setText("Transfer: Enter Account Number");
transferAccountLabel.setText("Please enter the account number:");
transferAmountLabel.setText(""); // Clear the amount field
inputBuffer.setLength(0); // Clear input buffer
isKeypadEnabled = true; // Enable keypad for input
revalidate();
repaint();
}
private void handleEnterInTransfer(String input) {
if (transferAccountLabel.getText().contains("Please enter")) {
// First input: Account number
try {
int targetAccount = Integer.parseInt(input);
if (!bankDatabase.isValidAccount(targetAccount)) {
transferAccountLabel.setText("Invalid account number, returning to menu.");
inputBuffer.setLength(0); // Clear the buffer
returnToMenu(); // Return to main menu after a delay
} else {
transferAccountLabel.setText("Target Account: " + targetAccount);
transferAmountLabel.setText("Please enter the transaction amount:");
inputBuffer.setLength(0); // Clear buffer for the next input
}
} catch (NumberFormatException e) {
transferAccountLabel.setText("Invalid account number.");
inputBuffer.setLength(0); // Clear the buffer
returnToMenu(); // Return to main menu after a delay
}
} else if (transferAmountLabel.getText().contains("Please enter")) {
// Second input: Transaction amount
try {
double amount = Double.parseDouble(input);
int targetAccount = Integer.parseInt(transferAccountLabel.getText().replace("Target Account: ", "").trim());
System.out.println(enteredAccount+" "+amount+" "+targetAccount);
if (amount <= 0 || amount > bankDatabase.getAvailableBalance(Integer.parseInt(accountInfoLabel.getText().substring(9)))) {
transferAmountLabel.setText("Invalid amount, returning to menu.");
inputBuffer.setLength(0); // Clear the buffer
returnToMenu(); // Return to main menu after a delay
}else {
// Successful transfer
transferMessageLabel.setText("Transfer successful! HK$" + amount + " to Account: " + targetAccount);
bankDatabase.debit(Integer.parseInt(accountInfoLabel.getText().substring(9)),amount);
bankDatabase.credit(targetAccount,amount);
// Return to the main menu after a delay
Timer returnToMenu = new Timer(1500, e -> {
remove(transferPanel);
add(menuPanel, BorderLayout.CENTER);
screenLabel.setText("Main Menu");
revalidate();
repaint();
});
returnToMenu.setRepeats(false);
returnToMenu.start();
}
} catch (NumberFormatException e) {
transferAmountLabel.setText("Invalid amount. Please try again:");
}
}
}
private void initDepositChequePanel() {
depositChequePanel = new JPanel(new BorderLayout());
depositChequeLabel = new JLabel("Please input the cheque amount.", SwingConstants.CENTER);
depositChequeLabel.setFont(new Font("Arial", Font.BOLD, 24));
depositChequePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
depositChequePanel.setPreferredSize(new Dimension(800, 300));
depositChequePanel.add(depositChequeLabel, BorderLayout.CENTER);
}
private void handleDepositCheque() {
remove(menuPanel);
add(depositChequePanel, BorderLayout.CENTER);
add(accountInfoLabel, BorderLayout.NORTH);
add(keypadPanel, BorderLayout.SOUTH);
depositChequeLabel.setText("Please input the cheque amount.");
inputBuffer.setLength(0); // Clear input buffer
isKeypadEnabled = true; // Enable keypad for input
isWaitingForCheque = false; // Reset the flag
chequeAmount = 0.0; // Reset cheque amount
revalidate();
repaint();
}
private void handleEnterInDepositCheque(String input) {
try {
chequeAmount = Double.parseDouble(input); // Parse and store the cheque amount
if (chequeAmount <= 0.0 || chequeAmount > 10000.0) {
depositChequeLabel.setText("Invalid amount. Maximum: HK$10000. Try again.");
} else {
depositChequeLabel.setText("Please insert the cheque, then press OK.");
isWaitingForCheque = true; // Set flag to indicate waiting for cheque insertion
}
} catch (NumberFormatException ex) {
depositChequeLabel.setText("Invalid amount. Please input a valid number.");
}
}
private void showMainMenu(int accountNumber) {
// Update account info
accountInfoLabel.setText("Account: " + accountNumber);
// Create the main menu panel with FlowLayout for better spacing
menuPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 20)); // Center-aligned with spacing
JButton balanceButton = new JButton("Balance");
JButton withdrawalButton = new JButton("Withdrawal");
JButton transferButton = new JButton("Transfer");
JButton depositChequeButton = new JButton("Deposit Cheque");
JButton exitButton = new JButton("Exit");
// Set medium-large size for buttons
Dimension buttonSize = new Dimension(200, 50);
balanceButton.setPreferredSize(buttonSize);
withdrawalButton.setPreferredSize(buttonSize);
transferButton.setPreferredSize(buttonSize);
depositChequeButton.setPreferredSize(buttonSize);
exitButton.setPreferredSize(buttonSize); // Set size for Exit button
// Add action listener for the Balance button
balanceButton.addActionListener(e -> {
double availableBalance = bankDatabase.getAvailableBalance(Integer.parseInt(accountInfoLabel.getText().substring(9)));
double totalBalance = bankDatabase.getTotalBalance(Integer.parseInt(accountInfoLabel.getText().substring(9)));
// Set balance info on the Balance Panel
JLabel balanceInfoLabel = (JLabel) balancePanel.getComponent(0);
balanceInfoLabel.setText("<html>Available Balance: HK$" + availableBalance +
"<br>Total Balance: HK$" + totalBalance + "</html>");
// Display the Balance Panel
isBalanceActive = true;
remove(menuPanel);
add(balancePanel, BorderLayout.CENTER);
revalidate();
repaint();
});
depositChequeButton.addActionListener(e -> handleDepositCheque());
transferButton.addActionListener(e -> handleTransfer());
// Add action listener for the Withdrawal button
withdrawalButton.addActionListener(e -> {
remove(menuPanel);
add(withdrawalPanel, BorderLayout.CENTER);
screenLabel.setText("Please choose the withdrawal option.");
revalidate();
repaint();
});
// Add action listener for the Exit button
exitButton.addActionListener(e -> {
remove(menuPanel);
remove(accountInfoLabel);
add(screenPanel);
screenLabel.setText("Please press ok to exit and take the card.");
isExiting = true; // Set the exiting state
revalidate();
repaint();
});
// Add buttons to the menu panel
menuPanel.add(balanceButton);
menuPanel.add(withdrawalButton);
menuPanel.add(transferButton);
menuPanel.add(depositChequeButton);
menuPanel.add(exitButton);
// Replace the screen with the main menu
remove(screenPanel);
add(menuPanel, BorderLayout.CENTER);
revalidate();
repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ATMViewGUI::new);
}
}