Skip to content
Advertisement

ATM Machine in Java

I have been trying to figure out what is going wrong with this program I’m writing. Heres the full description of what I’m trying to do: Use the Account class created in Programming Exer- cise 9.7 to simulate an ATM machine. Create ten accounts in an array with id 0, 1, . . . , 9, and initial balance $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, the main menu is displayed as shown in the sample run. You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing money, and 4 for exiting the main menu. Once you exit, the system will prompt for an id again. Thus, once the system starts, it will not stop.

The program now only displays the default $100 after each purchase. I’ve realized the problem is that my accounts are being recreated each time after a transaction. My question is basically what can I do or where can I rewrite the account creation so I avoid this problem. I’m new to programming so I’m still struggling. Thanks in advanced.

import java.util.Date;
import java.util.Scanner;
public class test {

    public static void main(String[] args) {

            mainMenuOptions();      
    }
            //main menu option method
    public static void mainMenuOptions(){
        Scanner input = new Scanner(System.in);
        //enter id
        System.out.println("Enter an id: "); 
        int id = input.nextInt();
        //create accounts
          Account [] accounts = new Account[10];
        //initialize 100 dollars
        for (int i = 0; i < accounts.length; i++) { 
            accounts[i] = new Account(i, 100); 
        }
        int index = -1; 
        for(int i = 0; i < accounts.length; i++) {
            if(accounts[i].getid() == id) {
            index = i; 
            i = accounts.length;
            }               
        }
        if(id <= 9){
            //main menu
            mainMenu();
            //user enters choice in main menu   
            int enterchoice = input.nextInt();                          
            if(enterchoice == 1){
                System.out.println("The balance is " + accounts[index].getbalance());
                mainMenuOptions();
            }
            else if(enterchoice == 2){
                System.out.println("Enter an amount to withdraw ");
                double amount = input.nextDouble();
                //withdraw method
                accounts[index].withdraw(amount);
                mainMenuOptions();
            }
            else if(enterchoice == 3){
                System.out.println("Enter an amount to deposit ");
                double amount = input.nextDouble();
                //deposit method
                accounts[index].deposit(amount);
                mainMenuOptions();
            }
            else if(enterchoice == 4){
                mainMenuOptions();
            }   
        }
        else{
            System.out.println("Please enter a correct id");
            mainMenuOptions();
        }

    }
    //main menu method
    public static void mainMenu(){
        System.out.println("Main menu"+"n1:check balance"+"n2:withdraw"
                +"n3:deposit"+"n4:exit"+"nEnter a choice");

    }
    }
        class Account{
            private int id = 0;
            private double balance = 0;
            private double withdraw = 0;
            private double deposit = 0;
            private double amount = 0;

            Account(){
            }

            Account(int id, double balance){
                this.id = id;
                this.balance = balance;
            }

            public int getid(){
                return this.id;

            }
            public void setid(int newid){
                id = newid;
            }
            public double getbalance(){
                return this.balance;
            }
            public void withdraw(double amount){
                balance = balance - amount;
            }

            public void deposit(double amount){
                balance = balance + amount;
            }
        }

EDIT: When I go to check balance it will re loop me back to the scanners for id and choice. I’m not sure what the problem is here, can someone try to run my code or give me some insight on what might be the problem. Thanks. The program is supposed to be able to go through any choice of transactions, simulating an ATM machine, the id being a number between 0-9. Here is the new updated code.

import java.util.Date;

import java.util.Scanner;

public class test {

    private static Account[] accounts = new Account[10];

