Member Avatar for Member #365538

Hi Guys, Please i need someone who could hlp me write a simple ATM program for accepting PIN, Checking Balance and Withdrawing..

Dani AI

Generated

A compact, practical path: implement a small console program first, then layer on GUI, persistence, or networking only if required. That keeps scope small (the point made) while still exercising the three features asked for: accept PIN, show balance, perform withdrawals. Below is a minimal, complete example you can compile and run as a learning exercise — demo-only code (do not use in production).

import java.util.*;

public class SimpleATM {
    static class Account {
        final String id;
        final int pin;    // demo only — do NOT store plaintext pins in real systems
        double balance;
        Account(String id, int pin, double balance) { this.id=id; this.pin=pin; this.balance=balance; }
        boolean checkPin(int p) { return pin==p; }
        boolean withdraw(double amt) { if (amt<=0 || amt>balance) return false; balance -= amt; return true; }
    }

    public static void main(String[] args) {
        Map<String,Account> db = new HashMap<>();
        db.put("1001", new Account("1001", 1234, 500.00));
        db.put("1002", new Account("1002", 4321, 1000.00));

        Scanner sc = new Scanner(System.in);
        System.out.print("Account number: ");
        Account a = db.get(sc.nextLine().trim());
        if (a == null) { System.out.println("Unknown account"); sc.close(); return; }

        int tries = 0;
        while (tries < 3) {
            System.out.print("Enter PIN: ");
            int pin = Integer.parseInt(sc.nextLine().trim());
            if (a.checkPin(pin)) break;
            tries++;
            System.out.println("Invalid PIN (" + (3-tries) + " left)");
            if (tries == 3) { System.out.println("Card blocked"); sc.close(); return; }
        }

        boolean done = false;
        while (!done) {
            System.out.println("\n1) Balance  2) Withdraw  3) Exit");
            switch (sc.nextLine().trim()) {
                case "1": System.out.printf("Balance: $%.2f%n", a.balance); break;
                case "2":
                    System.out.print("Amount to withdraw: ");
                    double amt = Double.parseDouble(sc.nextLine().trim());
                    if (a.withdraw(amt)) System.out.printf("Dispensed. New balance: $%.2f%n", a.balance);
                    else System.out.println("Insufficient funds or invalid amount.");
                    break;
                case "3": done = true; break;
                default: System.out.println("Invalid option.");
            }
        }
        sc.close();
    }
}

Notes and next steps: this code is a learning scaffold only. For real systems: never store plaintext PINs (use salted hashing/HSM), use secure channels (TLS), persist balances in a transactional database, and handle concurrency/rollback. ’s Swing attempt shows common beginner issues (typos like Bufferedeader vs BufferedReader, stray + in the JTextArea initializer, System.out.print calls at class scope, and mismatched braces); fixing those and using setVisible(true) will help a GUI version. Finally, as implied, showing incremental attempts makes troubleshooting much easier; ’s comments about server/DB are appropriate when the project grows beyond a single-process demo.

Recommended Answers

All 9 Replies

Hi Guys, Please i need someone who could hlp me write a simple ATM program for accepting PIN, Checking Balance and Withdrawing..

What's your progress? (aka post your code)

Member Avatar for Member #365538

I am new in Java.... Please help

You said that you wantd help, not someone to do your program from scratch. There are a lot of solutions fro this problem, so don't expect someone just to hand you the complete code. You need to show us how you want to implement it in order to point you to the right direction.
Because such program could consist of only one file, or thousands of different classes depending on the level of complexity you want to add.

Several steps: one way:

Need to identify the card first
Need scanning device and analyze the acct number is right
then interface to type the pin

--
there should be a server program that can handle request from the ATMs. ATMs will create a socket connection to the server and communicate messages
--
First connect to server [can use Java socket classes for the purpose]
scan card and send the text to the server
Server sends response OK/Failed
Collect PIN then send to the Server using the socket
server will respond with fail/success.
[A local ATM database and asociated server program can also does the verification, may be insecure]

