-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
107 lines (84 loc) · 2.14 KB
/
Card.java
File metadata and controls
107 lines (84 loc) · 2.14 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.pslin.cards;
/**
* Represents a single playing card.
*
* @author plin
*/
public class Card implements Comparable<Card> {
public static final int MAX_VALUE = 14;
public static final int MIN_VALUE = 2;
private int value;
private Suit suit;
public enum Suit {
CLUB("\u2663", 0),
DIAMOND("\u2666", 1),
HEART("\u2665", 2),
SPADE("\u2660", 3);
private final String unicode;
private final int rank;
Suit(String unicode, int rank) {
this.unicode = unicode;
this.rank = rank;
}
public String getUnicode() {
return unicode;
}
public int getRank() {
return rank;
}
}
public Card(int value, Suit suit) {
this.value = value;
this.suit = suit;
}
public String getDisplayValue() {
if(value == MAX_VALUE) {
return "A";
}
if(value == 11) {
return "J";
}
if(value == 12) {
return "Q";
}
if(value == 13) {
return "K";
}
return String.valueOf(value);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Suit getSuit() {
return suit;
}
public void setSuit(Suit suit) {
this.suit = suit;
}
@Override
public int compareTo(Card o) {
if(value > o.getValue())
return 1;
if(value < o.getValue())
return -1;
return 0;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Card card = (Card) o;
if (value != card.value) return false;
if (suit != card.suit) return false;
return true;
}
@Override
public int hashCode() {
int result = value;
result = 31 * result + suit.hashCode();
return result;
}
}