• Post Reply Bookmark Topic Watch Topic
  • New Topic

3 array help  RSS feed

 
charlie conway
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create an array of three account that hold objects( id and balance). If the person want to create an account, he enter an id, goes through menu and set final balance in object array. Then if a person has an existing account, he can enter in the id and goes through the menu(deposit,withdraw,check,etc.) The problem is that I can set the id, but after i go through the menu(deposit, withdraw, etc), i can't set the final balance to the .setbal . Plus, it reinitiate a new array so i can't get it to look for an existing account.
I really appreciate it if somebody could help me with this program.



And I have another class to setter and getter for id and balance

 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure which class is CheckingAccount mentioned in the first line in the main method in your code. I've assumed the class name here is CheckingAccount instead of Bank as mentioned by you.

Also, I believe the array initialization should be at the class level (or in the main method), so that it would not re-initialize the array every time the accountArray() method is called.

I've tried make a few changes in your code. The updated code is as follows:



Just check if this works as expected.


-Pushkar
 
charlie conway
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pushkar's tips work really great. Except now i put the Account[] sarray = new Account[3] in the class level, it can create as many account as it wants. Is there anyway i can put the Account[] sarray = new Account[3] so that I create only 3accounts?
Any advices here would be helpful.
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it wasn't creating any more than 3 accounts, but you thought it was doing so because it was just looping through giving you the same menu options. I ran the code in debugger mode and checked that after you have created 3 accounts, even if you say "Create account" it wasn't really creating one more. The problem was that you wouldn't know this since it did not tell you that "you already have 3 accounts and it would not be possible to create one more".

I've modified the code a bit to add this.




What I've basically added is a counter for the number of accounts created called "accountsCount". This is initialized to 0 at the start and incremented every time a new account is created.

I've also added an "if" condition check inside the if (ask == 1) block (when you opt for "create account"), which will check the accountsCount counter against the length of the array. If the counter value is already equal to the array length, then it would print a warning indicating the same and take you back to the main menu without creating a new account. Only if the condition check for the counter evaluates to false, then the code will create a new account.

Hope this solves your problem.

-Pushkar
 
charlie conway
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pushkar Choudhary wrote:Actually it wasn't creating any more than 3 accounts, but you thought it was doing so because it was just looping through giving you the same menu options. I ran the code in debugger mode and checked that after you have created 3 accounts, even if you say "Create account" it wasn't really creating one more. The problem was that you wouldn't know this since it did not tell you that "you already have 3 accounts and it would not be possible to create one more".

I've modified the code a bit to add this.

What I've basically added is a counter for the number of accounts created called "accountsCount". This is initialized to 0 at the start and incremented every time a new account is created.

I've also added an "if" condition check inside the if (ask == 1) block (when you opt for "create account"), which will check the accountsCount counter against the length of the array. If the counter value is already equal to the array length, then it would print a warning indicating the same and take you back to the main menu without creating a new account. Only if the condition check for the counter evaluates to false, then the code will create a new account.

Hope this solves your problem.

-Pushkar

I am kinda of new at Java. Anyway,thanks for taking the time to help me. I really appreciate it
 
Aaron Jaconis
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a similar project to this one (we might have the same teacher). Anyway I think I have everything figured out correctly but when I go to the "Enter Account Number" section all I get is 0.0 and no account number or balance. Could someone tell me what I am doing wrong.

We have to break the program apart into a runnable file and the have it call the other file with the attributs from another file.

My runnable coding


my attribute coding


Any help would be appreciated!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!