-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniATMSimulator.java
More file actions
149 lines (126 loc) · 5.84 KB
/
MiniATMSimulator.java
File metadata and controls
149 lines (126 loc) · 5.84 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MiniATMSimulator {
private JFrame frame;
private JLabel screen, balanceLabel;
private JTextField cardInput;
private JPasswordField pinInput;
private JButton enterButton, toggleBalanceButton;
private boolean isBalanceVisible = false;
private int balance = 5000;
private String transactionPin = "5678";
public MiniATMSimulator() {
frame = new JFrame("Mini ATM Simulator Project");
frame.setSize(600, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.getContentPane().setBackground(Color.DARK_GRAY);
JLabel title = new JLabel("Mini ATM Simulator Project", SwingConstants.CENTER);
title.setBounds(150, 10, 300, 30);
title.setForeground(Color.WHITE);
title.setFont(new Font("Arial", Font.BOLD, 18));
frame.add(title);
screen = new JLabel("Enter Card Number & PIN", SwingConstants.CENTER);
screen.setBounds(150, 50, 300, 50);
screen.setForeground(Color.WHITE);
screen.setBackground(Color.BLACK);
screen.setOpaque(true);
frame.add(screen);
cardInput = new JTextField();
cardInput.setBounds(200, 120, 200, 30);
frame.add(cardInput);
pinInput = new JPasswordField();
pinInput.setBounds(200, 170, 200, 30);
frame.add(pinInput);
enterButton = new JButton("Enter");
enterButton.setBounds(250, 220, 100, 30);
frame.add(enterButton);
enterButton.addActionListener(e -> authenticateUser());
frame.setVisible(true);
}
private void authenticateUser() {
String cardNumber = cardInput.getText();
String pin = new String(pinInput.getPassword());
if (cardNumber.equals("12345678") && pin.equals("1234")) {
showTransactionMenu();
} else {
screen.setText("Invalid Credentials! Try Again");
}
}
private void showTransactionMenu() {
frame.getContentPane().removeAll();
screen.setText("Select Transaction");
screen.setBounds(150, 30, 300, 50);
frame.add(screen);
balanceLabel = new JLabel("Balance: ****");
balanceLabel.setBounds(200, 90, 200, 30);
balanceLabel.setForeground(Color.WHITE);
frame.add(balanceLabel);
toggleBalanceButton = new JButton("👁");
toggleBalanceButton.setBounds(400, 90, 50, 30);
toggleBalanceButton.addActionListener(e -> toggleBalanceVisibility());
frame.add(toggleBalanceButton);
JButton checkBalanceButton = new JButton("Check Balance");
checkBalanceButton.setBounds(200, 140, 200, 30);
checkBalanceButton.addActionListener(e -> showBalanceInput());
frame.add(checkBalanceButton);
JButton withdrawButton = new JButton("Withdraw");
withdrawButton.setBounds(200, 190, 200, 30);
withdrawButton.addActionListener(e -> showWithdrawInput());
frame.add(withdrawButton);
JButton depositButton = new JButton("Deposit");
depositButton.setBounds(200, 240, 200, 30);
depositButton.addActionListener(e -> showDepositInput());
frame.add(depositButton);
frame.repaint();
frame.revalidate();
}
private void toggleBalanceVisibility() {
isBalanceVisible = !isBalanceVisible;
balanceLabel.setText(isBalanceVisible ? "Balance: " + balance : "Balance: ****");
}
private void showBalanceInput() {
String pin = JOptionPane.showInputDialog(frame, "Enter Transaction PIN:");
if (pin != null && pin.equals(transactionPin)) {
JOptionPane.showMessageDialog(frame, "Balance: " + balance, "Balance Check", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(frame, "Invalid PIN!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void showWithdrawInput() {
String pin = JOptionPane.showInputDialog(frame, "Enter Transaction PIN:");
if (pin != null && pin.equals(transactionPin)) {
String amount = JOptionPane.showInputDialog(frame, "Enter amount to withdraw:");
if (amount != null) {
int withdrawAmount = Integer.parseInt(amount);
if (withdrawAmount > 0 && withdrawAmount <= balance) {
balance -= withdrawAmount;
JOptionPane.showMessageDialog(frame, "Withdrawal Successful!", "Success", JOptionPane.INFORMATION_MESSAGE);
toggleBalanceVisibility();
} else {
JOptionPane.showMessageDialog(frame, "Invalid Amount!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
} else {
JOptionPane.showMessageDialog(frame, "Invalid PIN!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void showDepositInput() {
String amount = JOptionPane.showInputDialog(frame, "Enter amount to deposit:");
if (amount != null) {
int depositAmount = Integer.parseInt(amount);
if (depositAmount > 0) {
balance += depositAmount;
JOptionPane.showMessageDialog(frame, "Deposit Successful!", "Success", JOptionPane.INFORMATION_MESSAGE);
toggleBalanceVisibility();
} else {
JOptionPane.showMessageDialog(frame, "Invalid Amount!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
new MiniATMSimulator();
}
}