Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1.  
  2. public class Card implements Comparable<Card>{
  3.    
  4.     private Suit suit;
  5.     private Rank rank;
  6.    
  7.     public Card(Suit s, Rank r) {
  8.         this.suit = s;
  9.         this.rank = r;
  10.     }
  11.    
  12.     public Suit getSuit(){
  13.         return suit;
  14.     }
  15.    
  16.     public Rank getRank(){
  17.         return rank;
  18.     }
  19.    
  20.     public int compareTo(Card c) {
  21.         if (this.getRank() == c.getRank()) {
  22.             return this.getSuit().compareTo(c.getSuit());
  23.         }
  24.         else {
  25.             return this.getRank().compareTo(c.getRank());
  26.         }
  27.     }
  28.    
  29.     public String toString(Card c) {
  30.         return c.getRank().toString() + c.getSuit().toString();
  31.     }
  32.    
  33.     public boolean equals(Card c) {
  34.         if (this.getRank() == c.getRank()) {
  35.             if (this.getSuit() == c.getSuit()) {
  36.                 return true;
  37.             }
  38.         }
  39.         return false;
  40.     }
  41.    
  42.     public int hashCode() {
  43.         return hashCodeSuit(this) + hashCodeRank(this);
  44.     }
  45.    
  46.     private int hashCodeSuit(Card c) {
  47.         int result = 0;
  48.         if( c.getSuit() == Suit.DIAMOND) {result = 1;}
  49.         if( c.getSuit() == Suit.CLUB) {result = 2;}
  50.         if( c.getSuit() == Suit.HEART) {result = 3;}
  51.         if( c.getSuit() == Suit.SPADE) {result = 4;}
  52.         return result;
  53.     }
  54.    
  55.     private int hashCodeRank(Card c) {
  56.         int result = 0;
  57.         if( c.getRank() == Rank.TWO) {result = 20;}
  58.         if( c.getRank() == Rank.THREE) {result = 30;}
  59.         if( c.getRank() == Rank.FOUR) {result = 40;}
  60.         if( c.getRank() == Rank.FIVE) {result = 50;}
  61.         if( c.getRank() == Rank.SIX) {result = 60;}
  62.         if( c.getRank() == Rank.SEVEN) {result = 70;}
  63.         if( c.getRank() == Rank.EIGHT) {result = 80;}
  64.         if( c.getRank() == Rank.NINE) {result = 90;}
  65.         if( c.getRank() == Rank.TEN) {result = 100;}
  66.         if( c.getRank() == Rank.JACK) {result = 110;}
  67.         if( c.getRank() == Rank.QUEEN) {result = 120;}
  68.         if( c.getRank() == Rank.KING) {result = 130;}
  69.         if( c.getRank() == Rank.ACE) {result = 140;}
  70.        
  71.         return result;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement