package game.model; /** Abstract base class that should be used for cards. It automatically handles card * ID's and provides appropriate implementations of {@link #equals(Object)}, {@link #hashCode()} * and {@link #toString()}. It also provides a convenient {@link #printBoth(Game, String)} method * to print a text message on the UI of both players. */ public abstract class AbstractCard implements Card { private static int cardCounter = 0; private CardType type; private String id, name, description; /** Constructor. Besides handling the passed in parameters, this constructor also takes care * of assigning the card a unique ID. * @param t The type of card. * @param nm The name of the card. Multiple instances of the same card are allowed to have the * same name. * @param desc The card description. */ protected AbstractCard(CardType t, String nm, String desc) { type = t; name = nm; id = name + ++cardCounter; description = desc; } /** Convenience method to print a text message on both user interfaces. * @param game The game state. * @param message The message to print. */ protected static void printBoth(Game game, String message) { new MultiUserInterface(game.getPlayer().getUI(), game.getOtherPlayer().getUI()).print(message); } public String getDescription() { return description; } public String getID() { return id; } public String getName() { return name; } public CardType getType() { return type; } /** The equals method only takes the card ID into account, so two cards are assumed * to be equal if they are of the same class and their ID's match. */ @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!obj.getClass().equals(getClass())) { return false; } return getID().equals(((Card) obj).getID()); } @Override public int hashCode() { return getID().hashCode(); } /** This method simply returns the name of the card. */ @Override public String toString() { return getName(); } }