revised
Code:
/**
* Card class represents a playing card of 52 faces differing by values and suits.
* Each card contains two variables, namely value and suit.
* A card's face is defined to contain 2 elements, which are structured as arrays, with value in [0] and suit in [1].
* Values are encoded in [2, 14] with Ace/King/Queen/Jack as 14/13/12/11.
* Suits are encoded in [1, 4] with Spade/Heart/Club/Diamond as 4/3/2/1.
* Getters available to read a card's face as defined codes for computational purpose or as strings for printing purpose.
*
* @version 1.0
* @since 2019-11-10
*/
public enum Card{ //declared in ascending order of regular cards (enum uses declared order as natural ordering), so can be sorted without a Comparator (recall enum cannot implement Comparable)
AceSpade(14, 4), AceHeart(14, 3), AceClub(14, 2), AceDiamond(14, 1),
KingSpade(13, 4), KingHeart(13, 3), KingClub(13, 2), KingDiamond(13, 1),
QueenSpade(12, 4), QueenHeart(12, 3), QueenClub(12, 2), QueenDiamond(12, 1),
JackSpade(11, 4), JackHeart(11, 3), JackClub(11, 2), JackDiamond(11, 1),
TenSpade(10, 4), TenHeart(10, 3), TenClub(10, 2), TenDiamond(10, 1),
NineSpade(9, 4), NineHeart(9, 3), NineClub(9, 2), NineDiamond(9, 1),
EightSpade(8, 4), EightHeart(8, 3), EightClub(8, 2), EightDiamond(8, 1),
SevenSpade(7, 4), SevenHeart(7, 3), SevenClub(7, 2), SevenDiamond(7, 1),
SixSpade(6, 4), SixHeart(6, 3), SixClub(6, 2), SixDiamond(6, 1),
FiveSpade(5, 4), FiveHeart(5, 3), FiveClub(5, 2), FiveDiamond(5, 1),
FourSpade(4, 4), FourHeart(4, 3), FourClub(4, 2), FourDiamond(4, 1),
ThreeSpade(3, 4), ThreeHeart(3, 3), ThreeClub(3, 2), ThreeDiamond(3, 1),
TwoSpade(2, 4), TwoHeart(2, 3), TwoClub(2, 2), TwoDiamond(2, 1);
private byte valueAsCode; //numeric value codes
private byte suitAsCode; //spade, heart, club, diamond codes
private String value; //numeric value
private String suit; //spade, heart, club, diamond
private static final String[] valueDecoder = new String[]{"*", "*", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; //for converting values in codes to values in strings
private static final String[] suitDecoder = new String[]{"*", "Diamond", "Club", "Heart", "Spade"}; //for converting suits in codes to suits in strings
private Card(int valueAsCode, int suitAsCode){
this.valueAsCode = (byte)valueAsCode;
this.suitAsCode = (byte)suitAsCode;
}
/**
* getter for the card's face in strings, for display use by caller
* @return 2 elements String array with [0] for value and [1] for suit
*/
public String[] getFace(){
this.value = valueDecoder[this.valueAsCode];
this.suit = suitDecoder[this.suitAsCode];
return new String[]{value, suit};
}
/**
* getter for the card's face in author's defined codes, for computational use by caller
* @return 2 elements byte array with [0] for value and [1] for suit
*/
public byte[] getFaceAsCode(){
return new byte[]{this.valueAsCode, this.suitAsCode};
}
}