/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exemplopilha;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 42071420684
 */
public class Main {

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    IPilha<Integer> pilha;

    // Usando Pilha com delegadacao
    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>");
    System.out.println(">>> Pilha com delegacao >>>");
    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>");
    pilha = new PilhaDelegada<Integer>();
    operaPilha(pilha);

    // Usando Pilha implementada por heranca
    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
    System.out.println(">>> Pilha com heranca >>>");
    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
    pilha = new PilhaHerdada<Integer>();
    operaPilha(pilha);

  }

  public static void operaPilha(IPilha<Integer> p) {
    IPilhaIterator it;
    try {
      p.push(100);
      
      p.push(5);
      iteraPilha(p);
      p.push(2);
      iteraPilha(p);

      int val1 = p.pop();
      System.out.println("valor retirado da pilha:" + val1);
      iteraPilha(p);

      int val2 = p.pop();
      System.out.println("valor retirado da pilha:" + val2);
      if (!p.vazia()) {
        iteraPilha(p);
      }

      int result = val1 + val2;
      p.push(result);
      iteraPilha(p);

    } catch (PilhaException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public static void iteraPilha(IPilha<Integer> p) {
    IPilhaIterator<Integer> it = p.iterador();

    System.out.print("--> Forward iteration sobre pilha:");
    it.begin();
    do {
      System.out.print(it.next()+ " ");
    } while (it.hasNext());
    System.out.println();

    System.out.print("--> Backward iteration sobre pilha:");
    it.last();
    do {
      System.out.print(it.previous()+ " ");
    } while (it.hasPrevious());
    System.out.println();
    System.out.println("-----------------------------------");
  }
}
