-1

I'm trying to make a database type program and wanted to know if I should use enums or classes to represent the properties of each item in the database.

I'll do other stuff later on, but to try out the coding side I'm using Pokemon as an example. So, my current idea is to have a Pokemon class. The basic instance variables that represent the properties of each Pokemon object are the first and second type. Type is, right now, its own class, and each type has the property of being weak to other types. This is represented by a list of other types. I don't know much about enums, but from what I've read they're good for when you need an unchangeable set of values, and I thought that that would be good for the types. However, when I was trying to define the list of weaknesses in the enum constructor, I ran into the problem of trying to add other types to the list when they hadn't been initialized yet.

Should I make Type a class instead of an enum? Should I make Pokemon an enum instead of a class?

import java.util.ArrayList;

public enum Type {
    NORMAL,
    FIGHTING,
    FLYING,
    POISON,
    GROUND,
    ROCK,
    BUG,
    GHOST,
    FIRE,
    WATER,
    GRASS,
    ELECTRIC,
    PSYCHIC,
    ICE,
    DRAGON;

private String name;
private ArrayList<Type> weaknesses;

private Type(){
    generateWeaknesses();
}

private void generateWeaknesses(){
    switch(this){
    case NORMAL:
        weaknesses.add(FIGHTING);
        break;
    case FIGHTING:
        weaknesses.add(FLYING);
        weaknesses.add(PSYCHIC);
        break;
    case FLYING:
        weaknesses.add(ROCK);
        weaknesses.add(ELECTRIC);
        weaknesses.add(ICE);
        break;
    case POISON:
        weaknesses.add(GROUND);
        weaknesses.add(BUG);
        weaknesses.add(PSYCHIC);
    case GROUND:
        weaknesses.add(WATER);
        weaknesses.add(GRASS);
        weaknesses.add(ICE);
        break;
    case ROCK:
        weaknesses.add(FIGHTING);
        weaknesses.add(GROUND);
        weaknesses.add(WATER);
        weaknesses.add(GRASS);
        break;
    default:
        break;
    }
}
public ArrayList<Type> getWeaknesses(){
    return weaknesses;
}

}

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
user3010445
  • 215
  • 1
  • 2
  • 9
  • Could we see the code for when you tried to create enums for the weaknesses? – GBlodgett May 01 '18 at 15:26
  • I understand that there are quite a lot of pokemons. Maybe you don't want all of them hard-coded in an enum, but described in some kind of config that you can load into instances of a class. – khelwood May 01 '18 at 15:27
  • You might want to take a look at https://stackoverflow.com/questions/19429343/self-referential-enum-with-immutable-parameters – luk2302 May 01 '18 at 15:41

1 Answers1

0

An enum should be used to express something that has a relatively small set of values that do not change over time. MALE and FEMALE aren't the best examples these days, but you get the idea.

In the case of Pokémon, there are about 20 types. Those you could indeed put in an enum, but you have to figure out for yourself whether it makes sense. If you want to do the calculation which type is weak to another programmatically, then an enum could make sense. But if you store this in a database table with around 400 entries (20 types times 20 types), there's not much to be gained by using enums.

So it depends a bit. In this particular case, I would define types in the database, and their weaknesses as well, just in case somebody at Nintendo gets the bright idea of adding more types.

SeverityOne
  • 2,476
  • 12
  • 25