import java.io.*;
import java.util.*;
//-----------------------------------------------------------------------------
/**
A
Card object represents a playing card, such as "Ace of Hearts". A card has a suit(Diamond, Heart, Spade or Club) and a face-value(2,...10, 11 = Jack, 12 = Queen, 13 = king). A card object is immutable; once instantiated, the values cannot change.
@author: Tommy Flewwelling
@version: 1.00
Creation Date: 2007/07/17
*/
public class Card{
// Private instance variables.
// Card's face-value and suit.
private int faceValue, suit;
/** String representation of card suits - position in array indicates corresponding integral value. */
public static final String[] FACE_VALUE = {"", "", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
public static final String[] SUIT = {"Spades", "Hearts", "Clubs", "Diamonds"};
//---------------------------------------------------------------------------
/** Create a new Card instance, in * "uninitialized" state (requiring face and value settings).
*/
public Card(){
this.faceValue = -1;
this.suit = -1;
}
//---------------------------------------------------------------------------
/**
Creates a new Card instance.
@param face The card's face-value.
@param suit The card's suit.
*/
public Card(int face, int suit){
validateFace(face);
validateSuit(suit);
this.faceValue = face;
this.suit = suit;
}
//---------------------------------------------------------------------------
/**
Validates the card's face-value.
@param f The card's face-value.
@throws IllegalArgumentException If invalid face-value is provided.
*/
private void validateFace(int f){
if (f < 2 || f > 14) throw new IllegalArgumentException (f + " is an invalid face value. Valid face values are 2(Two) through 14(Ace).");
}
//---------------------------------------------------------------------------
/**
Validates the card's suit.
@param s The card's suit.
@throws IllegalArgumentException If invalid suit is provided.
*/
private void validateSuit(int s){
if (s < 0 || s > 3) throw new IllegalArgumentException (s + " is an invalid suit value. Valid suit values are 0(Spades), 1(Hearts), 2(Clubs), 3(Diamonds).");
}
//---------------------------------------------------------------------------
/**
Returns the card's face-value.
@return faceValue The card's face-value.
*/
public int getFaceValue(){return this.faceValue;}
//---------------------------------------------------------------------------
/**
Updates the card's face-value.
@param fv The card's face-value.
*/
public void setFaceValue(int fv){
validateFace(fv);
this.faceValue = fv;
}
//---------------------------------------------------------------------------
/**
Returns the card's suit.
@return suit The card's suit.
*/
public int getSuit(){return this.suit;}
//---------------------------------------------------------------------------
/**
Updates the card's suit.
@param s The card's suit.
*/
public void setSuit(int s){
validateSuit(s);
this.suit = s;
}
//--------------------------- Overridden Object methods ---------------
/**
Returns a string representation of the card's faceValue and suit.
@return A string representation of card's faceValue and suit(i.e. Two of Hearts).
*/
public String toString ()
{return(faceValue == -1 || suit == -1) ? "Unitialized Card" : (FACE_VALUE[faceValue] + " of " + SUIT[suit]);}
//---------------------------------------------------------------------------
/**
Overrides the Equals method than compares suit then face-value.
@return A boolean to determine if the objects are equal.
*/
public boolean equals(Object o){
boolean eq = false;
if (o instanceof Card)
{
Card c = (Card) o;
eq = this.faceValue == c.faceValue && this.suit == c.suit;
}
return eq;
}
//---------------------------------------------------------------------------
/**
Overrides the hashCode() method.
@return The hash code of the lenght of the face-value array(FACE_VALUE.length)
multiplied by instance field suit and added to the face-value(faceValue) instance field.
*/
public int hashCode(){return FACE_VALUE.length * suit + faceValue;}
//---------------------------------------------------------------------------
}
//---------------------------------------------------------------------------