-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankingApplicationGUI.java
More file actions
185 lines (159 loc) · 7.18 KB
/
BankingApplicationGUI.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
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
// Main class for the bancjing application GUI
public class BankingApplicationGUI{
public static void main(String[] args){
// Create a BankAccountGUI object and show the menu
BankAccount bank1 = new BankAccount("Cris", "0001");
bank1.showMenu();
}
}
// GUI representing a bank account with GUI components
class BankAccount{
// Variables
int balance;
int previousTransaction;
String customerName;
String customerId;
// GUI components
JFrame frame; // Maine frame of the application
JLabel balanceLabel; // Label to display the balance
JTextField amountField; // Text field to input the amount
JTextArea outputArea; // Text area to display messages
// Set customer name and ID
BankAccount(String cname, String cid){
customerName = cname;
customerId = cid;
}
// Method to deposit the amount into the account
void deposit(int amount){
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
void withdraw(int amount){
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
void getPreviousTransaction(){
if (previousTransaction > 0){ // check if previous trans. was a deposit
System.out.println("Deposited: "+ previousTransaction);
} else if(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
void updateBalance(){
balanceLabel.setText("Balance: $" + balance);
}
// Method to create and display the GUI
void showMenu(){
frame = new JFrame("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
Container container = frame.getContentPane(); // get the content pane of the frame
container.setLayout(new BorderLayout()); // set the layout to BorderLayout
// Top panel with customer information and balance
JPanel topPanel = new JPanel(); // panel for top section
topPanel.setLayout(new FlowLayout()); // set layout to FlowLayout
JLabel welcomLabel = new JLabel("Welcome" + customerName); // Create a label with the customer's name
JLabel idLabel = new JLabel("Your ID: " + customerId); // ...
balanceLabel = new JLabel(); // to display ...
// ... add
topPanel.add(welcomLabel);
topPanel.add(idLabel);
topPanel.add(balanceLabel);
container.add(topPanel, BorderLayout.NORTH);
// Center panel with action buttons
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(5, 1));
// Create acyion buttons
JButton checkBalanceButton = new JButton("Check Balance");
JButton depositButton = new JButton("Deposit");
JButton withdrawButton = new JButton("Withdraw");
JButton previousTransactionButton = new JButton("Previouse Transaction");
JButton exitButton = new JButton("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
JPanel bottomPanel = new JPanel(); // create a panel to the bottom section
bottomPanel.setLayout(new FlowLayout()); // set the layout to FlowLayout
JLabel amountLabel = new JLabel("Enter the amount: ");
amountField = new JTextField(10); // text field for entering of amount
outputArea = new JTextArea(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(new JScrollPane(outputArea));
container.add(bottomPanel, BorderLayout.SOUTH);
// Add action listeners to the buttons
// Action listener for the check balance button
checkBalanceButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
updateBalance();
}
});
// Action listener for the deposit button
depositButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
int amount = 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(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
int amount = 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(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
getPreviousTransaction();
}
});
// Action listener for exit button
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
frame.dispose();
}
});
// Make the frame visible
frame.setVisible(true);
}
}