0

I'm trying to create a menu for the user to view all the items in a map and make a selection. Unfortunately the output isn't quite right because it is calling the default tostring function for the pairs.getValue() call, and I want it to call a custom function getName(). If I try using pairs.getValue().getName() I get a syntax error because it expects this to be an object, and objects don't have a getName function. I tried casting it like (Item)pairs.getValue().getName() and that didn't work either so I'm not really sure how to call the getName function for the proper output. I also don't have an effective way to let the user make a selection since I am using a map. If they enter in "1" I can't actually get use 1 in the switch to grab the actual item being outputted first because maps don't use indexes... I can't think of a good way to allow the user to just enter in a number and be able to grab the actual object from the map, so a suggestion for a new approach for that would be awesome... If it were C++ I would grab the map.iterator.begin() and + the int to get the location... but I don't think you can do that in Java. Any ideas are appreciated, Thanks!

Iterator it = character.get("Items").entrySet().iterator();
int i = 1;
while(it.hasNext())
{
   Map.Entry pairs = (Map.Entry)it.next();
   System.out.println("[" + i + "] " + pairs.getKey() + " = " + pairs.getValue());
   i++;
}
System.out.print("\nYour Choice: ");
i = reader.readInt(1,character.get("Items").size());
switch(i)
{
    //foo
}

If you want to see any other code I'll add it, just let me know. Thanks!

Character

