Topic 8.1 Design the data structures for a generic deck of cards. Explain how you would subclass the data structures to implement blackjack.
public enum Suit {
Club (0),
Diamond (1),
Heart (2),
Spade (3);
private int value;
private Suit(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static Suit getSuitFromValue(int value) {
switch (value) {
case 0:
return Suit.Club;
case 1:
return Suit.Diamond;
case 2:
return Suit.Heart;
case 3:
return Suit.Spade;
default:
return null;
}
}
}
public abstract class Card {
private boolean available = true;
protected int value;
protected Suit suit;
public Card(int value, Suit suit) {
this.value = value;
this.suit = suit;
}
public abstract int value();
public Suit suit() {
return suit;
}
public boolean isAvailable() {
return available;
}
public void markUnavailable() {
available = false;
}
public void markAvailable() {
available = true;
}
public void print() {
String[] values = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
System.out.print(values[value - 1]);
switch (suit) {
case Club:
System.out.print("Club");
break;
case Heart:
System.out.print("Heart");
break;
case Diamond:
System.out.print("Diamond");
break;
case Spade:
System.out.print("Spade");
break;
}
System.out.print(" ");
}
}