解答例 - j2.lesson04.card.Card

package j2.lesson04.card;

/**
 * トランプカードを表すクラス。
 * @author arakawa
 * @version $Id: Card_java.rps,v 1.1 2006/03/06 12:56:15 java2005 Exp $
 */
public class Card {
    
    /** スペードを表す定数 (=0). */
    public static final int SPADE = 0;
    /** ダイヤを表す定数 (=1). */
    public static final int DIAMOND = 1;
    /** クラブを表す定数 (=2). */
    public static final int CLUB = 2;
    /** ハートを表す定数 (=3). */
    public static final int HEART = 3;
    
    /** マークを表す定数から文字列への変換表. */
    private static final String[] SUITE_STRING = {
        "スペード", "ダイヤ", "クラブ", "ハート",
    };
    
    /** ランクから文字列への変換表. */
    private static final String[] RANK_STRING = {
        "DUMMY",
        "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
        "J", "Q", "K",
    };
    
    /** トランプのマーク. */
    private final int suite;
    
    /** トランプのランク. */
    private final int rank;
    
    /**
     * カードを生成する。
     * @param suite このカードのマーク
     * @param rank ランク
     */
    public Card(int suite, int rank) {
        super();
        if (suite != SPADE && suite != DIAMOND && suite != CLUB && suite != HEART) {
            throw new IllegalArgumentException("suite (" + suite + ")"); //$NON-NLS-1$$NON-NLS-2$
        }
        if (rank < 1 || rank > 13) {
            throw new IllegalArgumentException("rank (" + rank + ")"); //$NON-NLS-1$$NON-NLS-2$
        }
        this.suite = suite;
        this.rank = rank;
    }
    
    /**
     * このカードのランクを表す文字列を取得する。
     * @return このカードのランクを表す文字列
     */
    public String getRank() {
        return RANK_STRING[this.rank];
    }
    
    /**
     * このカードのランクを 1 から 13 の値として取得する。
     * @return このカードのランク
     */
    public int getRankAsInt() {
        return this.rank;
    }
    
    /**
     * このカードのマークを表す文字列を取得する。
     * @return このカードのマークを表す文字列
     */
    public String getSuite() {
        return SUITE_STRING[this.suite];
    }
    
    /**
     * このカードのマークを 0 から 3 の値として取得する。
     * このクラスで宣言されている定数と照らし合わせることにより、マークを判定できる。
     * @return このカードのマークを表す数値
     * @see #SPADE
     * @see #DIAMOND
     * @see #CLUB
     * @see #HEART
     */
    public int getSuiteAsInt() {
        return this.suite;
    }
    
    /**
     * このカードがハートであるかどうか調べる。
     * @return ハートならば <code>true</code>
     */
    public boolean isHeart() {
        return this.suite == HEART;
    }
    
    /**
     * このカードがスペードであるかどうか調べる。
     * @return スペードならば <code>true</code>
     */
    public boolean isSpade() {
        return this.suite == SPADE;
    }
    
    /**
     * このカードがクラブであるかどうか調べる。
     * @return クラブならば <code>true</code>
     */
    public boolean isClub() {
        return this.suite == CLUB;
    }
    
    /**
     * このカードがダイヤモンドであるかどうか調べる。
     * @return ダイヤモンドならば <code>true</code>
     */
    public boolean isDiamond() {
        return this.suite == DIAMOND;
    }
    
    /**
     * このカードの文字列表現を取得する。
     * @return このカードの文字列表現
     */
    public String toString() {
        return this.getSuite() + "の" + this.getRank(); //$NON-NLS-1$
    }
}