/**
* Stats
* Items
* BodyParts
* Attributes
* Abilities
* Skills
* Effects
* Quests
* Weapons
* Armors
* 
*
*
* 
**/
import java.util.HashMap;
import java.io.Serializable;
public class Character extends GameObject
{
protected HashMap<String, HashMap> character;
protected HashMap<String, Weapon> weapons;
protected HashMap<String, Armor> armors;
protected HashMap<String, Attribute> attributes;
protected HashMap<String, Skill> skills;
protected HashMap<String, Effect> effects;
protected HashMap<String, Stat> stats;
protected HashMap<String, Ability> abilities;
protected HashMap<String, Quest> quests;
protected HashMap<String, Item> items;
protected HashMap<String, Response> responses;
protected HashMap<String, Implant> implants;

public Character()
{
    name = "Greg";
    description = "Just a regular guy living in a binary world";
    buildCharacter();
}
public Character(String newName, String newDescription)
{
    name = newName;
    description = newDescription;
    buildCharacter();
}
public Character(String newName, String newDescription, HashMap<String, Weapon> newWeapons, HashMap<String, Armor> newArmors, HashMap<String, Response> newResponses, HashMap<String, Attribute> newAttributes, HashMap<String, Skill> newSkills, HashMap<String, Effect> newEffects, HashMap<String, Stat> newStats, HashMap<String, Ability> newAbilities, HashMap<String, Quest> newQuests, HashMap<String, Item> newItems, HashMap<String, Implant> newImplants, int level, int coins, int health, int mana, int energy, int power, int accuracy, int toughness, int dexterity, int speed, int stealth, int concentration, int crit, int vision, int hearing, int smell, int temperature, int hunger, int thirst, String type)
{
    character = new HashMap<String, HashMap>();
    name = newName;
    description = newDescription;
    weapons = newWeapons;
    armors = newArmors;
    responses = newResponses;
    attributes = newAttributes;
    abilities = newAbilities;
    quests = newQuests;
    items = newItems;
    implants = newImplants;
    character.put("Weapons",weapons);
    character.put("Armors",armors);
    character.put("Attributes",attributes);
    character.put("Skills",skills);
    character.put("Effects",effects);
    character.put("Stats",stats);
    character.put("Abilities",abilities);
    character.put("Quests",quests);
    character.put("Items",items);
    character.put("Responses",responses);
    character.put("Implants",implants);
    skills.put("Level", new Skill("Level","Overall Level",level));
    items.put("Coins",new Item("Coins","Your Money", coins));
    attributes.put("Health",new Attribute("Health","Your hit points",1,health));
    attributes.put("Mana",new Attribute("Mana","Magic casting resource",1,mana));
    attributes.put("Energy",new Attribute("Energy","Physical ability resource",1,energy));
    attributes.put("Power",new Attribute("Power","Damage with attacks",1,power));
    attributes.put("Accuracy",new Attribute("Accuracy","Chance to hit",1,accuracy));
    attributes.put("Toughness",new Attribute("Toughness","Health regeneration / Physical damage reduction",1,toughness));
    attributes.put("Dexterity",new Attribute("Dexterity","Energy regeneration / Dodge chance",1,dexterity));
    attributes.put("Speed",new Attribute("Speed","Global cooldowns",1,speed));
    attributes.put("Stealth",new Attribute("Stealth","Ability to remain hidden",1,stealth));
    attributes.put("Concentration",new Attribute("Concentration","Mana regeneration / magical resistance",1,concentration));
    attributes.put("Crit",new Attribute("Crit","Chance to hit criticals",1,crit));
    attributes.put("Vision",new Attribute("Vision","View Distance",1,vision));
    attributes.put("Hearing",new Attribute("Hearing","Ear range",1,hearing));
    attributes.put("Smell",new Attribute("Smell","Smell Distance",1,smell));
    attributes.put("Temperature",new Attribute("Temperature","Current Temperature",1,temperature));
    attributes.put("Hunger",new Attribute("Hunger","Hunger Levels",1,hunger));
    attributes.put("Thirst",new Attribute("Thirst","Amount of thirstiness",1,thirst));
    stats.put("Type",new Stat("Type",type));
}
public void buildCharacter()
{
    character = new HashMap<String, HashMap>();
    responses = new HashMap<String, Response>();
    weapons = new HashMap<String, Weapon>();
    armors = new HashMap<String, Armor>();
    attributes = new HashMap<String, Attribute>();
    skills = new HashMap<String, Skill>();
    effects = new HashMap<String, Effect>();
    stats = new HashMap<String, Stat>();
    abilities = new HashMap<String, Ability>();
    quests = new HashMap<String, Quest>();
    items = new HashMap<String, Item>();
    implants = new HashMap<String, Implant>();
    character.put("Weapons",weapons);
    character.put("Armors",armors);
    character.put("Attributes",attributes);
    character.put("Skills",skills);
    character.put("Effects",effects);
    character.put("Stats",stats);
    character.put("Abilities",abilities);
    character.put("Quests",quests);
    character.put("Items",items);
    character.put("Responses",responses);
    character.put("Implants",implants);
    skills.put("Level", new Skill("Level","Overall Level",1));
    items.put("Coins",new Item("Coins","Your Money"));
    attributes.put("Health",new Attribute("Health","Your hit points",1,10));
    attributes.put("Mana",new Attribute("Mana","Magic casting resource",1,10));
    attributes.put("Energy",new Attribute("Energy","Physical ability resource",1,10));
    attributes.put("Power",new Attribute("Power","Damage with attacks",1,1));
    attributes.put("Accuracy",new Attribute("Accuracy","Chance to hit",1,1));
    attributes.put("Toughness",new Attribute("Toughness","Health regeneration / Physical damage reduction",1,1));
    attributes.put("Dexterity",new Attribute("Dexterity","Energy regeneration / Dodge chance",1,1));
    attributes.put("Speed",new Attribute("Speed","Global cooldowns",1,1));
    attributes.put("Stealth",new Attribute("Stealth","Ability to remain hidden",1,1));
    attributes.put("Concentration",new Attribute("Concentration","Mana regeneration / magical resistance",1,1));
    attributes.put("Crit",new Attribute("Crit","Chance to hit criticals",1,1));
    attributes.put("Vision",new Attribute("Vision","View Distance",1,1));
    attributes.put("Hearing",new Attribute("Hearing","Ear range",1,1));
    attributes.put("Smell",new Attribute("Smell","Smell Distance",1,1));
    attributes.put("Temperature",new Attribute("Temperature","Current Temperature",1,1));
    attributes.put("Hunger",new Attribute("Hunger","Hunger Levels",1,1));
    attributes.put("Thirst",new Attribute("Thirst","Amount of thirstiness",1,1));
    stats.put("Type",new Stat("Type","Human"));
    //effects
    //abilities
    //inventory
    //dialogue
}
//getResponse
public HashMap get(String key)
{
    return character.get(key);
}
public Weapon getWeapon(String key)
{
    return weapons.get(key);
}
public void setWeapon(String key, Weapon newWeapon)
{
    weapons.put(key,newWeapon);
}
public Armor getArmor(String key)
{
    return armors.get(key);
}
public void setArmor(String key, Armor newArmor)
{
    armors.put(key,newArmor);
}
public Attribute getAttribute(String key)
{
    return attributes.get(key);
}
public Skill getSkill(String key)
{
    return skills.get(key);
}
public Stat getStat(String key)
{
    return stats.get(key);
}
public Ability getAbility(String key)
{
    return abilities.get(key);
}
public Quest getQuest(String key)
{
    return quests.get(key);
}
public Item getItem(String key)
{
    return items.get(key);
}
public Effect getEffect(String key)
{
    return effects.get(key);
}
public Response getResponse(String key)
{
    return responses.get(key);
}
}