May use encryption for data communication
---
can keep a DB in the server side to keep information.
Each sent records can be matched with the db.

For the purpose, you need your logic to verify, DB connection logic is required and then query and verify is required. Decryption may be required if you use encryption

balances can be kept in db then you need to deduct the withdrawn amount

you should also, use the transaction management concept

So you see lots of issues to consider.

Several steps: one way:

Need to identify the card first
Need scanning device and analyze the acct number is right
then interface to type the pin

--
there should be a server program that can handle request from the ATMs. ATMs will create a socket connection to the server and communicate messages
--
First connect to server [can use Java socket classes for the purpose]
scan card and send the text to the server
Server sends response OK/Failed
Collect PIN then send to the Server using the socket
server will respond with fail/success.
[A local ATM database and asociated server program can also does the verification, may be insecure]

May use encryption for data communication
---
can keep a DB in the server side to keep information.
Each sent records can be matched with the db.

For the purpose, you need your logic to verify, DB connection logic is required and then query and verify is required. Decryption may be required if you use encryption

balances can be kept in db then you need to deduct the withdrawn amount

you should also, use the transaction management concept

So you see lots of issues to consider.

smslive2, asked for a simple program. I doubt it that if this is a school assignment they would want something that complex. Of course I don't blame you, since smslive2 didn't specify what was his problem you can give any solution you want

Maybe this class diagram will help you solve ur problem

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;


public class ATM extends JFrame implements ActionListener
{   
    Bufferedeader br=new BufferedReader(new InputStreamReader(System.in));
    String b;
    Container c=getContentPane();   
    JPanel p1,p2;
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15;
    JTextArea t1;

    public ATM()
    {   
        super("ATM");
        t1=new JTextArea("                                  "+);
        t1.setMargin(new Insets(50,50,50,50));
        t1.setLineWrap(true);
        t1.setEditable(true);
        }                                
        p1=new JPanel();
        p2=new JPanel();

        b1=new JButton("1");
        b2=new JButton("2");
        b3=new JButton("3");    
        b4=new JButton("4");    
        b5=new JButton("5");
        b6=new JButton("6");
        b7=new JButton("7"); 
        b8=new JButton("8");    
        b9=new JButton("9");
        b10=new JButton("0");
        b11=new JButton();
        b12=new JButton("Deposit");
        b12.setBackground(Color.yellow);
        b13=new JButton("Withraw");
        b13.setBackground(Color.green);
        b14=new JButton("Cancel");
        b14.setBackground(Color.red);
        b15=new JButton("Enter");

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        b10.addActionListener(this);
        b12.addActionListener(this);
        b13.addActionListener(this);
        b14.addActionListener(this);
        b15.addActionListener(this);

        p1.setLayout(new FlowLayout());
        p1.add(t1);

        p2.setLayout(new GridLayout(3,4));
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        p2.add(b4);
        p2.add(b5);
        p2.add(b6);
        p2.add(b7);
        p2.add(b8);
        p2.add(b9);
        p2.add(b10);
        p2.add(b11);
        p2.add(b12);
        p2.add(b13);
        p2.add(b14);
        p2.add(b15);


  setLayout(new GridLayout(2,1));
        add(p1);
        add(p2);
        setSize(400,200);
        show();
}
public void actionPerformed(ActionEvent e)
{
        String give;
        give=t1.getText();
        if(e.getSource()==b1)
        {
        t1.setText(give+"1");
        }
        if(e.getSource()==b2)
        {
        t1.setText(give+"2");
        }
        if(e.getSource()==b3)
        {
        t1.setText(give+"3");
        }
        if(e.getSource()==b4)
        {
        t1.setText(give+"4");
        }
        if(e.getSource()==b5)
        {
        t1.setText(give+"5");
        }
        if(e.getSource()==b6)
        {
        t1.setText(give+"6");
        }
        if(e.getSource()==b7)
        {
        t1.setText(give+"7");
        }
        if(e.getSource()==b8)
        {
        t1.setText(give+"8");
        }
        if(e.getSource()==b9)
        {
        t1.setText(give+"9");
        }
        if(e.getSource()==b10)
        {
        t1.setText(give+"0");
        }
        if(e.getSource()==b11)
        {
        t1.setText(give+"0");
        }
        if(e.getSource()==b14)
        {
        JOptionPane.showMessageDialog(null,"Transaction Cancelled");
        }
        if(e.getSource()==b15)
        {
        t1.setText(give+"");
        }
}
    public static void main (String []args)throws Exception
        {
        new ATM();
        }
    }

