forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayingCard.java
More file actions
78 lines (65 loc) · 2.41 KB
/
PlayingCard.java
File metadata and controls
78 lines (65 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.Arrays;
public class PlayingCard {
public enum Rank {TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,
TEN, JACK, QUEEN, KING, ACE};
public enum Suit {DIAMONDS, CLUBS, HEARTS, SPADES};
private Rank rank;
private Suit suit;
public PlayingCard(String aCardDescription) {
this(extractRank(aCardDescription), extractSuit(aCardDescription));
}
private static Rank extractRank(String cardDescription)
throws IllegalArgumentException {
String[] parts = cardDescription.split(" ");
if (parts.length != 3) {
String msg="Illegal card description: " + cardDescription;
msg += "\nCard descriptions must have the form <rank> of <suit>";
msg += ", as in \"ace of spades\".";
throw new IllegalArgumentException(msg);
}
return Rank.valueOf(normalizeString(parts[0].trim()));
}
private static Suit extractSuit(String cardDescription) {
String[] parts = cardDescription.split(" ");
if (parts.length != 3) {
String msg="Illegal card description: " + cardDescription;
msg += "\nCard descriptions must have the form <rank> of <suit>";
msg += ", as in \"ace of spades\".";
throw new IllegalArgumentException(msg);
}
return Suit.valueOf(normalizeString(parts[2]));
}
private static String normalizeString(String someString) {
return someString.trim().toUpperCase();
}
public PlayingCard(Rank aRank, Suit aSuit) {
rank = aRank;
suit = aSuit;
}
public Rank getRank() {
return rank;
}
public Suit getSuit() {
return suit;
}
public String toString() {
return rank + " of " + suit;
}
public static void main(String[] args) {
System.out.print("Possible ranks are: ");
for (Rank rank: Rank.values()) {
System.out.print(rank + " ");
}
System.out.println();
System.out.print("Possible suits are: ");
for (Suit suit: Suit.values()) {
System.out.print(suit + " ");
}
System.out.println();
System.out.println("Example PlayingCards:");
PlayingCard c1 = new PlayingCard(Rank.TWO, Suit.DIAMONDS);
System.out.println(c1);
PlayingCard c2 = new PlayingCard("QuEen of heARts");
System.out.println(c2);
}
}