-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMGUI.java
More file actions
213 lines (172 loc) · 6.66 KB
/
ATMGUI.java
File metadata and controls
213 lines (172 loc) · 6.66 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class BankAccount {
private int balance;
public BankAccount(int initialBalance) {
this.balance = initialBalance;
}
public int getBalance() {
return balance;
}
public void deposit(int amount) {
balance += amount;
}
public boolean withdraw(int amount) {
if (amount <= balance) {
balance -= amount;
return true; // Withdrawal successful
} else {
return false; // Insufficient balance
}
}
}
class ATM {
private BankAccount bankAccount;
public ATM(BankAccount bankAccount) {
this.bankAccount = bankAccount;
}
public void deposit(int amount) {
bankAccount.deposit(amount);
}
public boolean withdraw(int amount) {
return bankAccount.withdraw(amount);
}
public int checkBalance() {
return bankAccount.getBalance();
}
}
public class ATMGUI extends JFrame {
private ATM atm;
private JTextField amountField;
private JTextArea resultArea;
public ATMGUI(ATM atm) {
this.atm = atm;
setTitle("ATM Machine");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createUI();
}
private void createUI() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 2));
JLabel amountLabel = new JLabel(" Enter Amount: ");
amountLabel.setFont(new Font("sans-serif",Font.BOLD,14));;
amountField = new JTextField();
amountField.setFont(new Font("sans-serif", Font.PLAIN, 15));
JButton depositButton = new JButton("Deposit");
JButton withdrawButton = new JButton("Withdraw");
JButton balanceButton = new JButton("Check Balance");
resultArea = new JTextArea();
resultArea.setFont(new Font("sans-serif",Font.PLAIN,14));
resultArea.setEditable(false); // can't make any change by the user
// colors
panel.setBackground(Color.LIGHT_GRAY);
amountLabel.setForeground(Color.WHITE);
amountField.setBackground(Color.WHITE);
depositButton.setBackground(new Color(34, 139, 34)); // Green color
withdrawButton.setBackground(new Color(255, 69, 0)); // Red-Orange color
balanceButton.setBackground(new Color(70, 130, 180)); // Steel Blue color
depositButton.setForeground(Color.WHITE);
withdrawButton.setForeground(Color.WHITE);
balanceButton.setForeground(Color.WHITE);
// button details
Font buttonFont = new Font("sans-serif", Font.BOLD, 14);
depositButton.setFont(buttonFont);
withdrawButton.setFont(buttonFont);
balanceButton.setFont(buttonFont);
depositButton.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
withdrawButton.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
balanceButton.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
// hover effect
depositButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
depositButton.setBackground(new Color(0, 128, 0));
}
public void mouseExited(java.awt.event.MouseEvent evt) {
depositButton.setBackground(new Color(34, 139, 34));
}
});
withdrawButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
withdrawButton.setBackground(new Color(255, 99, 71));
}
public void mouseExited(java.awt.event.MouseEvent evt) {
withdrawButton.setBackground(new Color(255, 69, 0));
}
});
balanceButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
balanceButton.setBackground(new Color(100, 149, 237));
}
public void mouseExited(java.awt.event.MouseEvent evt) {
balanceButton.setBackground(new Color(70, 130, 180));
}
});
depositButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
handleDeposit();
}
});
withdrawButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
handleWithdraw();
}
});
balanceButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
handleCheckBalance();
}
});
panel.add(amountLabel);
panel.add(amountField);
panel.add(depositButton);
panel.add(withdrawButton);
panel.add(balanceButton);
panel.add(resultArea);
add(panel);
}
private void handleDeposit() {
try {
int amount = Integer.parseInt(amountField.getText());
atm.deposit(amount);
updateResult("Deposit successful😄.New balance: " + atm.checkBalance());
} catch (NumberFormatException ex) {
updateResult("Invalid input. Please enter a valid amount.");
}
}
private void handleWithdraw() {
try {
int amount = Integer.parseInt(amountField.getText());
boolean success = atm.withdraw(amount);
if (success) {
updateResult("Withdrawal successful😢. New balance: " + atm.checkBalance());
} else {
updateResult("Insufficient balance😂. Withdrawal failed.");
}
} catch (NumberFormatException ex) {
updateResult("Invalid input. Please enter a valid amount.");
}
}
private void handleCheckBalance() {
int balance = atm.checkBalance();
updateResult("Current balance: " + balance);
}
private void updateResult(String message) {
resultArea.setText(message);
}
public static void main(String[] args) {
BankAccount bankAccount = new BankAccount(0);
ATM atm = new ATM(bankAccount);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ATMGUI(atm).setVisible(true);
}
});
}
}