import java.awt.*;
import java.awt.event.*;

public class CityDeck extends Panel implements ActionListener {
    public CityDeck(){
        // Set the layout manager.
        setLayout(new CardLayout());

        // Give the labels an appropriately sized font.
        final int fontSize = 20;
        setFont(new Font("SansSerif",Font.BOLD,fontSize));

        // Add the city names to the deck.
        for(int city = 0; city < cityNames.length; city++){
            // Centre the label's text.
            Label label = new Label(cityNames[city],Label.CENTER);
            // Use the label's text as a constraint, which is
            // needed by the layout manager.
            add(label,label.getText());
        }
    }

    public void actionPerformed(ActionEvent e){
        String command = e.getActionCommand();
        CardLayout layout = (CardLayout) getLayout();

        if(command.equals("First")){
            layout.first(this);
        }
        else if(command.equals("Next")){
            layout.next(this);
        }
        else if(command.equals("Last")){
            layout.last(this);
        }
        else if(command.equals("Random")){
            int randomIndex = (int) (Math.random() * cityNames.length);
            String constraint = cityNames[randomIndex];
            layout.show(this,constraint);
        }
        else if(command.equals("Quit")){
            System.exit(0);
        }
        else{
            // Catch the error if extra buttons have been added.
            System.err.println("Unknown command "+command);
        }
        repaint();
    }

    // The names of the cities.
    private static final String cityNames[] = {
        "Berlin", "Brussels", "London", "Paris", "Rome", "Vienna",
    };
}
