-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMinterface.java
More file actions
120 lines (101 loc) · 3.71 KB
/
Copy pathATMinterface.java
File metadata and controls
120 lines (101 loc) · 3.71 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public double getBalance() {
return balance;
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
class ATM {
private BankAccount account;
private JFrame frame;
private JTextField amountField;
private JLabel balanceLabel;
public ATM(BankAccount account) {
this.account = account;
createUI();
}
private void createUI() {
frame = new JFrame("ATM Machine");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(5, 1, 10, 10));
balanceLabel = new JLabel("Balance: $" + account.getBalance(), SwingConstants.CENTER);
JButton withdrawButton = new JButton("Withdraw");
JButton depositButton = new JButton("Deposit");
JButton checkBalanceButton = new JButton("Check Balance");
amountField = new JTextField();
withdrawButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
performWithdraw();
}
});
depositButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
performDeposit();
}
});
checkBalanceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateBalance();
}
});
frame.add(balanceLabel);
frame.add(amountField);
frame.add(withdrawButton);
frame.add(depositButton);
frame.add(checkBalanceButton);
frame.setVisible(true);
}
private void performWithdraw() {
try {
double amount = Double.parseDouble(amountField.getText());
if (account.withdraw(amount)) {
JOptionPane.showMessageDialog(frame, "Withdrawal Successful!");
} else {
JOptionPane.showMessageDialog(frame, "Insufficient funds or invalid amount!", "Error", JOptionPane.ERROR_MESSAGE);
}
updateBalance();
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame, "Please enter a valid amount!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void performDeposit() {
try {
double amount = Double.parseDouble(amountField.getText());
if (amount > 0) {
account.deposit(amount);
JOptionPane.showMessageDialog(frame, "Deposit Successful!");
updateBalance();
} else {
JOptionPane.showMessageDialog(frame, "Enter a valid amount!", "Error", JOptionPane.ERROR_MESSAGE);
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame, "Please enter a valid amount!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void updateBalance() {
balanceLabel.setText("Balance: $" + account.getBalance());
}
public static void main(String[] args) {
BankAccount account = new BankAccount(1000.0); // Initial balance
new ATM(account);
}
}