import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class VectorTesti extends Applet implements ActionListener
{
    private Vector v;
    private Label prompt;
    private TextField input;
    private Button addBtn, removeBtn, firstBtn, lastBtn, emptyBtn, containsBtn, locationBtn, trimBtn,
           statsBtn, displayBtn;
    Panel YaziPaneli;

    public void init()
    {
        setBackground(Color.lightGray);      
        YaziPaneli=new Panel();
        YaziPaneli.setFont(new Font("Serif",Font.BOLD,12));
        YaziPaneli.setLayout( new GridLayout(5,2) );
        v = new Vector(1);
        prompt= new Label("Bir String degiskeni giriniz ");
        input = new TextField(10);
        addBtn = new Button("Ekle");
        removeBtn = new Button("Cikar");
        firstBtn = new Button("Ilk");
        lastBtn = new Button("Son");
        emptyBtn = new Button("Bos mu ?");
        containsBtn = new Button("Listede varmi?");
        locationBtn = new Button("Adresi");
        trimBtn = new Button("Kes");
        statsBtn = new Button("Istatistikler");
        displayBtn = new Button("Listeyi Goster");
        add(prompt);
        add(input);
        addBtn.addActionListener(this);
        YaziPaneli.add(addBtn);
        removeBtn.addActionListener(this);
        YaziPaneli.add(removeBtn);
        firstBtn.addActionListener(this);
        YaziPaneli.add(firstBtn);
        lastBtn.addActionListener(this);
        YaziPaneli.add(lastBtn);
        emptyBtn.addActionListener(this);
        YaziPaneli.add(emptyBtn);
        containsBtn.addActionListener(this);
        YaziPaneli.add(containsBtn);
        locationBtn.addActionListener(this);
        YaziPaneli.add(locationBtn);
        trimBtn.addActionListener(this);
        YaziPaneli.add(trimBtn);
        statsBtn.addActionListener(this);
        YaziPaneli.add(statsBtn);
        displayBtn.addActionListener(this);
        YaziPaneli.add(displayBtn);
        add(YaziPaneli);
    }

    public void  actionPerformed(ActionEvent e)
    {
        if(e.getSource()==addBtn)
        {
            v.addElement(input.getText());
            showStatus("Listenin sonuna eklendi: "+input.getText());
        }
        else if(e.getSource()==removeBtn)
        {
            if(v.removeElement(input.getText()))
                showStatus("Silindi :"+input.getText());
              else
                showStatus(input.getText()+"vektor listesinden silindi");
        }
        else if(e.getSource() == firstBtn)
        {
            try{
                showStatus("Ilk eleman : "+v.firstElement());
            }
            catch(NoSuchElementException exception)
            {
                showStatus(exception.toString() );
            }
        }
        else if(e.getSource()==lastBtn)
        {
            try{
                showStatus("Son eleman : "+v.lastElement());
            }
            catch(NoSuchElementException exception)
            {
                showStatus(exception.toString() );
            }
        }
        else if(e.getSource() == emptyBtn)
        {
            showStatus(v.isEmpty()? "Vector bos ":"vector dolu ");
        }
        else if(e.getSource()==containsBtn)
        {
            String searchKey = input.getText();

            if(v.contains(searchKey) )
               showStatus("Vektor elemanlar¨ " +searchKey);
            else
               showStatus("Aranan eleman Vektorde bulunamadi "+searchKey);
        }
        else if(e.getSource()==locationBtn)
        {
            showStatus("Eleman "+v.indexOf(input.getText() )+" pozisyonunda bulundu");
        }
        else if(e.getSource()==trimBtn)
        {
            v.trimToSize();
            showStatus("Vector boyu eleman boyuna k¨salt¨ld¨");
        }
        else if(e.getSource()== statsBtn)
        {
            showStatus("Boyut = "+v.size()+"; Toplam kapasite = "+v.capacity());
        }
        else if(e.getSource()== displayBtn)
        {
            Enumeration enum=v.elements();
            StringBuffer buf = new StringBuffer();
            while(enum.hasMoreElements())
                 {
                 buf.append(enum.nextElement() );
                 buf.append(" ");
                 }
            showStatus(buf.toString() );
        }
        input.setText("");
    }
}
