/**
   A deck implemented by means of an array.

   @author Franck van Breugel
*/
public class ArrayDeck implements Deck
{
    private Card[] content;
    private int size;

    /**
       Create a deck of cards.
    */
    public ArrayDeck()
    {
	final int SIZE = 52;
	this.content = new Card[SIZE];
	int i = 0;
	for (int suit = Card.HEARTS; suit <= Card.SPADES; suit++)
	{
	    for (int rank = 2; rank <= Card.ACE; rank++)
	    {
		this.content[i] = new Card(suit, rank);
		i++;
	    }
        }
	this.size = SIZE;
    }

    /**
       Create a copy of the given deck.
 
       @param copied the deck to be copied.
       @pre. copied != null
    */
    public ArrayDeck(ArrayDeck copied)
    {
	this.content = new Card[copied.content.length];
	System.arraycopy(copied.content, 0, this.content, 0, copied.content.length);
	this.size = copied.size;
    }

    public int size()
    {
	return this.size;
    }
    
    public boolean isEmpty()
    {
	return this.size() == 0;
    }
    
    public Card top()
    {
	return this.content[0];
    }
    
    public Card bottom()
    {
	return this.content[this.size - 1];
    }
    
    public void removeTop()
    {
	for (int i = 0; i < size - 1; i++)
	{
	    this.content[i] = this.content[i + 1];
	}
	this.size--;
	this.content[this.size] = null;
    }

    public void removeBottom()
    {
	this.size--;
	this.content[this.size] = null;
    }

    public boolean equals(Object object)
    {
	if (object == null || this.getClass() != object.getClass())
	{
	    return false;
	}
	ArrayDeck other = (ArrayDeck) object;
	boolean equal = this.size == other.size;
	for (int i = 0; i < this.size && equal; i++)
	{
	    equal = this.content[i].equals(other.content[i]);
	}
	return equal;
    }

    public String toString()
    {
	String representation ="[";
	String separator = "";
	for (int i = 0; i < this.size; i++)
	{
	    representation += separator + this.content[i];
	    separator = ", ";
	}
	return representation + "]";
    }
}
