import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class StackSimulation extends Applet implements ActionListener
{
   private StackY theStack;
   private Label title;
   private TextField numberField, dataField;
   private Button push, pop, peek;
   private double data;
   private double return1, return2, return3;
   private boolean full, poped, peeked, empty, pushed;

   public void init()
   {
      title = new Label("Enter values here");
      add(title);

      dataField = new TextField(10);
      add(dataField);

      push = new Button("Push");
      push.addActionListener(this);
      add(push);

      pop = new Button("Pop");
      pop.addActionListener(this);
      add(pop);

      peek = new Button("Peek");
      peek.addActionListener(this);
      add(peek);

      theStack=new StackY(10);
   }

   public void actionPerformed(ActionEvent e)
   {
      if(e.getSource()==push)
      {      
         double data = Double.valueOf(dataField.getText()).doubleValue();

         if(!theStack.isFull())
         {
            theStack.push(data);
            dataField.setText("");
            dataField.requestFocus();
            empty = false;

         }
         else
            full = true;
            poped = false;
            peeked = false;
            pushed = true;

          return3 = theStack.justPush();
      }
      if(e.getSource()==pop)
      {
         if(!theStack.isEmpty())
         {
            return1 = theStack.pop();
            theStack.delete();
            empty = false;
         }
         else
            empty = true;
            full = false;
            poped = true;
            peeked = false;
            pushed = false;
      }
      if(e.getSource()==peek)
      {
         if(!theStack.isEmpty())
         {
            return2 = theStack.peek();
            peeked = true;
            poped = false;
            pushed = false;
         }
      }
      repaint();
   }

   public void paint(Graphics g)
   {
      if(full==true)
      {
         g.setColor(Color.red);
         g.drawString("Stack is Full",180,100);
         g.setColor(Color.black);
         theStack.display(g);
         pushed = false;
      }
      if(poped==true)
      {
         g.drawString("The value just popped is "+return1,180,120);
         theStack.display(g);
      }
      if(peeked==true)
      {
         g.drawString("The value on top of the stack currently is "+return1,180,120);
         theStack.display(g);
      }
      if(empty==true)
      {
         g.drawString("Stack is empty!",180,230);
         theStack.display(g);
      }

      if(pushed==true)
      {
        theStack.display(g);
        g.drawString("Your value has just been inserted into the stack "+return3,180,120);
      }
   }
}

class StackY
{
   private double[] stackArr;
   private int top;
   private int maxSize;
   private int startX=50,startY=250;
   private int boxHeight=20,boxWidth=40;
   private int y,height2=boxHeight*3/4;
   private int b;

   int y1=startY+boxHeight;

   public StackY(int s)
   {
      maxSize = s;
      stackArr = new double[maxSize];
      top=-1;
   }

   public void push(double j)
   {
      stackArr[++top]=j;
   }

   public void delete()
   {
      stackArr[top+1]=0;
   }

   public double pop()
   {
      return stackArr[top--];
   }

   public double peek()
   {
      return stackArr[top];
   }

   public double justPush()
   {
     return stackArr[top];
   }


   public boolean isEmpty()
   {
      return(top==-1);
   }

   public boolean isFull()
   {
      return(top==maxSize-1);
   }

   public void display(Graphics g)
   {       
      y = startY;
      for(int i=0;i<maxSize;i++)
      {
         g.setColor(Color.blue);
         g.fillRect(startX,y,boxWidth,boxHeight);
         g.setColor(Color.green);
         g.drawRect(startX,y,boxWidth,boxHeight);
         g.drawString(""+stackArr[i],startX,y+boxHeight*3/4);
         y = y-boxHeight;
      }

      if(top>-1)
      {
         b = (2*top)+1;
         g.drawLine(100,y1-(boxHeight*b/2),150,y1-(boxHeight*b/2));
         g.drawString("top",130,y1-(boxHeight*b/2));
      }
   }
}
