Untitled

mail@pastecode.io avatar
unknown
java
13 days ago
7.3 kB
0
Indexable
Never
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;

public class ATMSystem {
    static ArrayList<String> username = new ArrayList<>(Arrays.asList("vidya", "vamsi", "sai", "devi"));
    static ArrayList<String> password = new ArrayList<>(Arrays.asList("vidya1", "vamsi2", "sai3", "devi4"));
    static ArrayList<Integer> pin = new ArrayList<>(Arrays.asList(0000, 1111, 2222, 3333));
    static ArrayList<Integer> balance = new ArrayList<>(Arrays.asList(1000, 4000, 6000, 7000));
    static Scanner scanner = new Scanner(System.in);

    public static void withdrawal(int i) {
        System.out.println("Enter Your Amount To Withdraw:");
        int amount = scanner.nextInt();
        System.out.println("Enter Your PIN:");
        int pincode = scanner.nextInt();
        if (pincode != pin.get(i)) {
            System.out.println("Incorrect PIN");
            System.out.println("END");
        } else {
            if (amount > balance.get(i)) {
                System.out.println("Insufficient Balance");
            } else {
                balance.set(i, balance.get(i) - amount);
                System.out.println("Withdrawal Successful");
                System.out.println("Do You Want to Know your Available Balance? Yes/No");
                scanner.nextLine(); // Consume newline
                String q = scanner.nextLine();
                if (q.equals("No")) {
                    System.out.println("Thank You for Banking, Visit Us Again.");
                } else {
                    System.out.println("The Available Balance is " + balance.get(i));
                    System.out.println("Thank You for Banking, Visit Us Again.");
                }
            }
        }
    }

    public static void deposit(int i) {
        System.out.println("Enter Your Amount:");
        int amount = scanner.nextInt();
        System.out.println("Enter Your PIN:");
        int pincode = scanner.nextInt();
        if (pincode != pin.get(i)) {
            System.out.println("Incorrect PIN");
            System.out.println("END");
        } else {
            balance.set(i, balance.get(i) + amount);
            System.out.println("Deposit Successful");
            System.out.println("Do You Want to know your Available Balance? Yes/No");
            scanner.nextLine(); // Consume newline
            String q = scanner.nextLine();
            if (q.equals("No")) {
                System.out.println("Thank You for Banking, Visit Us Again.");
            } else {
                System.out.println("The Available Balance is " + balance.get(i));
                System.out.println("Thank You for Banking, Visit Us Again.");
            }
        }
    }

    public static void checkBalance(int i) {
        System.out.println("Enter Your PIN");
        int pincode = scanner.nextInt();
        if (pincode != pin.get(i)) {
            System.out.println("Incorrect PIN");
            System.out.println("END");
        } else {
            System.out.println("Your Available Balance: " + balance.get(i));
            System.out.println("Thank You for Banking, Visit Us Again.");
        }
    }

    public static void changePassword(int i) {
        System.out.println("Enter Your Current Password:");
        scanner.nextLine(); // Consume newline
        String cp = scanner.nextLine();
        while (true) {
            if (!cp.equals(password.get(i))) {
                System.out.println("Enter Your Correct Password:");
                cp = scanner.nextLine();
            } else {
                System.out.print("Enter Your New Password:");
                String np = scanner.nextLine();
                System.out.print("Re-enter Your New Password:");
                String np1 = scanner.nextLine();
                if (!np1.equals(np)) {
                    System.out.println("Password Doesn't Match");
                } else {
                    password.set(i, np);
                    System.out.println("Password Changed Successfully");
                    System.out.println("Thank You for Banking, Visit Us Again.");
                    break;
                }
            }
        }
    }

    public static void ATM() {
        System.out.println("Enter Your UserName:");
        String user_name = scanner.nextLine();
        if (!username.contains(user_name)) {
            System.out.println("END");
        } else {
            int i = username.indexOf(user_name);
            System.out.println("Enter Your Password");
            String passcode = scanner.nextLine();
            int flag = 0;
            if (!passcode.equals(password.get(i))) {
                System.out.println("Incorrect Password,Enter Correct password,only 2 chances left");
                passcode = scanner.nextLine();
                if (!passcode.equals(password.get(i))) {
                    System.out.println("Incorrect Password,Enter Correct password,only 1 chance left");
                    passcode = scanner.nextLine();
                    if (!passcode.equals(password.get(i))) {
                        System.out.println("Account Blocked");
                        System.out.println("END");
                    } else {
                        flag = 1;
                    }
                } else {
                    flag = 1;
                }
            } else {
                flag = 1;
            }
            if (flag == 0) {
                System.out.println("END");
            } else {
                System.out.println("Enter Your Choice:");
                System.out.println("1.Withdrawal");
                System.out.println("2.Deposit");
                System.out.println("3.Check Balance");
                System.out.println("4.Change Password");
                int choice = scanner.nextInt();
                while (true) {
                    if (choice < 1 || choice > 4) {
                        System.out.println("Enter a Valid Choice");
                        choice = scanner.nextInt();
                    } else {
                        switch (choice) {
                            case 1:
                                System.out.println("************Withdrawal**************");
                                withdrawal(i);
                                break;
                            case 2:
                                System.out.println("************Deposit************");
                                deposit(i);
                                break;
                            case 3:
                                System.out.println("************Check Balance*************");
                                checkBalance(i);
                                break;
                            case 4:
                                System.out.println("************Change Password*************");
                                changePassword(i);
                                break;
                        }
                        break;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        ATM();
    }
}

Leave a Comment