i want to put this up

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;


public class ATM extends JFrame implements ActionListener
{   
    Bufferedeader br=new BufferedReader(new InputStreamReader(System.in));
    String b;
    System.out.print("Account Number:");
    System.out.print("Account Pin:");
    System.out.print("Account Balance:");
    Container c=getContentPane();   
    JPanel p1,p2;
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15;
    JTextArea t1;

    public ATM()
    {   
        super("ATM");
        t1=new JTextArea("                                  "+);
        t1.setMargin(new Insets(50,50,50,50));
        t1.setLineWrap(true);
        t1.setEditable(true);
        }                                
        p1=new JPanel();
        p2=new JPanel();

        b1=new JButton("1");
        b2=new JButton("2");
        b3=new JButton("3");    
        b4=new JButton("4");    
        b5=new JButton("5");
        b6=new JButton("6");
        b7=new JButton("7"); 
        b8=new JButton("8");    
        b9=new JButton("9");
        b10=new JButton("0");
        b11=new JButton();
        b12=new JButton("Deposit");
        b12.setBackground(Color.yellow);
        b13=new JButton("Withraw");
        b13.setBackground(Color.green);
        b14=new JButton("Cancel");
        b14.setBackground(Color.red);
        b15=new JButton("Enter");

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        b10.addActionListener(this);
        b12.addActionListener(this);
        b13.addActionListener(this);
        b14.addActionListener(this);
        b15.addActionListener(this);

        p1.setLayout(new FlowLayout());
        p1.add(t1);

        p2.setLayout(new GridLayout(3,4));
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        p2.add(b4);
        p2.add(b5);
        p2.add(b6);
        p2.add(b7);
        p2.add(b8);
        p2.add(b9);
        p2.add(b10);
        p2.add(b11);
        p2.add(b12);
        p2.add(b13);
        p2.add(b14);
        p2.add(b15);


  setLayout(new GridLayout(2,1));
        add(p1);
        add(p2);
        setSize(400,200);
        show();
}
public void actionPerformed(ActionEvent e)
{
        String give;
        give=t1.getText();
        if(e.getSource()==b1)
        {
        t1.setText(give+"1");
        }
        if(e.getSource()==b2)
        {
        t1.setText(give+"2");
        }
        if(e.getSource()==b3)
        {
        t1.setText(give+"3");
        }
        if(e.getSource()==b4)
        {
        t1.setText(give+"4");
        }
        if(e.getSource()==b5)
        {
        t1.setText(give+"5");
        }
        if(e.getSource()==b6)
        {
        t1.setText(give+"6");
        }
        if(e.getSource()==b7)
        {
        t1.setText(give+"7");
        }
        if(e.getSource()==b8)
        {
        t1.setText(give+"8");
        }
        if(e.getSource()==b9)
        {
        t1.setText(give+"9");
        }
        if(e.getSource()==b10)
        {
        t1.setText(give+"0");
        }
        if(e.getSource()==b11)
        {
        t1.setText(give+"0");
        }
        if(e.getSource()==b14)
        {
        JOptionPane.showMessageDialog(null,"Transaction Cancelled");
        }
        if(e.getSource()==b15)
        {
        t1.setText(give+"");
        }
}
    public static void main (String []args)throws Exception
        {
        new ATM();
        }
    }

Please create a new thread for your own... Reviving almost 2 years old thread is not a good thing to do... Also, use the 'code' tag given by the forum to enhance your code display.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.