/*
Copyright (c) 2013, Matthew Malensek
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright holder or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
*/
package jcrib.cards;

public class Card implements Comparable<Card> {
    private Suit suit;
    private Face face;
    private int value;

    public Card(String cardName) {
        if (cardName.length() > 3) {
            throw new IllegalArgumentException("Malformed Card identifier");
        }

        int len = cardName.length();

        String id = cardName.substring(0, len - 1).toUpperCase();
        String suit = cardName.substring(len - 1, len).toUpperCase();

        try {
            value = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            value = 10;
            face = Face.fromString(id);
        }

        this.suit = Suit.fromString(suit);
        // TODO verify correctness here
    }

    public Card(int value, Suit suit) {
        this.suit = suit;
        this.face = null;
        this.value = value;
    }

    public Card(Face face, Suit suit) {
        this.suit = suit;
        this.face = face;
        this.value = 10;
    }

    public Suit getSuit() {
        return suit;
    }

    public Face getFace() {
        return face;
    }

    public int getValue() {
        return value;
    }

    public int getOrdinal() {
        if (isFaceCard()) {
            return value + face.ordinal() + 1;
        } else {
            return value;
        }
    }

    public boolean isFaceCard() {
        return !(face == null);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null) {
            return false;
        }

        if (getClass() != obj.getClass()) {
            return false;
        }

        Card other = (Card) obj;

        if (this.suit != other.suit) {
            return false;
        }

        return compareTo((Card) obj) == 0;
    }

    @Override
    public int compareTo(Card other) {
        return (this.getOrdinal() - other.getOrdinal()) * 10
            + this.getSuit().ordinal() - other.getSuit().ordinal();
    }

    @Override
    public String toString() {
        if (face == null) {
            return value + suit.toString();
        } else {
            return face.toString() + suit.toString();
        }
    }
}

@@ -2692,32 +2692,347 @@
 = null);%0A    %7D%0A%0A
+    @Override%0A    public int hashCode() %7B%0A        final int prime = 31;%0A        int result = 1;%0A        result = prime * result + ((face == null) ? 0 : face.hashCode());%0A        result = prime * result + ((suit == null) ? 0 : suit.hashCode());%0A        result = prime * result + value;%0A        return result;%0A    %7D%0A%0A
     @Override%0A  
@@ -3123,33 +3123,32 @@
 true;%0A        %7D%0A
-%0A
         if (obj 
@@ -3186,33 +3186,32 @@
 alse;%0A        %7D%0A
-%0A
         if (getC
@@ -3266,33 +3266,32 @@
 alse;%0A        %7D%0A
-%0A
         Card oth
@@ -3307,17 +3307,16 @@
 d) obj;%0A
-%0A
         
@@ -3323,113 +3323,227 @@
 if (
-this.suit != other.suit) %7B%0A            return false;%0A        %7D%0A%0A        return compareTo((Card) obj) == 0
+face != other.face) %7B%0A            return false;%0A        %7D%0A        if (suit != other.suit) %7B%0A            return false;%0A        %7D%0A        if (value != other.value) %7B%0A            return false;%0A        %7D%0A        return true
 ;%0A  

Implement hashCode()