import java.util.ArrayList;

class Pack {
        // Shuffle the cards in the pack.
    public void shuffle(){
        // Repeatedly swap cards around in the pack.
        final ArrayList pack = getPack();
        final int cardsInDeck = pack.size();
        final int shuffleTimes = 10*cardsInDeck;
        for(int i = 0; i < shuffleTimes; i++){
            // Generate the indices of two cards to move.
            int first = (int) (Math.random()*cardsInDeck),
                second = (int) (Math.random()*cardsInDeck);
            Object card = pack.get(first);
            // Replace the second with the first ...
            card = pack.set(second,card);
            // ... and vice versa.
            pack.set(first,card);
        }
    }

        // Return the card on the top of the pack, or null
        // if there are no more.
    public Card nextCard(){
        ArrayList pack = getPack();
        final int numCards = pack.size();
        if(numCards > 0){
            // Remove the last.
            return (Card) pack.remove(numCards-1);
        }
        else{
            return null;
        }
    }

    private ArrayList getPack(){
        return pack;
    }

    // The cards in the pack.
    private final ArrayList pack = new ArrayList();
    {
        // Construct a pack of 52 cards, 4 copies of values 1..13.
        final int numDuplicates = 4;
        final int low = 1, high = 13;
        for(int i = 0; i < numDuplicates; i++){
            for(int value = low; value <= high; value++){
                pack.add(new Card(value));
            }
        }
    }
}
