Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Card implements Comparable<Card>{
- private Suit suit;
- private Rank rank;
- public Card(Suit s, Rank r) {
- this.suit = s;
- this.rank = r;
- }
- public Suit getSuit(){
- return suit;
- }
- public Rank getRank(){
- return rank;
- }
- public int compareTo(Card c) {
- if (this.getRank() == c.getRank()) {
- return this.getSuit().compareTo(c.getSuit());
- }
- else {
- return this.getRank().compareTo(c.getRank());
- }
- }
- public String toString(Card c) {
- return c.getRank().toString() + c.getSuit().toString();
- }
- public boolean equals(Card c) {
- if (this.getRank() == c.getRank()) {
- if (this.getSuit() == c.getSuit()) {
- return true;
- }
- }
- return false;
- }
- public int hashCode() {
- return hashCodeSuit(this) + hashCodeRank(this);
- }
- private int hashCodeSuit(Card c) {
- int result = 0;
- if( c.getSuit() == Suit.DIAMOND) {result = 1;}
- if( c.getSuit() == Suit.CLUB) {result = 2;}
- if( c.getSuit() == Suit.HEART) {result = 3;}
- if( c.getSuit() == Suit.SPADE) {result = 4;}
- return result;
- }
- private int hashCodeRank(Card c) {
- int result = 0;
- if( c.getRank() == Rank.TWO) {result = 20;}
- if( c.getRank() == Rank.THREE) {result = 30;}
- if( c.getRank() == Rank.FOUR) {result = 40;}
- if( c.getRank() == Rank.FIVE) {result = 50;}
- if( c.getRank() == Rank.SIX) {result = 60;}
- if( c.getRank() == Rank.SEVEN) {result = 70;}
- if( c.getRank() == Rank.EIGHT) {result = 80;}
- if( c.getRank() == Rank.NINE) {result = 90;}
- if( c.getRank() == Rank.TEN) {result = 100;}
- if( c.getRank() == Rank.JACK) {result = 110;}
- if( c.getRank() == Rank.QUEEN) {result = 120;}
- if( c.getRank() == Rank.KING) {result = 130;}
- if( c.getRank() == Rank.ACE) {result = 140;}
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement