Home > Blockchain >  Java What is wrong with my transaction methods
Java What is wrong with my transaction methods

Time:12-28

I'm fairly new to java especially in arrays and the code below is a simple atm program that has two users available(since I'm just testing it).

For my question I want to ask what went wrong in my "Previous Transaction" code where in I expected it to show the previous amount used for doing the withdraw, deposit and fund transfer methods but instead it just shows nothing.

This is my main class

import java.util.Scanner;

public class MyATM_Test {
    public static void main(String[] args) {
        //
        //  User's Name and PIN is predefined for example
        //  Users:
        //         User One, Credentials
        //  name: User1
        //  pin : 6644
        //         User Two, Credentials
        //  name: User2
        //  pin : 7745
        //
        MyATM_Accounts User1 = new MyATM_Accounts("User1","6644", 109321, 500);
        MyATM_Accounts User2 = new MyATM_Accounts("User2","7745", 109123, 500);
        try {
            Thread.sleep(1000);
            System.out.println("__________________________________________");
            System.out.println("|    This Program is still in Testing.   |");
            System.out.println("|    Account Creation is not available.  |");
            System.out.println("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        } try {
            Thread.sleep(1000);
        System.out.println(        "__________________________________________");
        System.out.println(        "|    Available Accounts :                |" "\n"  
                                   "|            User One, Credentials       |" "\n"  
                                   "|    name: User1                         |" "\n"  
                                   "|    pin : 6644                          |" "\n"  
                                   "|            User Two, Credentials       |" "\n"  
                                   "|    name: User2                         |" "\n"  
                                   "|    pin : 7745                          |" "\n"  
                                   "¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }
        //Pause for Reading
        System.out.print("Press Enter key to continue...");
        Scanner ent = new Scanner(System.in);
        ent.nextLine();

         //Begin Main menu Log in and Exit code
        Scanner user_input1 = new Scanner(System.in);
        int mistakes = 0;//Counter for Log in and Exit Menu.
        //Loop for Wrong inputs.
        while (mistakes<3) {
            int uChoice;
            System.out.println(        "\n\n\n\n\n__________________________________________");
            System.out.println(        "|            Welcome to MyATM®           |");
            System.out.println(        "|         [1] Log in    [2] Exit         |");
            System.out.println(        "¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
            //Loops if User enters a letter, word or numbers greater than 2.
            do {
                System.out.println(    "|    Please Enter only the number given  |");
                System.out.print(      "|    Choice : ");
                while (!user_input1.hasNextInt()) {
                    System.out.println("==========================================");
                    System.out.println("|           That's not a number!         |");
                    System.out.println("==========================================");
                    System.out.println("|    Please Enter only the number given  |");
                    System.out.print(  "|    Choice : ");
                    user_input1.next();
                }
                uChoice = user_input1.nextInt();
            } while(uChoice>2);
            if (uChoice==1) {
                System.out.println("\n\n" 
                                   "_____________________________");
                System.out.println("|       Log in to MyATM®    |");
                System.out.println("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
                 //Begin Logging in Code
                Scanner user_input2 = new Scanner(System.in);
                //Variables for User Input checking
                int attempts = 0;//Attempt counter will be used in logging in
                String currentUsr = "User";//Shows what user is Logged in
                long currentUsrID = 0;//Shows what is the ID of Logged-in User
                String tmpPin;//User Input
                while (attempts < 3) {
                    System.out.println("_____________________________");
                    System.out.println("|   Please Enter your PIN   |");
                    System.out.print(  "|   PIN : ");
                    tmpPin = user_input2.nextLine();
                    if (tmpPin.equals(User1.getPin())) {
                        System.out.println("_________________________________________");
                        System.out.println("|       Welcome to MyATM® (User1)       |");
                        System.out.println("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
                        currentUsr = currentUsr.replace("User", "User1");
                        currentUsrID = User1.getAccNum();
                        break;
                    } else if (tmpPin.equals(User2.getPin())) {
                        System.out.println("_________________________________________");
                        System.out.println("|       Welcome to MyATM® (User2)       |");
                        System.out.println("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
                        currentUsr = currentUsr.replace("User", "User2");
                        currentUsrID = User2.getAccNum();
                        break;
                    } else {
                        System.out.println("________________________________________");
                        System.out.println("|||||||||||||User not Found!||||||||||||");
                        System.out.println("|     This ATM is still in testing     |");
                        System.out.println("| Please input only the given accounts |");
                        System.out.println("|            Thank you.                |");
                        System.out.println("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
                        attempts  ;
                        System.out.println("________________________________________");
                        System.out.println("| Remaining Log in Chances : "   (3 - attempts)   "        |");
                        System.out.println("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
                    }
                }
                //Prompts when all chances are used
                if (attempts == 3) {
                    System.out.println("________________________________________");
                    System.out.println("|  You have used up all your chances.  |"  
                                     "\n|         Please Try again Later       |");
                    System.out.println("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
                    System.out.println("Exiting...");
                    System.exit(0);
                }
                 //End of Logging in code
                 //Begin Main Menu code
                int ucMenu = 0;
                Scanner user_input3 = new Scanner(System.in);
                if (currentUsr.equals("User1") || currentUsr.equals("User2")) {
                    //Valid User
                    //Delaying the Display
                    //For better performance
                    try {
                        Thread.sleep(2000);
                    System.out.println("__________________________________________");
                    System.out.println("|    Currently Signed in as (" currentUsr ")      |");
                    System.out.println("|    MyATM User ID :    " currentUsrID "           |");
                    System.out.println("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
                    } catch (InterruptedException ie) {
                        Thread.currentThread().interrupt();
                    }
                    while (true) { //Infinite Loop
                        //Display Menu
                        //Delaying the Display
                        //For better performance
                        try {
                            Thread.sleep(2500);
                            System.out.println("===========================================");
                            System.out.println("| What transaction would you like to do?  |");
                            System.out.println("===========================================");
                            System.out.println("| [1] Balance Inquiry   [2] Withdraw      |");
                            System.out.println("| [3] Deposit           [4] Fund Transfer |");
                            System.out.println("| [5] Change PIN        [0] Log out       |");
                            System.out.println("===========================================");
                            System.out.println("| MyATM User ID : " currentUsrID "                  |");
                            System.out.println("===========================================");
                        } catch (InterruptedException ie) {
                            Thread.currentThread().interrupt();
                        }
                        //Loops if User enters a letter, word or numbers greater than 5.
                            do {
                                System.out.println(    "|    Please Enter only the number given  |");
                                System.out.print(      "|    Choice : ");
                                while (!user_input3.hasNextInt()) {
                                    System.out.println("==========================================");
                                    System.out.println("|           That's not a number!         |");
                                    System.out.println("==========================================");
                                    System.out.println("|    Please Enter only the number given  |");
                                    System.out.print(  "|    Choice : ");
                                    user_input3.next();
                                }
                                ucMenu = user_input3.nextInt();
                            }while(ucMenu>5);
                        int[] trans = new int[10];
                        double amount  = 0 ;
                        double balance = 0;
                        long MyATM_id = 0;
                        Scanner user_trans = new Scanner(System.in);
                        //Balance Inquiry
                        if (ucMenu == 1) {
                            System.out.println("================================");
                            System.out.println("|        Current Balance       |");
                                System.out.println("================================");
                            if (currentUsr.equals("User1")) {
                                balance = User1.getBalance();
//                                User1.showTransactions();
//                                System.out.println("| Account: " currentUsr " MyATM User ID : " currentUsrID);
                            }else if (currentUsr.equals("User2")) {
                                balance = User2.getBalance();
//                                User2.showTransactions();
//                                System.out.println("| Account: " currentUsr " MyATM User ID : " currentUsrID);
                            }
                            //System.out.println((int)(balance));
                            showTransactions(trans, (int) (balance));
                            System.out.println("================================================");
                            System.out.println("|    Balance for " currentUsr " is ₱" balance "    |");
                            System.out.println("================================================");


                        }
                        //Withdraw
                        else if (ucMenu == 2) {
                            System.out.println("================================");
                            System.out.println("|           Withdraw           |");
                            System.out.println("================================");
                            System.out.println("|     Amount to be Withdrawn   |");
                            System.out.print(  "|     Amount : ");
                            amount = user_trans.nextInt();
                            System.out.println("================================");
                            System.out.println("|            Details           |");
                            System.out.println("================================");
                            if (currentUsr.equals("User1")) {
                                User1.withdraw(amount);
                                makeTransactions(trans, (int) (amount));
                            }else if (currentUsr.equals("User2")) {
                                User2.withdraw(amount);
                                makeTransactions(trans, (int) (amount));
                            }
                        }
                        //Deposit
                        else if (ucMenu == 3) {
                            System.out.println("================================");
                            System.out.println("|           Deposit            |");
                            System.out.println("================================");
                            System.out.println("|     Amount to be Deposited   |");
                            System.out.print(  "|     Amount : ");
                            amount = user_trans.nextInt();
                            System.out.println("================================");
                            System.out.println("|            Details           |");
                            System.out.println("================================");
                            if (currentUsr.equals("User1")) {
                                User1.deposit(amount);
                                makeTransactions(trans, (int) (amount));
                            }else if (currentUsr.equals("User2")) {
                                User2.deposit(amount);
                                makeTransactions(trans, (int) (amount));
                            }
                        }
                        //Fund Transfer
                        else if (ucMenu == 4) {
                            System.out.println("===================================");
                            System.out.println("|          Fund Transfer          |");
                            System.out.println("===================================");
                            System.out.println("|    Please Enter MyATM User ID   |");
                            System.out.print("|    MyATM User ID : ");
                            MyATM_id = user_trans.nextLong();
                            if (currentUsr.equals("User1")) {
                                System.out.println("================================");
                                System.out.println("|            Details           |");
                                System.out.println("================================");
                                if (MyATM_id == User1.getAccNum()) {
                                    System.out.println("|          User Found!            |"  
                                                     "\n|      Account : " User1.getName() "          |");
                                    System.out.println("|    Amount to be Transferred     |");
                                    System.out.println("|(Enter 0 to cancel Fund Transfer)|");
                                    System.out.print(  "|   Amount : ");
                                    amount = user_trans.nextInt();
                                    User1.transfer(amount, User1);
                                    makeTransactions(trans, (int) (amount));
                                    System.out.println("===================================");
                                } else if (MyATM_id == User2.getAccNum()) {
                                    System.out.println("|          User Found!            |"  
                                            "\n|      Account : " User2.getName() "           |");
                                    System.out.println("|    Amount to be Transferred     |");
                                    System.out.println("|(Enter 0 to cancel Fund Transfer)|");
                                    System.out.print(  "|   Amount : ");
                                    amount = user_trans.nextInt();
                                    User1.transfer(amount, User2);
                                    makeTransactions(trans, (int) (amount));
                                    System.out.println("===================================");
                                } else {
                                    System.out.println(  "| User not Found!                        |"  
                                                       "\n| Possible Cause:                        |"  
                                                       "\n| -User does not exist in MyATM accounts |"  
                                                       "\n| -Incorrect User ID                     |"  
                                                       "\n==========================================");
                                }
                            }else if (currentUsr.equals("User2")) {
                                System.out.println("================================");
                                System.out.println("|            Details           |");
                                System.out.println("================================");
                                if (MyATM_id == User1.getAccNum()) {
                                    System.out.println("|          User Found!            |"  
                                            "\n|      Account : " User1.getName() "           |");
                                    System.out.println("|    Amount to be Transferred     |");
                                    System.out.println("|(Enter 0 to cancel Fund Transfer)|");
                                    System.out.print(  "|   Amount : ");
                                    amount = user_trans.nextInt();
                                    User2.transfer(amount, User1);
                                    makeTransactions(trans, (int) (amount));
                                    System.out.println("===================================");
                                } else if (MyATM_id == User2.getAccNum()) {
                                    System.out.println("|          User Found!            |"  
                                            "\n|      Account : " User2.getName() "           |");
                                    System.out.println("|    Amount to be Transferred     |");
                                    System.out.println("|(Enter 0 to cancel Fund Transfer)|");
                                    System.out.print(  "|   Amount : ");
                                    amount = user_trans.nextInt();
                                    User2.transfer(amount, User2);
                                    makeTransactions(trans, (int) (amount));
                                    System.out.println("===================================");
                                } else {
                                    System.out.println(  "| User not Found!                        |"  
                                            "\n| Possible Cause:                        |"  
                                            "\n| -User does not exist in MyATM accounts |"  
                                            "\n| -Incorrect User ID                     |"  
                                            "\n==========================================");
                                }
                            }
                        }
                        //Change PIN
                        else if (ucMenu == 5) {
                            System.out.println("===================================");
                            System.out.println("|            Change PIN           |");
                            System.out.println("===================================");
                            System.out.println("|        Change PIN for " currentUsr "     |");
                            System.out.print("|  Input new PIN : ");
                            String ppin = user_trans.nextLine();
                            String chPin = ppin;
                            User1.setPin(chPin);
                            System.out.println("|     You're new PIN : " User1.getPin() "     |");
                            System.out.println("===================================");
                        }
                        //Log Out
                        else if (ucMenu == 0) {
                            System.out.println("===================================");
                            System.out.println("|           Logging out...        |");
                            System.out.println("===================================");
                            break;
                        }
                        //Empty
                        else {
                            System.out.println(" ");
                        }
                    }
                }
            }
            else if (uChoice==2) {
                    System.out.println("_________________________________________");
                    System.out.println("|       Thank you for Using MyATM®      |");
                    System.out.println("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
                    System.out.println("Exiting...");
                    System.exit(0);
            }
            else {
                    System.out.println("_________________________________________");
                    System.out.println("| Please Select Only The Given Numbers. |");
                    System.out.println("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
                    mistakes  ;
                }
        }
        //Exit code when Maximum count of mistakes is reached.
        if (mistakes==3) {
            System.out.println("Consecutive Errors Detected!" "\nNow Exiting...");
            System.exit(0);
        }
         //End for Log in and Exit Menu code
    }
    //Previous Transactions Method
    //Shows Transactions
    public static void showTransactions(int[] trans, int balance ) {
        System.out.println();
        System.out.println("Previous Transactions: ");
        System.out.println("-------------------------------------------\n");

        for(int i = 0; i < trans.length; i  ) {
            if(trans[i] == 0) {
                System.out.print("");
            }
            else {
                System.out.print(trans[i]   "\n");
                balance = balance   trans[i];
            }
        }
        System.out.println("-------------------------------------------\n");
        System.out.println("Balance: ₱"   balance   "\n" );
    }
    //Makes Transactions
    public static void makeTransactions(int[] trans, int amount) {
        int position = findNr(trans);
        if(position == -1) {
            moveTrans(trans);
            trans[trans.length - 1] = amount;
        }
        else {
            trans[position] = amount;
        }
    }
    //Finds position
    private static int findNr(int[] trans) {
        int position = -1;

        for(int i = 0; i < trans.length; i  ) {
            if (trans[i] == 0) {
                position = i;
                break;
            }
        }
        return position;
    }
    //Moves transaction when a new one is made
    private static void moveTrans(int[] trans) {
        for(int i = 0; i < (trans.length - 1); i  ) {
            trans[i] = trans[i   1];
        }
    }
}

This is my methods and functions class

import java.util.Vector;

public class MyATM_Accounts {
    //
    //ATM Basic Functions and Features
    //
    private double balance;
    private String name;
    private String pin;
    private long accNum;

    public MyATM_Accounts(String name, String pin, long accNum, double balance) {
        this.name = name;
        this.pin = pin;
        this.accNum = accNum;
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    public String getName() {
        return name;
    }

    public String getPin() {
        return pin;
    }

    public void setPin(String pin) {
        this.pin = pin;
    }

    public long getAccNum() {
        return accNum;
    }
    public void deposit(double amount) {
        balance  = amount;
        System.out.println("| "   name   " | has ₱"   balance   "   |");
    }

    public void withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
            System.out.println("| "   name   " | has ₱"   balance   "   |");
        } else {
            System.out.println("| Withdrawal by | "   name   " | FAILS |");
        }
    }

    public void transfer(double amount, MyATM_Accounts acc) {
        if (this.balance < amount) {
            System.out.println("|      Transfer Fails    |");
        } else if (amount == 0) {
            System.out.println("|   Transfer Cancelled   |");
        } else {
            this.balance -= amount;
            acc.balance  = amount;
            System.out.println("|  Account of  ("   this.name  
                    ")   \n|Becomes ₱"   this.balance   "  ");
            System.out.println("|  Account of  ("   acc.name  
                    ")   \n|Becomes ₱"   acc.balance   "   |");
        }
    }
}

CodePudding user response:

The problem is that it reinitializes the transactions array to an empty array on each iteration of the menu loop:

int[] trans = new int[10];

You'll need to move this initialization before the start of the loop.

  •  Tags:  
  • java
  • Related