I am storing classes like House, Car, Clothes, FacialFeatures; all parts of a Person in a HashMap:
public class Person {
private HashMap<String, PersonalItem> map = new HashMap<String, PersonalItem>();
public void init(){
map.put("house", new House());
map.put("car", new Car());
map.put("clothes", new Clothes());
//etc...
}
public PersonalItem getPersonalItem(String name){
return map.get(name);
}
}
external usage:
public static void main(String[] args){
Person person = getPerson(args);
//if i want to use car.setColor(blue); i have to do:
((Car) person.getPersonalItem("car")).setColor(blue);
//or if i want to use house.setExterior(wooden); i have to do:
((House) person.getPersonalItem("house")).setExterior(wooden);
}
How can I make it so if I were to use the following code:
person.getPersonalItem("house").setExterior(wooden);
It would work and getPersonalItem would return Object instanceof House if the input is "house." I don't want to have to write out a getter every time:
public House getPersonalItemHouse(){
return (House) map.get("house");
}
Is there an alternative?
House getHouse().publicmembers might be a better alternative. Seems like encapsulation is not the big concern here."frontporch","frontPorch", or"front porch"? The best solution by far is to bite the bullet and write the properties. Note that every IDE can generate the get/set methods for you, if you write out just the fields. I bet writing those fields would take less than 30 minutes.