0% found this document useful (0 votes)
2 views

code

The document contains a Java program that simulates an ATM GUI, allowing users to log in with a customer ID and PIN, deposit and withdraw money, and view a mini statement of transactions. It manages user accounts and transaction logs using HashMaps and file I/O for saving and loading statements. The program includes error handling for invalid inputs and insufficient funds during transactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

code

The document contains a Java program that simulates an ATM GUI, allowing users to log in with a customer ID and PIN, deposit and withdraw money, and view a mini statement of transactions. It manages user accounts and transaction logs using HashMaps and file I/O for saving and loading statements. The program includes error handling for invalid inputs and insufficient funds during transactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

import javax.swing.

*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ATMSimulationGUI {


private static HashMap<Integer, Double> accounts = new HashMap<>();
private static HashMap<Integer, Integer> pins = new HashMap<>();
private static int userId;
private static JLabel balanceLabel;
private static JTextArea miniStatement;
private static StringBuilder transactionLog = new StringBuilder();

public static void main(String[] args) {


// Setup accounts and PINs
accounts.put(101, 5000.0);
accounts.put(102, 7000.0);
accounts.put(103, 3000.0);
accounts.put(104, 10000.0);

pins.put(101, 1234);
pins.put(102, 2345);
pins.put(103, 3456);
pins.put(104, 4567);

// Prompt for login


try {
userId = Integer.parseInt(JOptionPane.showInputDialog("Enter
Customer ID (101-104):"));
if (!accounts.containsKey(userId)) {
JOptionPane.showMessageDialog(null, "Invalid Customer ID!");
return;
}

int enteredPin = Integer.parseInt(JOptionPane.showInputDialog("Enter


4-digit PIN:"));
if (pins.get(userId) != enteredPin) {
JOptionPane.showMessageDialog(null, "Incorrect PIN!");
return;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Invalid Input!");
return;
}

// 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");

miniStatement = new JTextArea(8, 30);


miniStatement.setEditable(false);
JScrollPane scrollPane = new JScrollPane(miniStatement);

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();

depositButton.addActionListener(e -> updateBalance(amountField,


true));
withdrawButton.addActionListener(e -> updateBalance(amountField,
false));
clearButton.addActionListener(e -> amountField.setText(""));
amountField.addActionListener(e -> updateBalance(amountField, true));
topPanel.add(balanceLabel);
topPanel.add(amountField);
topPanel.add(depositButton);
topPanel.add(withdrawButton);
topPanel.add(clearButton);

frame.add(topPanel, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setVisible(true);
}

private static void updateBalance(JTextField amountField, boolean


isDeposit) {
String input = amountField.getText();
double amount;

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;
}

double balance = accounts.get(userId);


String action;

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.");
}
}

private static void loadLogFromFile() {


try {
String fileName = "statement_" + userId + ".txt";
File file = new File(fileName);
if (file.exists()) {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
transactionLog.append(scanner.nextLine()).append("\n");
}
scanner.close();
}
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error reading previous
statement file.");
}
}

private static String timestamp() {


return "[" +
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd
HH:mm:ss")) + "] ";
}
}

You might also like