Which is more suitable for static data - final char or enum?

Regarding my Java poker program, would it be a smart decision to choose enum over chars and ints?

As far as I can tell, assigning a separate integer char value has the advantage of ease of use of mathematical operators when it comes to comparing map values ​​to determine the winner. However, this may be possible with transfers, if so I do not know.

Please can someone explain me adv / negative?

My first option to declare card ranks is to enumerate:

public enum Rank { DEUCE (1), THREE (2), FOUR (3), FIVE (4), SIX (5), SEVEN (6), EIGHT (7), NINE (8), TEN (9), JACK (10), QUEEN (11), KING (12), ACE (13) } public enum Suit { CLUBS (1), DIAMONDS (2), HEARTS (3), SPADES (4) } 

My second option is static trailing characters with assigned int values ​​as such:

  public static final char ACE = 'A'; public static final char TWO = '2'; public static final char THREE = '3'; public static final char FOUR = '4'; public static final char FIVE = '5'; public static final char SIX = '6'; public static final char SEVEN = '7'; public static final char EIGHT = '8'; public static final char NINE = '9'; public static final char TEN = 'T'; public static final char JACK = 'J'; public static final char QUEEN = 'Q'; public static final char KING = 'K'; public Rank (char c) { switch (c) { case TWO: _val = 0; break; case THREE: _val = 1; break; case FOUR: _val = 2; break; case FIVE: _val = 3; break; case SIX: _val = 4; break; case SEVEN: _val = 5; break; case EIGHT: _val = 6; break; case NINE: _val = 7; break; case 'T': _val = 8; break; case 'J': _val = 9; break; case 'Q': _val = 10; break; case 'K': _val = 11; break; case 'A': _val = 12; break; default: _val = -1; } } 

Thanks!

+2
source share
4 answers

I would prefer Enum . The code looks cleaner and the IDEs can help you avoid the case of switch .

+5
source

I suggest you read Point 30: Use enumerations instead of int constants from Effective Java by Joshua Bloch. This explains the many benefits of using enumerations on int constants.

+4
source

Use enumerations. Because of:

  • Style
  • - they look better and cleaner.
  • IDE Integration - IDE may request correct input
  • methods can be transferred by listings that automatically filter with a range check (see below).
  • enums may have methods for returning values ​​and / or performing calculations
  • the data you are dealing with has a certain range of values ​​that will never change, so defining an enumeration instance for each value makes sense

Consider this method:

 public static char addCards(char a, char b); 

I can call it with invalid input

 addCards('x', '$'); 

There is no built-in range check with char . But with transfers it is provided free of charge.


As for your ranking problem, with listings you can just do it

 Rank r1, r2; boolean r1RankedHigherThanR2 = r1.ordinal() > r2.ordinal(); 

The order you define for enum instances is enough to convey the ranking order.

+2
source

Recursions have an ordinal (based on 0). Therefore, you can use them for ranking, and you probably shouldn't duplicate this. e.g. KING.ordinal ().

Also, aside, poker suits do not have a rank - you have your suits in order of rank of the bridge.

0
source

All Articles