    public static void main(String[] args) {
        accounts();
        mainMenuOptions();      
}
    //main menu option method
    public static void mainMenuOptions() {
        Scanner input = new Scanner(System.in);

        int enterchoice = -1;
        int id=-1;
        while (enterchoice != 4) {
          mainMenu();
          System.out.println("Enter an id: ");
          id = input.nextInt();
          //enter id
          System.out.println("Enter choice:  ");
           enterchoice = input.nextInt();

          int index = -1;
          for (int i = 0; i < accounts.length; i++) {
            if (accounts[i].getid() == id) {
              index = i;
              break;
            }
          }
          if (enterchoice == 1) {
            System.out.println("The balance is " + accounts[index].getbalance());       
          } else if (enterchoice == 2) {
            System.out.println("Enter an amount to withdraw ");
            double amount = input.nextDouble();
            //withdraw method
            accounts[index].withdraw(amount);               
          } else if (enterchoice == 3) {
            System.out.println("Enter an amount to deposit ");
            double amount = input.nextDouble();
            //deposit method
            accounts[index].deposit(amount);
          } 
        }
      }

     public static void accounts() {
        //create accounts
        //initialize 100 dollars
        for (int i = 0; i < accounts.length; i++) {
            accounts[i] = new Account(i, 100);
        }
    }
    //main menu method
    public static void mainMenu(){
        System.out.println("Main menu"+"n1:check balance"+"n2:withdraw"
                +"n3:deposit"+"n4:exit");

    }
    }
        class Account{
            private int id = 0;
            private double balance = 0;
            private double withdraw = 0;
            private double deposit = 0;
            private double amount = 0;

            Account(){
            }

            Account(int id, double balance){
                this.id = id;
                this.balance = balance;
            }

            public int getid(){
                return this.id;

            }
            public void setid(int newid){
                id = newid;
            }
            public double getbalance(){
                return this.balance;
            }
            public void withdraw(double amount){
                balance = balance - amount;
            }

            public void deposit(double amount){
                balance = balance + amount;
            }
        }

Advertisement

Answer

so you can do it like this bro:

public class test {

private Account [] accounts = new Account[10];

public static void main(String[] args) {

        mainMenuOptions();      
}
        //main menu option method
public static void mainMenuOptions(){
    Scanner input = new Scanner(System.in);
    //enter id
    System.out.println("Enter an id: "); 
    int id = input.nextInt();

    //initialize 100 dollars
    for (int i = 0; i < accounts.length; i++) { 
        accounts[i] = new Account(i, 100); 
    }
    int index = -1; 
    for(int i = 0; i < accounts.length; i++) {
        if(accounts[i].getid() == id) {
        index = i; 
        i = accounts.length;
        }               
    }

now the Object lives outside of the method, so when the method closes out it won’t destroy the object. The accounts will persist and you should be able to do what you’re looking to do from there.

I had the same problem a while back where I had data getting all hosed up in an arraylist. I had the Object declared at the class level, and it was existing in every loop of my For statement causing doubled up entries in the list. I moved it inside the For statement and realized that every time the For statement finished a loop it would destroy the object and reinstantiate it.

Give this a try, I think it should get rid of the problem with the account object being recreated every time you invoke the mainMenuOptions() method.

If you’re worried about memory overages in bigger applications you can also do it like the other poster was talking about by creating it in the main like so

 public class test {

public static void main(String[] args) {
        Account [] accounts = new Account[10];
        mainMenuOptions(accounts);      
}
        //main menu option method
public static void mainMenuOptions(Account accounts){
    Scanner input = new Scanner(System.in);
    //enter id
    System.out.println("Enter an id: "); 
    int id = input.nextInt();

    //initialize 100 dollars
    for (int i = 0; i < accounts.length; i++) { 
        accounts[i] = new Account(i, 100); 
    }
    int index = -1; 
    for(int i = 0; i < accounts.length; i++) {
        if(accounts[i].getid() == id) {
        index = i; 
        i = accounts.length;
        }               
    }

in this version you’ve created the object to live inside the main method. here I instantiated the object and passed it as an argument to mainMenuOptions. This way if you decide that you don’t want this to be globally available to all sub methods within main, you can now make it live within the space of the main method only and use it as an argument where necessary.

Both of these solutions will work. My first suggestion is the quick and dirty since it’s a small program. However, in larger programs you may want to use the second method to prevent the object from being available for use in other methods contained within the class.

Advertisement