-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithdrawScreen.java
More file actions
109 lines (88 loc) · 3.87 KB
/
WithdrawScreen.java
File metadata and controls
109 lines (88 loc) · 3.87 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* WithdrawScreen class - Screen for withdrawing money
* Demonstrates: GUI components, event handling, exception handling
*/
public class WithdrawScreen extends JDialog {
private BankAccount account;
private BasicATM atm;
private JTextField amountField;
private JLabel statusLabel;
private MainScreen parentScreen;
public WithdrawScreen(MainScreen parent, BankAccount account, BasicATM atm) {
super(parent, "Withdraw Cash", true);
this.account = account;
this.atm = atm;
this.parentScreen = parent;
setSize(400, 300);
setLocationRelativeTo(parent);
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Title
JLabel titleLabel = new JLabel("Withdraw Cash", SwingConstants.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 18));
mainPanel.add(titleLabel, BorderLayout.NORTH);
// Center Panel for Input and Quick Buttons
JPanel centerPanel = new JPanel(new BorderLayout(10, 10));
JPanel inputPanel = new JPanel(new GridLayout(4, 1, 5, 5));
JLabel balanceLabel = new JLabel("Available Balance: $" +
String.format("%.2f", account.getBalance()));
JLabel amountLabel = new JLabel("Enter amount to withdraw:");
amountField = new JTextField();
statusLabel = new JLabel("", SwingConstants.CENTER);
statusLabel.setForeground(Color.RED);
inputPanel.add(balanceLabel);
inputPanel.add(amountLabel);
inputPanel.add(amountField);
inputPanel.add(statusLabel);
JPanel quickAmountPanel = new JPanel(new GridLayout(1, 3, 10, 0));
JButton btn20 = new JButton("$20");
JButton btn50 = new JButton("$50");
JButton btn100 = new JButton("$100");
quickAmountPanel.add(btn20);
quickAmountPanel.add(btn50);
quickAmountPanel.add(btn100);
centerPanel.add(inputPanel, BorderLayout.CENTER);
centerPanel.add(quickAmountPanel, BorderLayout.SOUTH);
// Bottom buttons
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton withdrawButton = new JButton("Withdraw");
JButton cancelButton = new JButton("Cancel");
buttonPanel.add(withdrawButton);
buttonPanel.add(cancelButton);
// Add listeners
btn20.addActionListener(e -> amountField.setText("20"));
btn50.addActionListener(e -> amountField.setText("50"));
btn100.addActionListener(e -> amountField.setText("100"));
withdrawButton.addActionListener(e -> handleWithdrawal());
cancelButton.addActionListener(e -> dispose());
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
add(mainPanel);
}
private void handleWithdrawal() {
try {
double amount = Double.parseDouble(amountField.getText().trim());
if (amount <= 0) {
statusLabel.setText("Please enter a valid amount");
return;
}
if (atm.withdrawCash(account, amount)) {
JOptionPane.showMessageDialog(this,
"Successfully withdrew $" + String.format("%.2f", amount),
"Success", JOptionPane.INFORMATION_MESSAGE);
parentScreen.updateBalanceLabel();
dispose();
} else {
statusLabel.setText("Withdrawal failed. Check balance or limits.");
}
} catch (NumberFormatException ex) {
statusLabel.setText("Please enter a valid number");
} catch (Exception ex) {
statusLabel.setText("Error: " + ex.getMessage());
}
}
}