Sam
Sam

Reputation: 920

Ranking in Java. How to set it?

I have a Class called ranking. It has certain ranks: general, soldier, corporal, mayor.

Now I need to set it default by ranking:

  1. soldier
  2. corporal
  3. mayor
  4. general.

This is a member:

private static char Ranks[]; // size 4

Where can I set them by default like:

Ranks[0] = soldier;
Ranks[1] = corporal...

I don't really know where to put default values, so program will know that soldier by default is Ranks[0]. Into constructors? Into members?

Upvotes: 1

Views: 1464

Answers (1)

Powerslave
Powerslave

Reputation: 1428

I'd suggest to use an Enum instead.

You could have a constant in the class using the Enum to point out the default and have a constructor/setter to go with another instead.

public enum Rank {
    SOLDIER, CORPORAL, MAYOR, GENERAL
}

...

public class MilitaryPerson {

    private static final Rank DEFAULT_RANK = Rank.SOLDIER;

    private Rank rank;

    public MilitaryPerson() {
        this.rank = DEFAULT_RANK;
    }

    public MilitaryPerson(Rank rank) {
        this.rank = rank;
    }

    // ...

}

An alternate (and better) solution would be to have the default one set up in a config file (e.g. *.properties)

Upvotes: 9

Related Questions