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.

JavaScript

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.

JavaScript

import java.util.Scanner;

JavaScript

Advertisement

Answer

so you can do it like this bro:

JavaScript

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

JavaScript

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