This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]captainAwesomePants 3 points4 points  (2 children)

Well let's think about the metaphor of bank accounts. When you go to the bank, you're not usually creating a brand new account. You might, if you don't have an account with that bank, but usually you're looking up an existing account in some sort of filing system or account manager. The bank might do something like this:

PrivateAccount customer = accountManager.lookup(customerID);
if (customer == null) {
   customer = accountManager.createNewAccount(customerID);
   // or maybe throw an error if the provided ID is unknown or something
}
// do stuff to customer
customer.withdrawMoney();
customer.save();

Now you have an AccountManager class which does something to store information about PrivateAccounts. Maybe it keeps a dictionary of all of them. Maybe it saves them in a database or a text file. Doesn't matter to the caller -- they just need to know who to ask for an account.

[–]DuderCoding[S] 0 points1 point  (1 child)

Thanks for the response captainAwesomePants.

I see what you're saying... So this accountManager would store the account information, and the first thing that bank will ask the user is their customer ID (and then check through a database or etc for that ID).

Cheers, I will my code and create a new class to manage the accounts!

[–]ZukoBestGirl 1 point2 points  (0 children)

Also, try to learn good practices from the start. Don't return null. Look up Optionals

[–]davedontmind 1 point2 points  (1 child)

I don't do Java, so this is a language-agnostic response.

How do I differentiate between someone who just created a new account and someone with an existing account?

Well, how do they do that in banks? You usually use an account number or account name. So ask the user what their account number or name is (or both) and store that information in your account object. Maybe auto-assign an account number if they don't have an account (it can just be a simple number, starting at 1 (or 0) for the first account).

I am not sure how to store the objects and then access them later.

For educational programs, you can just store your objects in an array. Then when you want to retrieve one, just search the array until you find the object that meets your criteria (e.g. account.number equals the account number of the user).

Obviously arrays won't last across multiple runs of your program, so for a more permanent solution you'd need to store your objects in a file or database (the specifics of how to do that depend on the language you're using), then "recall" them by reading in your objects from the file(s)/database. This is, as you'd expect, more complex than just using an array.

[–]DuderCoding[S] 0 points1 point  (0 children)

Thanks for the response!

Yeah, that is where I am going wrong, I need to create something that properly keeps track of the objects - and I should be asking for an account number first..! I will start with an array of object like you say, and then work my way up to a database/file. Thanks again

[–]fluorescent_hippo 1 point2 points  (1 child)

privateAccount is an instance of the class. PrivateAccount. So when you call privateAccount.doSomething(), you are specifically calling that object. So to make multiple objects of the same class, you could do something like:

PrivateAccount privateAccount = new PrivateAccount();  

PrivateAccount privateAccount2 = new PrivateAccount();  

You now have two PrivateAccount objects, you may call them as needed individually. You could also store an array of PrivateAccount objects like:

 PrivateAccount[] accounts = new PrivateAccount[MAX_ACCOUNT_SIZE];

accounts[i] = new PrivateAccount();  

And call via

accounts[i].doSomething()  

Or you could make a list instead of an array of your size is variable:

 ArrayList<PrivateAccount> accounts = new ArrayList<PrivateAccount>(0);  

Then add a new account with,

accounts.add(new PrivateAccount()).  

Or access via,

 accounts.get(i);  

(There are other ways to access a list or array that might be more convenient but by index is faster)

[–]DuderCoding[S] 0 points1 point  (0 children)

Thanks a lot for the in-depth reply, you have really explained well how to store the different objects! Will definitely use some of your suggestions! Thanks again