- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankingApplicationGUI.java
More file actions
Latest commit
185 lines (159 loc) · 7.18 KB
/
Copy pathBankingApplicationGUI.java
File metadata and controls
185 lines (159 loc) · 7.18 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
importjava.awt.BorderLayout;
importjava.awt.Container;
importjava.awt.FlowLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
// Main class for the bancjing application GUI
publicclassBankingApplicationGUI{
publicstaticvoidmain(String[] args){
// Create a BankAccountGUI object and show the menu
BankAccountbank1 = newBankAccount("Cris", "0001");
bank1.showMenu();
}
}
// GUI representing a bank account with GUI components
classBankAccount{
// Variables
intbalance;
intpreviousTransaction;
StringcustomerName;
StringcustomerId;
// GUI components
JFrameframe; // Maine frame of the application
JLabelbalanceLabel; // Label to display the balance
JTextFieldamountField; // Text field to input the amount
JTextAreaoutputArea; // Text area to display messages
// Set customer name and ID
BankAccount(Stringcname, Stringcid){
customerName = cname;
customerId = cid;
}
// Method to deposit the amount into the account
voiddeposit(intamount){
if (amount !=0){ // check if the amount is not zero
balance = balance + amount; // add the amount to the balance
previousTransaction = amount;
updateBalance();
}
}
// Method to withdraw an amount from the account
voidwithdraw(intamount){
if (amount != 0){ // check if the amount is not zero
balance = balance - amount; // subtract the amount from the balance
previousTransaction = -amount;
updateBalance();
}
}
// Method to display the previous transaction
voidgetPreviousTransaction(){
if (previousTransaction > 0){ // check if previous trans. was a deposit
System.out.println("Deposited: "+ previousTransaction);
} elseif(previousTransaction < 0) { // check if privious trans. was a withdraw
System.out.println("Withdrawn: "+ Math.abs(previousTransaction));
} else { // the previous trans. was 0$ or this is a new account
System.out.println("No transaction is occured!");
}
}
// method to update the balance label
voidupdateBalance(){
balanceLabel.setText("Balance: $" + balance);
}
// Method to create and display the GUI
voidshowMenu(){
frame = newJFrame("Banking Application"); // create the main frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set the default close operation
frame.setSize(400, 400); // set the size of the frame
Containercontainer = frame.getContentPane(); // get the content pane of the frame
container.setLayout(newBorderLayout()); // set the layout to BorderLayout
// Top panel with customer information and balance
JPaneltopPanel = newJPanel(); // panel for top section
topPanel.setLayout(newFlowLayout()); // set layout to FlowLayout
JLabelwelcomLabel = newJLabel("Welcome" + customerName); // Create a label with the customer's name
JLabelidLabel = newJLabel("Your ID: " + customerId); // ...
balanceLabel = newJLabel(); // to display ...
// ... add
topPanel.add(welcomLabel);
topPanel.add(idLabel);
topPanel.add(balanceLabel);
container.add(topPanel, BorderLayout.NORTH);
// Center panel with action buttons
JPanelcenterPanel = newJPanel();
centerPanel.setLayout(newGridLayout(5, 1));
// Create acyion buttons
JButtoncheckBalanceButton = newJButton("Check Balance");
JButtondepositButton = newJButton("Deposit");
JButtonwithdrawButton = newJButton("Withdraw");
JButtonpreviousTransactionButton = newJButton("Previouse Transaction");
JButtonexitButton = newJButton("Exit");
// Add buttons to the center panel
centerPanel.add(checkBalanceButton);
centerPanel.add(depositButton);
centerPanel.add(withdrawButton);
centerPanel.add(previousTransactionButton);
centerPanel.add(exitButton);
//... add the center panel to the center section of the container
container.add(centerPanel, BorderLayout.CENTER);
// Bottom panel with amount input and output area
JPanelbottomPanel = newJPanel(); // create a panel to the bottom section
bottomPanel.setLayout(newFlowLayout()); // set the layout to FlowLayout
JLabelamountLabel = newJLabel("Enter the amount: ");
amountField = newJTextField(10); // text field for entering of amount
outputArea = newJTextArea(5, 30); // text area to display message
outputArea.setEditable(false); // make text area - non-editable
// ... set label, field at/and bottom panel
bottomPanel.add(amountLabel);
bottomPanel.add(amountField);
bottomPanel.add(newJScrollPane(outputArea));
container.add(bottomPanel, BorderLayout.SOUTH);
// Add action listeners to the buttons
// Action listener for the check balance button
checkBalanceButton.addActionListener(newActionListener() {
@Override
publicvoidactionPerformed(ActionEvente){
updateBalance();
}
});
// Action listener for the deposit button
depositButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
intamount = Integer.parseInt(amountField.getText()); // get the amount from the text field
deposit(amount); // call the deposit method
outputArea.setText("Deposited: $" + amount); // display the deposited amount
}
});
// Action listener for the previous withdraw button
withdrawButton.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
intamount = Integer.parseInt(amountField.getText()); // get the amount from the text field
withdraw(amount); // call the withdraw method
outputArea.setText("Withdrawn: $" + amount); // display the withdrawn amount
}
});
// Action listener for the previous transaction button
previousTransactionButton.addActionListener(newActionListener() {
@Override
publicvoidactionPerformed(ActionEvente){
getPreviousTransaction();
}
});
// Action listener for exit button
exitButton.addActionListener(newActionListener() {
@Override
publicvoidactionPerformed(ActionEvente){
frame.dispose();
}
});
// Make the frame visible
frame.setVisible(true);
}
}