-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankingSystem.java
More file actions
484 lines (394 loc) · 18.3 KB
/
Copy pathBankingSystem.java
File metadata and controls
484 lines (394 loc) · 18.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
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
import java.awt.*; // import karta abstract window toolkit package for creating gui elements
import java.awt.event.ActionEvent; // actionevent class for handling button click
import java.util.ArrayList; // arraylist and list for creating and managing list of trans.
import java.util.HashMap; //hashmap and map for storing and retrieving acc. by account number
import java.util.List;
import java.util.Map;
import javax.swing.*; //import the swing gui framework components
// defines class bankingsystem that recieve from jframe (window in gui a-pplication)
public class BankingSystem extends JFrame {
// accounts - hashmap to store account object with the account no. as his key
// transaction - arraylist to store all transaction object
// tabbedpane - it is going to hold diifernt tabs of the application
private final Map<String, Account> accounts = new HashMap<>();
private final List<Transaction> transactions = new ArrayList<>();
private final JTabbedPane tabbedPane;
//Create Account Tab
// create account tab to collect name, account number, and initial balance // button to sumit the account creation form
private JTextField createNameField;
private JTextField createAccountNumberField;
private JTextField createInitialBalanceField;
private JButton createAccountButton;
//Deposit Tab
// collect ccount number and deposit amount and button to sumit deposit transaction
private JTextField depositAccountNumberField;
private JTextField depositAmountField;
private JButton depositButton;
// Withdraw Tab
// collect account number and withdrawal amount and button to sumit the withdrawal transaction
private JTextField withdrawAccountNumberField;
private JTextField withdrawAmountField;
private JButton withdrawButton;
// Transaction History
private JTextArea transactionHistoryArea;
private JTextField historyAccountNumberField;
private JButton showHistoryButton;
// Constructor for BankingSystem class
// window title to Banking System
// window size (600x500px)
// the default close operation to exit the application when the window is closed
// Centers the window on the screen (setLocationRelativeTo(null)
public BankingSystem() {
setTitle("Banking System");
setSize(600, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
tabbedPane = new JTabbedPane();
// Creating Tabs
// Creates new JTabbedPane for tabbed interface
// ya add bhi kar dega 4 tabs with their respective panels
// add tabbed pane to the main window
tabbedPane.addTab("Create Account", createCreateAccountPanel());
tabbedPane.addTab("Deposit", createDepositPanel());
tabbedPane.addTab("Withdraw", createWithdrawPanel());
tabbedPane.addTab("Transaction History", createTransactionHistoryPanel());
add(tabbedPane);
}
// ui panel creation method
// new panel banayega with GridBagLayout for flexible component positioning
// banayega GridBagConstraints for controlling the layout
// ya set kar dega 5px padding around components
// ya set components to anchor to the west (left) side
private JPanel createCreateAccountPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.WEST;
// Naming the fields
// set grid postion(o,o) for the label
// add kar label for customer name
// move grid pos. (1,0) for text field
// create kar text field for createName input width of 20 columns
// add text field to the panel
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JLabel("Customer Name:"), gbc);
gbc.gridx = 1;
createNameField = new JTextField(20);
panel.add(createNameField, gbc);
// acc no. field
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(new JLabel("Account Number:"), gbc);
gbc.gridx = 1;
createAccountNumberField = new JTextField(20);
panel.add(createAccountNumberField, gbc);
// initial balance field
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(new JLabel("Initial Balance:"), gbc);
gbc.gridx = 1;
createInitialBalanceField = new JTextField(20);
panel.add(createInitialBalanceField, gbc);
// create balance field
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
createAccountButton = new JButton("Create Account");
createAccountButton.addActionListener((ActionEvent e) -> {
createAccount();
});
panel.add(createAccountButton, gbc);
return panel;
}
private JPanel createDepositPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.WEST;
// Account Number field
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JLabel("Account Number:"), gbc);
gbc.gridx = 1;
depositAccountNumberField = new JTextField(20);
panel.add(depositAccountNumberField, gbc);
// Amount field
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(new JLabel("Amount:"), gbc);
gbc.gridx = 1;
depositAmountField = new JTextField(20);
panel.add(depositAmountField, gbc);
// Deposit button
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
depositButton = new JButton("Deposit");
depositButton.addActionListener((ActionEvent e) -> {
deposit();
});
panel.add(depositButton, gbc);
return panel;
}
private JPanel createWithdrawPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.WEST;
// Account Number field
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JLabel("Account Number:"), gbc);
gbc.gridx = 1;
withdrawAccountNumberField = new JTextField(20);
panel.add(withdrawAccountNumberField, gbc);
// Amount field
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(new JLabel("Amount:"), gbc);
gbc.gridx = 1;
withdrawAmountField = new JTextField(20);
panel.add(withdrawAmountField, gbc);
// Withdraw button
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
withdrawButton = new JButton("Withdraw");
withdrawButton.addActionListener((ActionEvent e) -> {
withdraw();
});
panel.add(withdrawButton, gbc);
return panel;
}
private JPanel createTransactionHistoryPanel() {
JPanel panel = new JPanel(new BorderLayout(5, 5));
// Top panel for account number input
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
topPanel.add(new JLabel("Account Number:"));
historyAccountNumberField = new JTextField(15);
topPanel.add(historyAccountNumberField);
showHistoryButton = new JButton("Show History");
showHistoryButton.addActionListener((ActionEvent e) -> {
showTransactionHistory();
});
topPanel.add(showHistoryButton);
panel.add(topPanel, BorderLayout.NORTH);
// Transaction history area
transactionHistoryArea = new JTextArea();
transactionHistoryArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(transactionHistoryArea);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private void createAccount() {
String name = createNameField.getText().trim();
String accountNumber = createAccountNumberField.getText().trim();
String initialBalanceStr = createInitialBalanceField.getText().trim();
if (name.isEmpty() || accountNumber.isEmpty() || initialBalanceStr.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill in all fields", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
double initialBalance = Double.parseDouble(initialBalanceStr);
if (initialBalance < 0) {
JOptionPane.showMessageDialog(this, "Initial balance cannot be negative", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (accounts.containsKey(accountNumber)) {
JOptionPane.showMessageDialog(this, "Account number already exists", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
Account account = new Account(name, accountNumber, initialBalance);
accounts.put(accountNumber, account);
// Record the transaction
Transaction transaction = new Transaction(accountNumber, "Account Creation", initialBalance, initialBalance);
transactions.add(transaction);
JOptionPane.showMessageDialog(this, """
Account created successfully!
Name: """ + name + "\n" +
"Account Number: " + accountNumber + "\n" +
"Initial Balance: " + initialBalance, "Success", JOptionPane.INFORMATION_MESSAGE);
// Clear fields
createNameField.setText("");
createAccountNumberField.setText("");
createInitialBalanceField.setText("");
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Please enter a valid number for initial balance", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void deposit() {
String accountNumber = depositAccountNumberField.getText().trim();
String amountStr = depositAmountField.getText().trim();
if (accountNumber.isEmpty() || amountStr.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill in all fields", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (!accounts.containsKey(accountNumber)) {
JOptionPane.showMessageDialog(this, "Account not found", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
double amount = Double.parseDouble(amountStr);
if (amount <= 0) {
JOptionPane.showMessageDialog(this, "Amount must be positive", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
Account account = accounts.get(accountNumber);
double newBalance = account.getBalance() + amount;
account.setBalance(newBalance);
// Record the transaction
Transaction transaction = new Transaction(accountNumber, "Deposit", amount, newBalance);
transactions.add(transaction);
JOptionPane.showMessageDialog(this,
"""
Deposit successful!
Account: """ + accountNumber + "\n" +
"Amount deposited: " + amount + "\n" +
"New balance: " + newBalance, "Success", JOptionPane.INFORMATION_MESSAGE);
// Clear fields
depositAccountNumberField.setText("");
depositAmountField.setText("");
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Please enter a valid number for amount", "Error", JOptionPane.ERROR_MESSAGE);
}
}
/**
*
*/
private void withdraw() {
String accountNumber = withdrawAccountNumberField.getText().trim();
String amountStr = withdrawAmountField.getText().trim();
if (accountNumber.isEmpty() || amountStr.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill in all fields", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (!accounts.containsKey(accountNumber)) {
JOptionPane.showMessageDialog(this, "Account not found", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
try {
double amount = Double.parseDouble(amountStr);
if (amount <= 0) {
JOptionPane.showMessageDialog(this, "Amount must be positive", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
Account account = accounts.get(accountNumber);
if (account.getBalance() < amount) {
JOptionPane.showMessageDialog(this, "Insufficient funds", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
double newBalance = account.getBalance() - amount;
account.setBalance(newBalance);
// Record the transaction
Transaction transaction = new Transaction(accountNumber, "Withdrawal", -amount, newBalance);
transactions.add(transaction);
JOptionPane.showMessageDialog(this,
"""
Withdrawal successful!
Account: """ + accountNumber + "\n" +
"Amount withdrawn: " + amount + "\n" +
"New balance: " + newBalance, "Success", JOptionPane.INFORMATION_MESSAGE);
// Clear fields
withdrawAccountNumberField.setText("");
withdrawAmountField.setText("");
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Please enter a valid number for amount", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void showTransactionHistory() {
String accountNumber = historyAccountNumberField.getText().trim();
if (accountNumber.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter an account number", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (!accounts.containsKey(accountNumber)) {
JOptionPane.showMessageDialog(this, "Account not found", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
Account account = accounts.get(accountNumber);
transactionHistoryArea.setText("");
// Add account info
transactionHistoryArea.append("Account Holder: " + account.getName() + "\n");
transactionHistoryArea.append("Account Number: " + account.getAccountNumber() + "\n");
transactionHistoryArea.append("Current Balance: " + account.getBalance() + "\n\n");
transactionHistoryArea.append("Transaction History:\n");
// Add transactions
boolean found = false;
for (Transaction transaction : transactions) {
if (transaction.getAccountNumber().equals(accountNumber)) {
found = true;
transactionHistoryArea.append(String.format(
"Type: %-15s Amount: %10.2f Balance: %10.2f\n",
transaction.getType(),
transaction.getAmount(),
transaction.getBalanceAfter()
));
}
}
if (!found) {
transactionHistoryArea.append("No transactions found for this account.\n");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new BankingSystem().setVisible(true);
});
}
// Account class to store account information
private class Account {
private final String name;
private final String accountNumber;
private double balance;
public Account(String name, String accountNumber, double balance) {
this.name = name;
this.accountNumber = accountNumber;
this.balance = balance;
}
public String getName() {
return name;
}
public String getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
// Transaction class to store transaction information
private class Transaction {
private final String accountNumber;
private final String type;
private final double amount;
private final double balanceAfter;
private final String timestamp;
public Transaction(String accountNumber, String type, double amount, double balanceAfter) {
this.accountNumber = accountNumber;
this.type = type;
this.amount = amount;
this.balanceAfter = balanceAfter;
this.timestamp = java.time.LocalDateTime.now().toString();
}
public String getAccountNumber() {
return accountNumber;
}
public String getType() {
return type;
}
public double getAmount() {
return amount;
}
public double getBalanceAfter() {
return balanceAfter;
}
public String getTimestamp() {
return timestamp;
}
}
}