Been given the task of creating a Wallet class where you can create members and transfer money between them. The user can then pick who gives the money and who receives it. Here is a little of the relevant code:
Wallet adamsWallet = new Wallet("Adam", 403.50);
Wallet bentsWallet = new Wallet("Bent", 873);
Wallet clarksWallet = new Wallet("Clark", 319.50);
object_name_here.transfer(other_object_name_here, 200);
So I want the user for example to enter “adam” in the Scanner, then somehow append “sWallet” to adam and then use that as the object name for the method call.
How can I do that or is there a better way of doing it?
Use a
Mapto store the wallets. For example:You can lookup a wallet by name from the map.
Note that objects do not have names – variables have names. An object is not a variable; a variable is a reference to an object. You can have multiple variables that refer to the same object.