-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMInterface.java
More file actions
131 lines (116 loc) · 4.42 KB
/
Copy pathATMInterface.java
File metadata and controls
131 lines (116 loc) · 4.42 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
public class ATMInterface {
private JFrame frame;
private JPanel panel;
private JTextArea transactionHistoryTextArea;
private JTextField inputField;
private JButton withdrawButton;
private JButton depositButton;
private JButton transferButton;
private JButton quitButton;
private double balance = 1000.0; // Initial balance
private List<String> transactionHistory = new ArrayList<>();
public ATMInterface() {
frame = new JFrame("ATM Interface");
panel = new JPanel();
transactionHistoryTextArea = new JTextArea(10, 30);
inputField = new JTextField(10);
withdrawButton = new JButton("Withdraw");
depositButton = new JButton("Deposit");
transferButton = new JButton("Transfer");
quitButton = new JButton("Quit");
setupUI();
}
private void setupUI() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
panel.setLayout(new FlowLayout());
withdrawButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double amount = Double.parseDouble(inputField.getText());
if (amount > 0 && amount <= balance) {
balance -= amount;
transactionHistory.add("Withdraw: -$" + amount);
updateTransactionHistory();
updateBalanceLabel();
} else {
JOptionPane.showMessageDialog(frame, "Invalid withdrawal amount.");
}
inputField.setText("");
}
});
depositButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double amount = Double.parseDouble(inputField.getText());
if (amount > 0) {
balance += amount;
transactionHistory.add("Deposit: +$" + amount);
updateTransactionHistory();
updateBalanceLabel();
} else {
JOptionPane.showMessageDialog(frame, "Invalid deposit amount.");
}
inputField.setText("");
}
});
transferButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double amount = Double.parseDouble(inputField.getText());
if (amount > 0 && amount <= balance) {
balance -= amount;
transactionHistory.add("Transfer: -$" + amount);
updateTransactionHistory();
updateBalanceLabel();
} else {
JOptionPane.showMessageDialog(frame, "Invalid transfer amount.");
}
inputField.setText("");
}
});
quitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(new JLabel("Balance: $" + balance));
panel.add(new JLabel("Enter Amount:"));
panel.add(inputField);
panel.add(withdrawButton);
panel.add(depositButton);
panel.add(transferButton);
panel.add(quitButton);
panel.add(new JScrollPane(transactionHistoryTextArea));
frame.add(panel);
frame.setVisible(true);
}
private void updateTransactionHistory() {
StringBuilder historyText = new StringBuilder();
for (String transaction : transactionHistory) {
historyText.append(transaction).append("\n");
}
transactionHistoryTextArea.setText(historyText.toString());
}
private void updateBalanceLabel() {
panel.remove(0); // Remove the old balance label
panel.add(new JLabel("Balance: $" + balance), 0); // Add the updated balance label
frame.revalidate();
frame.repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ATMInterface();
}
});
}
}