Untitled

mail@pastecode.io avatar
unknown
java
23 days ago
3.3 kB
6
Indexable
Never
package com.mycompany.atm;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ATM {

    String[][] accounts = {{"1234567887654321", "123456", "15000", "UNLOCKED"},
    {"8765432112345678", "654321", "10000", "UNLOCKED"}
    };
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public void authenticateAccount(String idNum, String pin) {
        int row = 0;
        String checkPin = "";
        int retry = 0;

        try {
            System.out.print("Enter your PIN >>: ");
            pin = br.readLine();

            for (int i = 0; i < accounts.length; i++) {
                for (int j = 0; j < accounts[0].length; j++) {
                    if (idNum.equals(accounts[i][j])) {
                        row = i;
                        checkPin = accounts[row][1];
                    }
                }
            }
            if (!pin.equals(checkPin)) {
                while (true) {
                    System.out.println("Wrong PIN try again.");
                    System.out.print(">>: ");
                    pin = br.readLine();

                    if (pin.equals(checkPin)) {
                        break;
                    } else {
                        retry++;
                        if (retry == 2) {
                            lockAccount(row);
                            break;
                        }
                    }
                }
            }

        } catch (Exception e) {
        }

    }

    public void lockAccount(int row) {
        accounts[row][3] = "LOCKED";
        System.out.println("Your accounts has been locked");
    }

    public String checkBalance(String idNum) {
        String balance = "";
        int row = 0;

        for (int i = 0; i < accounts.length; i++) {
            for (int j = 0; j < accounts[0].length; j++) {
                if (accounts[i][j].equals(idNum)) {
                    row = i;
                }
            }
        }
        balance = accounts[row][2];
        return balance;
    }

    public static void main(String[] args) {
        String idNum = "";
        String pin = "";
        boolean checker = true;
        int choice = 0;
        String balance = "";

        ATM ans = new ATM();
        System.out.println("=================================================");
        System.out.println("                  VERIFICATION");
        System.out.println("=================================================");
        try {
            while (checker) {
                System.out.print("Enter your ID number >>: ");
                idNum = ans.br.readLine();

                for (int i = 0; i < ans.accounts.length; i++) {
                    if (idNum.equals(ans.accounts[i][0])) {
                        if (ans.accounts[i][3].equals("UNLOCKED")) {
                            ans.authenticateAccount(idNum, pin);
                            
                            
                        } else {
                            System.out.println("Your account is locked.");
                        }
                    }
                }
            }
        } catch (Exception e) {
        }

    }
}
Leave a Comment