0

I have two Cat objects

   Cat A = new Cat();
   Cat B = new Cat();

then I ask the user which cat he want to modify

   String choice = input.nextLine;

I want to run all my Cat instance methods on choice and change the object to which it refers. For example:

   choice.drinkMilk(15, 2);

if choice was "A" it would execute drinkMilk(15, 2) on cat A and if it was "B" it would do it on B.

Is there any way to do this?

EDIT: Is there any way to do this without HashMap? This is for my introductory Java course and we haven't learned that yet.

albert
  • 8,285
  • 3
  • 19
  • 32
Natantantan
  • 47
  • 2
  • 8
  • You could create getters and setters in your `Cat` class. – iRuth Feb 12 '15 at 02:21
  • I'm taking introduction to Java so you guys are going to have to go a little easy on me. What is map and reflection? And I have getters and setters in the Cat class but how does that help? – Natantantan Feb 12 '15 at 02:23

3 Answers3

3

When you create your cat objects, put them in a HashMap:

Cat A = new Cat();
Cat B = new Cat();
Map<String, Cat> herd = new HashMap<String, Cat>();
herd.put("A", A);
herd.put("B", B);

Then, when the user makes a selection from A or B, you can retrieve the appropriate cat to operate on:

String choice = input.nextLine;
Cat selectedCat = herd.get(choice);
if(selectedCat != null) {
    selectedCat.drinkMilk(15,2);
} else {
    // user made a bad selection, so let them know
}

If you can't use a Map, then you could use an if/else solution:

String choice = input.nextLine;
if("A".equals(choice)) {
    A.drinkMilk(15,2);
} else if("B".equals(choice)) {
    B.drinkMilk(15,2);
} else {
    // user made a bad selection, so let them know
}
Jason
  • 11,744
  • 3
  • 42
  • 46
2

You can add another variable in your cat class called string id which is then initialized after you create the cat object;

Cat A = new Cat();
A.id = "cat1";
Cat B = new Cat();
B.id = "cat2";

then you can do an if statement from the users input with the cat id:

String choice = input.nextLine;

if(choice.Equals(A.id))
//execute cat1
else
//execute cat2
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • Thats what I was doing before but that doesn't really work for me because it gets really complicated and lengthy for one of the things I have to do. And besides what's the point of the id system? This is what I did if(choice.Equals(A) A.drinkMilk else B.drinkMilk – Natantantan Feb 12 '15 at 02:26
  • @Natantantan how many cat objects are you planning to make? if it is a lot then a Map is a way to go. – Rod_Algonquin Feb 12 '15 at 02:27
  • @Natantantan No that wont work because you are comparing an `object `with `string` which will always return false. – Rod_Algonquin Feb 12 '15 at 02:28
  • Well in my real problem it's not cat objects its collection objects and there's only two but one of the things i have to do is move one thing from one collection to another collection and that requires a lot of nested if loops – Natantantan Feb 12 '15 at 02:31
  • @Natantantan what do you mean by collection objects. if it is not the car object paste you snippet of code above. – Rod_Algonquin Feb 12 '15 at 02:44
1

What about this:

HashMap<String, Cat> cats = new HashMap<String, Cat>();

this creates a map

Cat A = new Cat();
Cat B = new Cat();
Cat C = new Cat();
//    ... and so on.

cats.put("A", A); //assigns the appropriate cat object to the key ("A")
cats.put("B", B);
cats.put("C", C);
//    ... and so on for more cats.    

String choice = input.nextLine();

Cat cat = cats.get(cats.get(choice));
if(cat!=null)
cat.drinkMilk(15,2); //get the appropriate cat object.
EDToaster
  • 3,160
  • 3
  • 16
  • 25
  • You should check if the user has made a valid choice - otherwise you get a `NullPointerException`. – Jason Feb 12 '15 at 03:54