0

In a small project I am working on I've gotten stuck. The user enters a command that may be "xp Speed", my command handler class finds that it wants to the XP value of the Speed Instance. In this case it needs to return the value of Skill.Speed.currentXP back to the user.

Small Part of the program:

//Example Instance initialization there is over 40 of these
Skill Speed = (new SkillSpeed(Skills.SKILL_SPEED,Skills.SKILL_SPEED_MODIFIER));

//Constructor for skill class
public Skill(String skillName, double modifier) {
    this.name  = skillName;
    this.minLevel = Skills.MIN_SKILL_LEVEL;
    this.Modifier = 1f;
    this.currentLevel = (int)calculateLevel();
    this.currentXP = 1;
    this.leaderboard = getCurrentLeaderboard();
    this.ID = getNextID();
}

Now, theres one way i could do this. by having a switch statement with case value being the string entered. However I'm sure having 40+ cases in one switch statement must be avoidable. The other theory I have had is creating a array of all current instances then iterating through that list, finding if the user inputted string is equal to the name of that instance, then returning the instance itself. This is what I came up with:

//method inside another classs that attempts to return the appropriate skill Instance
public Skill getSkillFromName(String Name) {
    for(int i = 0; i < Skill.SkillArray.length; i++) {
        final String SkillName = Skill.SkillArray[i].getName();
        if(SkillName.equalsIgnoreCase(Name)) {
            return Skill.SkillArray[i];
        }
    }
    return null;
}

So here's what I need help with:

  • Creating a array of all initialized instances

  • Creating the method that will return Skill."InsertRandomInstanceDependingOnUserInputHere".currentXP

  • Fixing any problems you see in the getSkillFromName() method

Or perhaps I have overlooked a far easier way of doing this, and you can help me with that.

Thanks for the help,

BigDaveNz

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
David Dudson
  • 116
  • 9
  • 1
    A suggestion from my side: Please use also [code review](http://codereview.stackexchange.com/) from stack exchange to review your code. For a seasoned Java programmer it is difficult to read as you use no best practices in your code. also you can post on code review some more code (complete classes). – Uwe Plonus Jul 02 '13 at 09:18

2 Answers2

0

You can use a Map for this. E.g.:

Map<String, Skill> skills = new HashMap<String, Skill>();

To insert the values you put the values into the Map:

skills.put(skill.getName(), skill);

To retrieve your skill you can get the skill by name:

Skill skill = skills.get(name);
Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
  • so do i insert all the values manually ie. skills.put(skill1.getName(), skill1); skills.put(skill2.getName(), skill2); or is there an automated way of doing this when creating the instance itself? And thankyou for the quick reply – David Dudson Jul 02 '13 at 09:20
  • @BigDaveNz You can use a factory to create you instances and then use this to insert all skills into the `Map`. – Uwe Plonus Jul 02 '13 at 09:36
0

If the names of the skills excatly match method names you might find the aswer at "How do I invoke a Java method when given the method name as a string?".

For finding instances by name you can still use Map's.

Community
  • 1
  • 1
Viktor Seifert
  • 636
  • 1
  • 7
  • 17