In case you wanted to see the parent...

import java.util.HashMap;
import java.io.Serializable; 
public class GameObject
 {
protected String name, description;
protected int number;
protected boolean bool;
public GameObject()
{
    name = "Typical name of gameobjects";
    description = "What would you expect - it's a game object";
    bool = false;
    number = 1;
}
public String getName()
{
    return name;
}
public void setName(String newName)
{
    name = newName;
}
public String getDescription()
{
    return description;
}
public void setDescription(String newDescription)
{
    description = newDescription;
}
public int getNumber()
{
    return number;
}
public void setNumber(int newNumber)
{
    number = newNumber;
}
public boolean getBool()
{
    return bool;
}
public void setBool(boolean newBool)
{
    bool = newBool;
}
}
CodeManiak
  • 1,903
  • 4
  • 19
  • 32

2 Answers2

1

I believe you are looking for something like this:

((Item)pairs.getValue()).getName()
Josh M
  • 11,611
  • 7
  • 39
  • 49
  • 1
    You are right, my java is rusty since i've been doing C++ so much lately. Thanks, now do you perhaps have any other smart ideas about how to allow my user to make a selection and then use that selection to grab the associated value? I get confused since the way I have it set up is that the user selects an int, but the key is still a string. – CodeManiak Jan 12 '14 at 03:27
1

The proper thing to use here is type parameters (Generics in Java world). This is a way to tell compiler what type you want to store in a collection.

As you're attempting to store objects of different type in a single map we'll take the GameObject as a base type for the collection. First, lets declare a proper content type for the main storage:

protected HashMap<String, HashMap<String, ? extends GameObject>> character;
...
character = new HashMap<String, HashMap<String, ? extends GameObject>>();

which means we have the following pseudocode mapping:

String -> Map(String -> something extending GameObject)

Next, we need to alter accesor method to reflect detailed return type:

//getResponse
public HashMap<String, ? extends GameObject> get(String key) {
    return character.get(key);
}

and put proper type boundaries on usage site:

    int i = 1;
    for (Map.Entry<String, ? extends GameObject> item : character.get("Items").entrySet()) {
        System.out.println("[" + i + "] " + item.getKey() + " = " + item.getValue().getName());
        i++;
    }

For me it compiles and runs without errors or warnings.

Jk1
  • 11,233
  • 9
  • 54
  • 64
  • 1
    See also ["What is a raw type and why shouldn't we use it?"](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Radiodef Jan 12 '14 at 03:43
  • I still get a slight issue with this statement `Iterator> it = character.get("Items").entrySet().iterator();` in the fact that the right side doesn't have enough specificity. How do I add the Specificity to the right side so that I don't get an Xlint error? – CodeManiak Jan 12 '14 at 07:19
  • It depends on how 'character' is declared and what type is realy stored inside. Please add this code and we'll easily eliminate all the compiler warnings. – Jk1 Jan 12 '14 at 10:25