|
| 1 | +import javax.swing.*; |
| 2 | +import java.awt.*; |
| 3 | +import java.awt.event.*; |
| 4 | +import java.text.DecimalFormat; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class ATMgui { |
| 8 | + private float balance = 0; |
| 9 | + private int pin = 1019; |
| 10 | + private int pinAttempts = 0; |
| 11 | + private HashMap<String, Float> accounts = new HashMap<>(); |
| 12 | + private ArrayList<String> miniStatement = new ArrayList<>(); |
| 13 | + private JFrame frame; |
| 14 | + |
| 15 | + // Decimal format for displaying money properly |
| 16 | + private DecimalFormat df = new DecimalFormat("#,##0.00"); |
| 17 | + |
| 18 | + public ATMgui() { |
| 19 | + try { |
| 20 | + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| 21 | + } catch (Exception ignored) {} |
| 22 | + |
| 23 | + accounts.put("12345", 10000f); |
| 24 | + accounts.put("67890", 20000f); |
| 25 | + showLoginScreen(); |
| 26 | + } |
| 27 | + |
| 28 | + public void showLoginScreen() { |
| 29 | + frame = new JFrame("ATM - Login"); |
| 30 | + frame.setExtendedState(JFrame.MAXIMIZED_BOTH); |
| 31 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 32 | + frame.setLayout(new GridBagLayout()); |
| 33 | + frame.setLocationRelativeTo(null); |
| 34 | + |
| 35 | + GridBagConstraints gbc = new GridBagConstraints(); |
| 36 | + gbc.insets = new Insets(10, 10, 10, 10); |
| 37 | + |
| 38 | + JLabel label = new JLabel("Enter PIN:"); |
| 39 | + label.setFont(new Font("Arial", Font.PLAIN, 20)); |
| 40 | + JPasswordField pinField = new JPasswordField(10); |
| 41 | + pinField.setFont(new Font("Arial", Font.PLAIN, 20)); |
| 42 | + JButton loginBtn = new JButton("Login"); |
| 43 | + loginBtn.setFont(new Font("Arial", Font.BOLD, 18)); |
| 44 | + |
| 45 | + loginBtn.addActionListener(e -> { |
| 46 | + String entered = new String(pinField.getPassword()); |
| 47 | + if (entered.equals(String.valueOf(pin))) { |
| 48 | + pinAttempts = 0; |
| 49 | + frame.dispose(); |
| 50 | + showMenu(); |
| 51 | + } else { |
| 52 | + pinAttempts++; |
| 53 | + if (pinAttempts >= 3) { |
| 54 | + JOptionPane.showMessageDialog(frame, "Too many attempts. Exiting."); |
| 55 | + System.exit(0); |
| 56 | + } else { |
| 57 | + JOptionPane.showMessageDialog(frame, "Incorrect PIN. Attempts left: " + (3 - pinAttempts)); |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + gbc.gridx = 0; gbc.gridy = 0; |
| 63 | + frame.add(label, gbc); |
| 64 | + gbc.gridx = 1; |
| 65 | + frame.add(pinField, gbc); |
| 66 | + gbc.gridx = 1; gbc.gridy = 1; |
| 67 | + frame.add(loginBtn, gbc); |
| 68 | + |
| 69 | + frame.setVisible(true); |
| 70 | + } |
| 71 | + |
| 72 | + public void showMenu() { |
| 73 | + frame = new JFrame("ATM - Menu"); |
| 74 | + frame.setExtendedState(JFrame.MAXIMIZED_BOTH); |
| 75 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 76 | + frame.setLayout(new GridLayout(7, 1, 20, 20)); |
| 77 | + frame.setLocationRelativeTo(null); |
| 78 | + |
| 79 | + Font btnFont = new Font("Arial", Font.BOLD, 18); |
| 80 | + |
| 81 | + JButton balanceBtn = new JButton("Check Balance"); |
| 82 | + JButton depositBtn = new JButton("Deposit"); |
| 83 | + JButton withdrawBtn = new JButton("Withdraw"); |
| 84 | + JButton transferBtn = new JButton("Transfer Funds"); |
| 85 | + JButton miniStmtBtn = new JButton("Mini Statement"); |
| 86 | + JButton changePinBtn = new JButton("Change PIN"); |
| 87 | + JButton exitBtn = new JButton("Exit"); |
| 88 | + |
| 89 | + for (JButton btn : new JButton[]{balanceBtn, depositBtn, withdrawBtn, transferBtn, miniStmtBtn, changePinBtn, exitBtn}) { |
| 90 | + btn.setFont(btnFont); |
| 91 | + frame.add(btn); |
| 92 | + } |
| 93 | + |
| 94 | + balanceBtn.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Balance: ₹" + df.format(balance))); |
| 95 | + depositBtn.addActionListener(e -> deposit()); |
| 96 | + withdrawBtn.addActionListener(e -> withdraw()); |
| 97 | + transferBtn.addActionListener(e -> transferFunds()); |
| 98 | + miniStmtBtn.addActionListener(e -> showMiniStatement()); |
| 99 | + changePinBtn.addActionListener(e -> changePin()); |
| 100 | + exitBtn.addActionListener(e -> { |
| 101 | + JOptionPane.showMessageDialog(frame, "Thank you for using the ATM!"); |
| 102 | + System.exit(0); |
| 103 | + }); |
| 104 | + |
| 105 | + frame.setVisible(true); |
| 106 | + } |
| 107 | + |
| 108 | + public void deposit() { |
| 109 | + String input = JOptionPane.showInputDialog(frame, "Enter amount to deposit:"); |
| 110 | + try { |
| 111 | + float amount = Float.parseFloat(input); |
| 112 | + balance += amount; |
| 113 | + miniStatement.add("Deposited: ₹" + df.format(amount)); |
| 114 | + JOptionPane.showMessageDialog(frame, "Deposit Successful!"); |
| 115 | + } catch (Exception e) { |
| 116 | + JOptionPane.showMessageDialog(frame, "Invalid amount!"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public void withdraw() { |
| 121 | + String input = JOptionPane.showInputDialog(frame, "Enter amount to withdraw:"); |
| 122 | + try { |
| 123 | + float amount = Float.parseFloat(input); |
| 124 | + if (amount > balance) { |
| 125 | + JOptionPane.showMessageDialog(frame, "Insufficient Balance!"); |
| 126 | + } else { |
| 127 | + balance -= amount; |
| 128 | + miniStatement.add("Withdrawn: ₹" + df.format(amount)); |
| 129 | + JOptionPane.showMessageDialog(frame, "Withdrawal Successful!"); |
| 130 | + } |
| 131 | + } catch (Exception e) { |
| 132 | + JOptionPane.showMessageDialog(frame, "Invalid amount!"); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public void transferFunds() { |
| 137 | + String acc = JOptionPane.showInputDialog(frame, "Enter recipient account number:"); |
| 138 | + if (!accounts.containsKey(acc)) { |
| 139 | + JOptionPane.showMessageDialog(frame, "Invalid account number!"); |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + String input = JOptionPane.showInputDialog(frame, "Enter amount to transfer:"); |
| 144 | + try { |
| 145 | + float amt = Float.parseFloat(input); |
| 146 | + if (amt > balance) { |
| 147 | + JOptionPane.showMessageDialog(frame, "Insufficient Balance!"); |
| 148 | + return; |
| 149 | + } |
| 150 | + balance -= amt; |
| 151 | + accounts.put(acc, accounts.get(acc) + amt); |
| 152 | + miniStatement.add("Transferred ₹" + df.format(amt) + " to Acc: " + acc); |
| 153 | + JOptionPane.showMessageDialog(frame, "Transfer Successful!"); |
| 154 | + } catch (Exception e) { |
| 155 | + JOptionPane.showMessageDialog(frame, "Invalid amount!"); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public void showMiniStatement() { |
| 160 | + if (miniStatement.isEmpty()) { |
| 161 | + JOptionPane.showMessageDialog(frame, "No transactions yet."); |
| 162 | + return; |
| 163 | + } |
| 164 | + StringBuilder sb = new StringBuilder(); |
| 165 | + for (String t : miniStatement) { |
| 166 | + sb.append(t).append("\n"); |
| 167 | + } |
| 168 | + sb.append("\nCurrent Balance: ₹").append(df.format(balance)); |
| 169 | + JTextArea textArea = new JTextArea(sb.toString()); |
| 170 | + textArea.setEditable(false); |
| 171 | + textArea.setFont(new Font("Monospaced", Font.PLAIN, 14)); |
| 172 | + JOptionPane.showMessageDialog(frame, new JScrollPane(textArea), "Mini Statement", JOptionPane.INFORMATION_MESSAGE); |
| 173 | + } |
| 174 | + |
| 175 | + public void changePin() { |
| 176 | + String oldPin = JOptionPane.showInputDialog(frame, "Enter current PIN:"); |
| 177 | + if (!oldPin.equals(String.valueOf(pin))) { |
| 178 | + JOptionPane.showMessageDialog(frame, "Incorrect current PIN!"); |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + String newPin = JOptionPane.showInputDialog(frame, "Enter new PIN:"); |
| 183 | + String reNew = JOptionPane.showInputDialog(frame, "Re-enter new PIN:"); |
| 184 | + if (newPin.equals(reNew)) { |
| 185 | + pin = Integer.parseInt(newPin); |
| 186 | + JOptionPane.showMessageDialog(frame, "PIN changed successfully!"); |
| 187 | + } else { |
| 188 | + JOptionPane.showMessageDialog(frame, "PINs do not match!"); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + public static void main(String[] args) { |
| 193 | + SwingUtilities.invokeLater(ATMgui::new); |
| 194 | + } |
| 195 | +} |
0 commit comments