-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMGUI.java
More file actions
328 lines (268 loc) · 11.3 KB
/
ATMGUI.java
File metadata and controls
328 lines (268 loc) · 11.3 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
} else {
return false; // Insufficient funds or invalid amount
}
}
}
class ATM {
private BankAccount account;
public ATM(BankAccount account) {
this.account = account;
}
public boolean withdraw(double amount) {
return account.withdraw(amount);
}
public void deposit(double amount) {
account.deposit(amount);
}
public double checkBalance() {
return account.getBalance();
}
}
public class ATMGUI {
private ATM atm;
private JFrame frame;
private JPanel loginPanel, mainPanel, historyPanel;
private CardLayout cardLayout;
private JLabel resultLabel, balanceLabel;
private JPasswordField pinField;
private List<String> transactionHistory = new ArrayList<>();
private final String USER_PIN = "1234"; // Demo PIN
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public ATMGUI(ATM atm) {
this.atm = atm;
initializeGUI();
}
private void initializeGUI() {
frame = new JFrame("ATM Machine");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(450, 350);
frame.setLocationRelativeTo(null);
cardLayout = new CardLayout();
JPanel container = new JPanel(cardLayout);
createLoginPanel();
createMainPanel();
createHistoryPanel();
container.add(loginPanel, "login");
container.add(mainPanel, "main");
container.add(historyPanel, "history");
frame.add(container);
cardLayout.show(container, "login");
frame.setVisible(true);
}
private void createLoginPanel() {
loginPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel pinLabel = new JLabel("Enter PIN:");
pinField = new JPasswordField(10);
JButton loginButton = new JButton("Login");
JLabel loginMsg = new JLabel("");
loginMsg.setForeground(Color.RED);
gbc.gridx = 0; gbc.gridy = 0;
loginPanel.add(pinLabel, gbc);
gbc.gridx = 1; gbc.gridy = 0;
loginPanel.add(pinField, gbc);
gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
loginPanel.add(loginButton, gbc);
gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 2;
loginPanel.add(loginMsg, gbc);
pinField.addActionListener(e -> loginButton.doClick());
loginButton.addActionListener(e -> {
String pin = new String(pinField.getPassword()).trim();
if (pin.equals(USER_PIN)) {
pinField.setText("");
loginMsg.setText("");
cardLayout.show((Container) pinField.getParent().getParent(), "main");
updateBalanceDisplay();
} else {
loginMsg.setText("Invalid PIN. Try again.");
pinField.setText("");
pinField.requestFocus();
}
});
}
private void createMainPanel() {
mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(8, 8, 8, 8);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
JButton withdrawButton = new JButton("Withdraw");
JButton depositButton = new JButton("Deposit");
JButton balanceButton = new JButton("Check Balance");
JButton historyButton = new JButton("Transaction History");
JButton logoutButton = new JButton("Logout");
resultLabel = new JLabel("", SwingConstants.CENTER);
balanceLabel = new JLabel("Current balance: $" + String.format("%.2f", atm.checkBalance()), SwingConstants.CENTER);
Font labelFont = new Font("Arial", Font.BOLD, 14);
balanceLabel.setFont(labelFont);
resultLabel.setForeground(Color.BLUE);
int yPos = 0;
gbc.gridx = 0; gbc.gridy = yPos++;
mainPanel.add(balanceLabel, gbc);
gbc.gridx = 0; gbc.gridy = yPos++;
mainPanel.add(withdrawButton, gbc);
gbc.gridx = 0; gbc.gridy = yPos++;
mainPanel.add(depositButton, gbc);
gbc.gridx = 0; gbc.gridy = yPos++;
mainPanel.add(balanceButton, gbc);
gbc.gridx = 0; gbc.gridy = yPos++;
mainPanel.add(historyButton, gbc);
gbc.gridx = 0; gbc.gridy = yPos++;
mainPanel.add(logoutButton, gbc);
gbc.gridx = 0; gbc.gridy = yPos++;
mainPanel.add(resultLabel, gbc);
withdrawButton.addActionListener(e -> performWithdrawal());
depositButton.addActionListener(e -> performDeposit());
balanceButton.addActionListener(e -> {
updateBalanceDisplay();
resultLabel.setText("Balance checked successfully");
});
historyButton.addActionListener(e -> showHistory());
logoutButton.addActionListener(e -> {
resultLabel.setText("");
cardLayout.show((Container) mainPanel.getParent(), "login");
});
}
private void createHistoryPanel() {
historyPanel = new JPanel(new BorderLayout(10, 10));
historyPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JTextArea historyArea = new JTextArea(15, 30);
historyArea.setEditable(false);
historyArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
JButton backButton = new JButton("Back to Main Menu");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(backButton);
historyPanel.add(new JLabel("Transaction History:", SwingConstants.CENTER), BorderLayout.NORTH);
historyPanel.add(new JScrollPane(historyArea), BorderLayout.CENTER);
historyPanel.add(buttonPanel, BorderLayout.SOUTH);
backButton.addActionListener(e -> {
cardLayout.show((Container) historyPanel.getParent(), "main");
});
}
private void performWithdrawal() {
String amountString = JOptionPane.showInputDialog(frame,
"Enter amount to withdraw:", "Withdrawal", JOptionPane.QUESTION_MESSAGE);
if (amountString == null) return;
try {
double amount = Double.parseDouble(amountString.trim());
if (amount <= 0) {
resultLabel.setText("Please enter a positive amount.");
return;
}
if (amount > 1000) {
resultLabel.setText("Withdrawal limit exceeded. Max: $1000");
return;
}
boolean success = atm.withdraw(amount);
if (success) {
updateBalanceDisplay();
resultLabel.setText("Withdrawal successful: $" + String.format("%.2f", amount));
addTransaction("WITHDRAW", amount);
} else {
resultLabel.setText("Insufficient funds.");
}
} catch (NumberFormatException ex) {
resultLabel.setText("Invalid amount. Please enter numbers only.");
}
}
private void performDeposit() {
String amountString = JOptionPane.showInputDialog(frame,
"Enter amount to deposit:", "Deposit", JOptionPane.QUESTION_MESSAGE);
if (amountString == null) return;
try {
double amount = Double.parseDouble(amountString.trim());
if (amount <= 0) {
resultLabel.setText("Please enter a positive amount.");
return;
}
if (amount > 10000) {
resultLabel.setText("Deposit limit exceeded. Max: $10,000");
return;
}
atm.deposit(amount);
updateBalanceDisplay();
resultLabel.setText("Deposit successful: $" + String.format("%.2f", amount));
addTransaction("DEPOSIT", amount);
} catch (NumberFormatException ex) {
resultLabel.setText("Invalid amount. Please enter numbers only.");
}
}
private void updateBalanceDisplay() {
balanceLabel.setText("Current balance: $" + String.format("%.2f", atm.checkBalance()));
}
private void addTransaction(String type, double amount) {
String timestamp = dateFormat.format(new Date());
String transaction = String.format("%s - %s: $%.2f", timestamp, type, amount);
transactionHistory.add(transaction);
}
private void showHistory() {
Component[] components = historyPanel.getComponents();
JTextArea historyArea = null;
for (Component comp : components) {
if (comp instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane) comp;
Component view = scrollPane.getViewport().getView();
if (view instanceof JTextArea) {
historyArea = (JTextArea) view;
break;
}
}
}
if (historyArea != null) {
StringBuilder sb = new StringBuilder();
if (transactionHistory.isEmpty()) {
sb.append("No transactions yet.\n");
} else {
sb.append("Transaction History:\n\n");
for (int i = transactionHistory.size() - 1; i >= 0; i--) {
sb.append(transactionHistory.get(i)).append("\n");
}
}
sb.append("\nCurrent Balance: $").append(String.format("%.2f", atm.checkBalance()));
historyArea.setText(sb.toString());
historyArea.setCaretPosition(0);
}
cardLayout.show((Container) historyPanel.getParent(), "history");
}
public static void main(String[] args) {
// Fixed: Removed the problematic setLookAndFeel line
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
BankAccount account = new BankAccount(1000.00);
ATM atm = new ATM(account);
new ATMGUI(atm);
});
}
}