-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMgui.java
More file actions
195 lines (171 loc) · 7.31 KB
/
ATMgui.java
File metadata and controls
195 lines (171 loc) · 7.31 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.*;
public class ATMgui {
private float balance = 0;
private int pin = 1019;
private int pinAttempts = 0;
private HashMap<String, Float> accounts = new HashMap<>();
private ArrayList<String> miniStatement = new ArrayList<>();
private JFrame frame;
// Decimal format for displaying money properly
private DecimalFormat df = new DecimalFormat("#,##0.00");
public ATMgui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ignored) {}
accounts.put("12345", 10000f);
accounts.put("67890", 20000f);
showLoginScreen();
}
public void showLoginScreen() {
frame = new JFrame("ATM - Login");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.setLocationRelativeTo(null);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
JLabel label = new JLabel("Enter PIN:");
label.setFont(new Font("Arial", Font.PLAIN, 20));
JPasswordField pinField = new JPasswordField(10);
pinField.setFont(new Font("Arial", Font.PLAIN, 20));
JButton loginBtn = new JButton("Login");
loginBtn.setFont(new Font("Arial", Font.BOLD, 18));
loginBtn.addActionListener(e -> {
String entered = new String(pinField.getPassword());
if (entered.equals(String.valueOf(pin))) {
pinAttempts = 0;
frame.dispose();
showMenu();
} else {
pinAttempts++;
if (pinAttempts >= 3) {
JOptionPane.showMessageDialog(frame, "Too many attempts. Exiting.");
System.exit(0);
} else {
JOptionPane.showMessageDialog(frame, "Incorrect PIN. Attempts left: " + (3 - pinAttempts));
}
}
});
gbc.gridx = 0; gbc.gridy = 0;
frame.add(label, gbc);
gbc.gridx = 1;
frame.add(pinField, gbc);
gbc.gridx = 1; gbc.gridy = 1;
frame.add(loginBtn, gbc);
frame.setVisible(true);
}
public void showMenu() {
frame = new JFrame("ATM - Menu");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(7, 1, 20, 20));
frame.setLocationRelativeTo(null);
Font btnFont = new Font("Arial", Font.BOLD, 18);
JButton balanceBtn = new JButton("Check Balance");
JButton depositBtn = new JButton("Deposit");
JButton withdrawBtn = new JButton("Withdraw");
JButton transferBtn = new JButton("Transfer Funds");
JButton miniStmtBtn = new JButton("Mini Statement");
JButton changePinBtn = new JButton("Change PIN");
JButton exitBtn = new JButton("Exit");
for (JButton btn : new JButton[]{balanceBtn, depositBtn, withdrawBtn, transferBtn, miniStmtBtn, changePinBtn, exitBtn}) {
btn.setFont(btnFont);
frame.add(btn);
}
balanceBtn.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Balance: ₹" + df.format(balance)));
depositBtn.addActionListener(e -> deposit());
withdrawBtn.addActionListener(e -> withdraw());
transferBtn.addActionListener(e -> transferFunds());
miniStmtBtn.addActionListener(e -> showMiniStatement());
changePinBtn.addActionListener(e -> changePin());
exitBtn.addActionListener(e -> {
JOptionPane.showMessageDialog(frame, "Thank you for using the ATM!");
System.exit(0);
});
frame.setVisible(true);
}
public void deposit() {
String input = JOptionPane.showInputDialog(frame, "Enter amount to deposit:");
try {
float amount = Float.parseFloat(input);
balance += amount;
miniStatement.add("Deposited: ₹" + df.format(amount));
JOptionPane.showMessageDialog(frame, "Deposit Successful!");
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Invalid amount!");
}
}
public void withdraw() {
String input = JOptionPane.showInputDialog(frame, "Enter amount to withdraw:");
try {
float amount = Float.parseFloat(input);
if (amount > balance) {
JOptionPane.showMessageDialog(frame, "Insufficient Balance!");
} else {
balance -= amount;
miniStatement.add("Withdrawn: ₹" + df.format(amount));
JOptionPane.showMessageDialog(frame, "Withdrawal Successful!");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Invalid amount!");
}
}
public void transferFunds() {
String acc = JOptionPane.showInputDialog(frame, "Enter recipient account number:");
if (!accounts.containsKey(acc)) {
JOptionPane.showMessageDialog(frame, "Invalid account number!");
return;
}
String input = JOptionPane.showInputDialog(frame, "Enter amount to transfer:");
try {
float amt = Float.parseFloat(input);
if (amt > balance) {
JOptionPane.showMessageDialog(frame, "Insufficient Balance!");
return;
}
balance -= amt;
accounts.put(acc, accounts.get(acc) + amt);
miniStatement.add("Transferred ₹" + df.format(amt) + " to Acc: " + acc);
JOptionPane.showMessageDialog(frame, "Transfer Successful!");
} catch (Exception e) {
JOptionPane.showMessageDialog(frame, "Invalid amount!");
}
}
public void showMiniStatement() {
if (miniStatement.isEmpty()) {
JOptionPane.showMessageDialog(frame, "No transactions yet.");
return;
}
StringBuilder sb = new StringBuilder();
for (String t : miniStatement) {
sb.append(t).append("\n");
}
sb.append("\nCurrent Balance: ₹").append(df.format(balance));
JTextArea textArea = new JTextArea(sb.toString());
textArea.setEditable(false);
textArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
JOptionPane.showMessageDialog(frame, new JScrollPane(textArea), "Mini Statement", JOptionPane.INFORMATION_MESSAGE);
}
public void changePin() {
String oldPin = JOptionPane.showInputDialog(frame, "Enter current PIN:");
if (!oldPin.equals(String.valueOf(pin))) {
JOptionPane.showMessageDialog(frame, "Incorrect current PIN!");
return;
}
String newPin = JOptionPane.showInputDialog(frame, "Enter new PIN:");
String reNew = JOptionPane.showInputDialog(frame, "Re-enter new PIN:");
if (newPin.equals(reNew)) {
pin = Integer.parseInt(newPin);
JOptionPane.showMessageDialog(frame, "PIN changed successfully!");
} else {
JOptionPane.showMessageDialog(frame, "PINs do not match!");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ATMgui::new);
}
}