/** * Data structure representing a playing card, including its numeric value, * and pointers to the previous and next cards in the deck order. * * Copyright (c) 2007 William R. Fraser * All rights reserved. * * @author William R. Fraser * @version 2007.03.29 */ public class Card { private int datum; // value of the card private Card next; // pointer to next card in the deck private Card prev; // pointer to the previous card in the deck /** * Default constructor. */ public Card() { datum = 0; next = null; prev = null; } /** * Make a new Card instance. * * @param datum Value of the card * @param prev Pointer to next card in the deck * @param next Pointer to the previous card in the deck */ public Card(int datum, Card prev, Card next) { this.datum = datum; this.prev = prev; this.next = next; } /** * Returns a String of the form "previous -> this -> next" with the values * of the cards. */ public String toString() { String prevDat, nextDat; if (prev != null) { prevDat = ((Integer) prev.getDatum()).toString(); } else { prevDat = "null"; } if (next != null) { nextDat = ((Integer) next.getDatum()).toString(); } else { nextDat = "null"; } return prevDat + " -> " + datum + " -> " + nextDat; } /** * Set the pointer to the next card * @param next */ public void setNext(Card next) { this.next = next; } /** * Set the pointer to the previous card * @param prev */ public void setPrev(Card prev) { this.prev = prev; } /** * Return the next card in the deck. * @return */ public Card getNext() { return next; } /** * Return the previous card in the deck. * @return */ public Card getPrev() { return prev; } /** * Return the value of the card. * @return */ public int getDatum() { return datum; } /** * Set the value of the card, only if it is unset (i.e. is 0). * * Throws IllegalArgumentException if the card already has a value. * * @param i */ public void setDatum(int i) { if (datum != 0) { throw new IllegalArgumentException("Card value can only be set " + "once!"); } datum = i; } }