Results 1 to 8 of 8
  1. #1
    Aaron D is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default 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:
    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");
         }
       }
    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?

    Thanx

    Aaron

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default

    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!

  3. #3
    Aaron D is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    In the textbook im using the examples given for account names are things like "myAccount" and "hisAccount"

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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
        }
    }

  5. #5
    Aaron D is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    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

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default

    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!

  7. #7
    Aaron D is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    They are variable names.
    What is a map and how does it work?
    Thanx

    Aaron

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default

    Quote Originally Posted by Aaron D View Post
    They are variable names.
    What is a map and how does it work?
    Thanx

    Aaron
    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

  1. String object Help
    By mohitchouhan in forum New To Java
    Replies: 3
    Last Post: 02-23-2011, 01:34 PM
  2. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  3. Adding functions to existing Object
    By ThommyW in forum Advanced Java
    Replies: 8
    Last Post: 03-25-2010, 10:21 AM
  4. String to an object?
    By bobbyboyy in forum New To Java
    Replies: 3
    Last Post: 10-27-2009, 07:43 PM
  5. Object to String.
    By Gelembjuk in forum New To Java
    Replies: 6
    Last Post: 10-29-2008, 10:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •