1

I have this class called Food.

Inside of food is an enum FoodType as so:

public class Food {

public enum FoodType {
    STRAWBERRY, COOKIE, CHILLI, APPLE, BURGER, HOTDOG, PIZZA, LEMON
}
FoodType foodType;
...
}

and in my constructor for food then I have this:

public Food(Vector2 position) {
    ...

    this.foodType=generateFoodType();
}

Where generateFoodType will randomly return one of the values from my enum.

The problem I am having is that certain other properties of a food will depend on its foodtype (size for example). This forces me to constantly check what the foods type is before setting its properties and I find myself constantly writing switch statements and just making things look like a total mess.

I have thought about making subclasses of Food instead of using an enum but I'm wondering if anyone can see another way, as I could potentially end up with a lot of classes if I do this.

tckmn
  • 57,719
  • 27
  • 114
  • 156
Colin
  • 267
  • 4
  • 11
  • @DoorknobofSnow Sorry I'm using Java – Colin Jan 14 '14 at 14:05
  • `public class Fruit extend Food`, and then override virtual members. Possibly you will not need one class for every fruit. – Raul Andres Jan 14 '14 at 14:07
  • Create an enum with properties like this [Planet](http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html) enum – pillingworth Jan 14 '14 at 14:13
  • Don't know if it applies but remember that enums are simply a class and you can put lots of stuff inside of them, like methods. So your `FoodType` can be a rather elaborate enum of you so desire. You can potentially put various helper methods in there. But then again: sub-classing `Food` may be a better option, dunno. – peterh Jan 14 '14 at 14:14

4 Answers4

1

Just add the properties to the enum values. This question contains lots of ways how to do that: Conveniently map between enum and int / String

You can even add methods to the enum and override them for each value.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

Give your enum more power: it can possess any properties and methods you want and each member can even override them if needed. This is a simple example with size:

public enum FoodType {
  STRAWBERRY(2), COOKIE(4), CHILLI(1), APPLE(8), 
  BURGER(12), HOTDOG(11), PIZZA(18), LEMON(3);

  public final int size;
  private FoodType(int size) { this.size = size; }
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Make a method wich will encapulate one switch. After that you will call this method every time you want to use the switch statement. You will set as argument of the method the value you want to compare.

public Object doCHeck(String comparableValue) {

    //do the check

    return object; //return a boolean or whatever you want
}

I think this is the better and cleaner solution for your problem.

0

I wouldn't hardcode the data. If you want a clean implementation your code shouldn't know about the specific details of food. Outsource it into a text-file:

Name="Strawberry",
Color="Red",
Size=10

As you are using Java look for a JSON or XML lib. This way you have one Food class getting the data from the text-file just implementing the logic to use these various stats.

Bonus: Any non-programmer can change stats or even add new food!

haschny
  • 3
  • 4