code
code
*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
pins.put(101, 1234);
pins.put(102, 2345);
pins.put(103, 3456);
pins.put(104, 4567);
// GUI setup
JFrame frame = new JFrame("ATM Simulation");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel topPanel = new JPanel(new GridLayout(5, 1));
balanceLabel = new JLabel("Balance: Rs. " + accounts.get(userId),
SwingConstants.CENTER);
JTextField amountField = new JTextField();
JButton depositButton = new JButton("Deposit");
JButton withdrawButton = new JButton("Withdraw");
JButton clearButton = new JButton("Clear");
loadLogFromFile();
transactionLog.append(timestamp()).append("Logged in as Customer ID:
").append(userId)
.append("\nInitial Balance: Rs. ").append(accounts.get(userId))
.append("\n---------------------------\n");
miniStatement.setText(transactionLog.toString());
saveLogToFile();
frame.add(topPanel, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}
try {
amount = Double.parseDouble(input);
if (amount <= 0) {
JOptionPane.showMessageDialog(null, "Enter a positive amount!");
return;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid amount! Please enter
a number.");
return;
}
if (isDeposit) {
balance += amount;
action = "Deposited Rs. " + amount;
} else if (amount <= balance) {
balance -= amount;
action = "Withdrew Rs. " + amount;
} else {
JOptionPane.showMessageDialog(null, "Insufficient Balance!");
return;
}
accounts.put(userId, balance);
balanceLabel.setText("Balance: Rs. " + balance);
amountField.setText("");
transactionLog.append(timestamp()).append(action)
.append("\nCurrent Balance: Rs. ").append(balance)
.append("\n---------------------------\n");
miniStatement.setText(transactionLog.toString());
saveLogToFile();
}
private static void saveLogToFile() {
try {
String fileName = "statement_" + userId + ".txt";
FileWriter writer = new FileWriter(fileName);
writer.write(transactionLog.toString());
writer.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error saving statement to
file.");
}
}