Results 1 to 8 of 8
- 04-20-2011, 07:06 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
Refferencing a string to an existing object
Hi all,
I'm currently learning Java from some books for an OU course that my dad did. One of the situations used to help with the learning is a bank accounts setting. As I want to create an Android app around as similar idea, I was changing some of the source code for the "Accounts" class to make it more user friendly for my own use in the future.
I have a method called transfer, allowing the user to transfer money from one account to another. Previously this method had two arguments, amount and account, however I have changed the method to the following:
The problem I have is that after the user has input the account they wish to transfer to via the dialog box, this is stored as a string. This means that at the line "account.credit(amount);" the method credit is searched for in the class string, rather than in the account class and run on the object with the same name as the string. Is there a way I can reference the string account that the user has entered to an existing object of the class account, so the method credit will then run on this?Java Code:public void transfer() { int amount = Integer.parseInt(OUDialog.request("How much do you want to transfer")); String account = OUDialog.request("Which account do you want to transfer the money to?"); if (amount <= this.getBalance()) { this.setBalance(this.getBalance() - amount); account.credit(amount); OUDialog.alert("Your new balance is " + this.getBalance); } else { OUDialog.alert("You do not have sufficient funds for this transaction"); } }
Thanx
Aaron
- 04-20-2011, 07:10 PM #2
I'm not sure I understand what you're asking, but I would guess you either want to convert the String to a number (check out the API for Integer) or use a Map of Strings to Accounts.
Edit- I see what you're talking about now. How are Accounts stored? What kinds of Strings are going to be entered?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-20-2011, 07:34 PM #3
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
In the textbook im using the examples given for account names are things like "myAccount" and "hisAccount"
- 04-20-2011, 11:24 PM #4
Personally I wouldn't have the transfer method in the Account class. I would have it in the Bank class or whatever class you are using as the Controller. Then you pass two Account objects and the amount to the method.
Java Code:public void transfer(Account fromAccount, Account toAccount, double amount) { if from account has sufficient funds { from account withdraw amount to account deposit amount } else { error message } }
- 04-21-2011, 07:28 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
Thanks for the help.
At the moment I am unfamiliar with the concept of a controller class.
Originaly the method was similar to that, using to arguments (Account toAccount, double anAmount), however it was a boolean and returned the values true or false if the transfer worked or not, however I was trying to edit the code to what I saw as being more "user friendly" where the user could input amounts and the account, and messages were returned.
Is there a way that in a method the user can input the arguments?
Thanx
Aaron
- 04-21-2011, 01:18 PM #6
Were "myAccount" and "hisAccount" variable names?
I'm still pretty sure you're just looking to use a Map.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-21-2011, 04:03 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
They are variable names.
What is a map and how does it work?
Thanx
Aaron
- 04-21-2011, 04:32 PM #8
Did you google "java map" or "java map tutorial" or look it up in the API? Thos should be your first steps.
But a Map is a data structure that pairs one type of Object to another. So you could pair Strings (your account names) with Accounts. Then you would use the Strings to access the Account, sort of like using an array only with Strings instead of indexes.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
String object Help
By mohitchouhan in forum New To JavaReplies: 3Last Post: 02-23-2011, 01:34 PM -
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 04:29 AM -
Adding functions to existing Object
By ThommyW in forum Advanced JavaReplies: 8Last Post: 03-25-2010, 10:21 AM -
String to an object?
By bobbyboyy in forum New To JavaReplies: 3Last Post: 10-27-2009, 07:43 PM -
Object to String.
By Gelembjuk in forum New To JavaReplies: 6Last Post: 10-29-2008, 10:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks