Reputation: 13
I'm making a class for a game I'm making and cannot figure out how to return the highest 'reward' to a user:
What I'm trying to get it to do is return the reward based on the player's kills. If he has 38 kills, it should return only 300. Not 150 aswell.
class EnumTest {
private static int playerKills = 38;
private enum KillReward {
BEGINNER(10, 150),
JUNIOR(25, 300),
EXPERT(50, 500),
CHAMPION(100, 1000);
private KillReward(int kills, int reward) {
this.kills = kills;
this.reward = reward;
}
private int kills, reward;
private int getKillsNeeded() {
return kills;
}
private int getKillReward() {
return reward;
}
}
public static void main (String[] args) {
for (KillReward k : KillReward.values()) {
if (playerKills > k.getKillsNeeded()) {
System.out.println("Kills: " + playerKills + "; Reward: " + k.getKillReward());
}
}
}
}
Upvotes: 1
Views: 2668
Reputation: 591
There are a couple ways of doing this.
One easy way is assign the reward to a variable. At the end of that loop the reward
variable below will be the highest kill reward that was applicable
public static void main (String[] args) {
KillReward reward = null;
for (KillReward k : KillReward.values()) {
if (playerKills > k.getKillsNeeded()) {
reward = k;
}
}
System.out.println("Kills: " + playerKills + "; Reward: " + k.getKillReward());
}
Note that this relies on the enum being listed in order which can sometimes be fragile. For example, if a new KillReward enum is added after CHAMPION but has a lower killsNeeded value, then it will not return the proper value.
A better solution would be to create a Comparator and use it to sort the enum values by killsNeeded first, thus ensuring that they are always in order. If you also sort it in descending order, then you can also break from your loop once you hit the first applicable one.
class EnumTest {
private enum KillReward {
BEGINNER(10, 150), JUNIOR(25, 300), EXPERT(50, 500), CHAMPION(100, 1000);
// Sort KillRewards once at initialization
private static final List<KillReward> sortedKillRewards = new ArrayList<KillReward>();
static {
for (KillReward k : values())
sortedKillRewards.add(k);
Collections.sort(sortedKillRewards, new Comparator<KillReward>() {
@Override
public int compare(KillReward o1, KillReward o2) {
return (o1.kills - o2.kills) * -1; // multiplying by -1 makes it
// descending
}
});
}
private KillReward(int kills, int reward) {
this.kills = kills;
this.reward = reward;
}
private int kills, reward;
private int getKillsNeeded() {
return kills;
}
private int getKillReward() {
return reward;
}
public static KillReward forKills(int killCount) {
for (KillReward k : sortedKillRewards)
if (killCount >= k.kills)
return k;
// must not have enough kills for any reward
return null;
}
}
public static void main(String[] args) {
int kills = 9;
System.out.println("Kills: " + kills + "; Reward: "
+ KillReward.forKills(kills));
kills = 10;
System.out.println("Kills: " + kills + "; Reward: "
+ KillReward.forKills(kills));
kills = 38;
System.out.println("Kills: " + kills + "; Reward: "
+ KillReward.forKills(kills));
}
}
Upvotes: 1