Why can't I make second constructor using switch case statements that refer input to the first constructor? It shows error "Constructor call must be the first statement in a constructor using this". So it seems that I have to retype assignments from 1st constructor for every case statement in the second.
public class Card {
public static final String CLUBS = "Clubs";
public static final String DIAMONDS = "Diamonds";
public static final String HEARTS = "Hearts";
public static final String SPADES = "Spades";
public static final int ACE = 1;
public static final int JACK = 11;
public static final int QUEEN = 12;
public static final int KING = 13;
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
public Card(String rank, String suit) {
if (!isCorrectSuit(suit)) throw new IllegalArgumentException("incorrect suit");
switch(rank) {
case ACE: this(1, suit);
case JACK: this(11, suit);
case QUEEN: this(12, suit);
case KING : this(13, suit);
default: throw new IllegalArgumentException("incorrect rank");
}
}
private boolean isCorrectSuit(String suit) {
return (suit.equals(CLUBS) || suit.equals(DIAMONDS) || suit.equals(HEARTS) || suit.equals(SPADES));
}
private boolean isCorrectRank(int rank) {
return rank == 1 || rank == 11 || rank == 12 || rank == 13;
}
private int rank;
private String suit;
}