/*
 * MenuItems.java
 *
 * Created on 26 Ноябрь 2009 г., 14:27
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fptrtest;

/**
 *
 * @author V.Kravtsov
 */

import java.util.Vector;

public class MenuItems implements MenuItemInterface
{
    
    private final String text;
    private final ConsoleUtil console;
    private final Vector list = new Vector();
    
    /** Creates a new instance of MenuItems */
    public MenuItems(String text, ConsoleUtil console) {
        this.text = text;
        this.console = console;
    }
    
    public String getText(){
        return text;
    }
    
    public void add(MenuItemInterface item){
        list.add(item);
    }
    
    public void execute() 
    throws Exception
    {
        while (true)
        {
            console.printSeparator();
            for (int i=0;i<list.size();i++)
            {
                MenuItemInterface item = (MenuItemInterface)list.get(i);
                String line = String.valueOf(i+1) + ") " + item.getText();
                System.out.println(line);
            }
            String line = String.valueOf(list.size()+1) + ") Main menu";
            System.out.println(line);
            
            int number = console.readMenuItem();
            if (number == list.size()+1) return;
            if ((number > 0)&&(number <= list.size()))
            {
                MenuItemInterface item = (MenuItemInterface)list.get(number-1);
                item.execute();
            }
        }
    }
    
    
}
