// an inner nested class is used as an iterator
// it has its own state.
import java.io.*;
class MyException extends Exception {

	static int errorCount;
	static { errorCount = 0;}
	MyException (String msg) {
		super ("User's Exception " + msg +  " occured");
		errorCount++;
	}

}

interface Iterator {

	public boolean isEmpty();
	public void reset();
	public int current();
	public boolean forward(); // false if stuck at max
	public boolean reverse(); // false if stuck at 0 
}

class BoundedStack {

	private class StackIterator implements Iterator { 
						// private inner class
						// inner class has access
						// to enclosing class. It can
						// be instantiated only from 
						// inside of the enclosing class
						// they can be static classes

		int currentindex;
		StackIterator() {currentindex=0;}
		public boolean isEmpty() {
			if (top==0) return true;
			else return false;
		}
		public int current() { return state[currentindex];}
		public boolean forward () {
			if (currentindex==MAX-1) return false;
			currentindex++; return true;
		}
		public boolean reverse() {
			if (currentindex==0) return false;
			currentindex--;
			return true;
		}
		public void reset() {currentindex=0;}

	}
	public Iterator getIterator() {return new StackIterator();}

	private int state[];
	private int top;
	private int MAX;
	private int errorCount;
	BoundedStack (int size) {
		state = new int[size];
		top=0; // top points to current empty location
		MAX = size;
		errorCount = 0;
	}
	int pop() throws MyException {
		// try here is useless -- why?
		if (top==0) 
		  throw (new MyException ("stack underflow in pop"));
		top = top-1;
		return (state[top]);
	}
	void push(int element) throws MyException {
		if (top==MAX) 
		   throw (new MyException ("stack overflow in push"));
		state[top]=element;
		top = top+1;
	}

}

class NestedClassDemo {

	public static void displayMenu () {
		System.out.println ("Menu: create stack-0, push--1, pop--2 dump--3");
	}

	public static void main (String args[]) {

		BoundedStack b = new BoundedStack (1);//default stack of size 1
		Iterator iter = b.getIterator();
		InputStreamReader r = new InputStreamReader (System.in);
		BufferedReader br = new BufferedReader (r);
		int x, element;
	
		while (true) {
  		  try {
		    displayMenu();
		    x = Integer.parseInt (br.readLine());
		    switch (x) {
			case 0: System.out.println("size?");
		  		x = Integer.parseInt (br.readLine());
				b = new BoundedStack (x);
				iter = b.getIterator();
				break;
			case 1: System.out.println("element?");
		  		element = Integer.parseInt (br.readLine());
				b.push(element);
				break;
			case 2: System.out.println ("popped:" + b.pop());
				break;
			case 3: if (iter.isEmpty()) break;
				System.out.print("stack dump:");
				do { 
					element = iter.current();
					System.out.print(element + " ");
				} while (iter.forward());
				System.out.println();
				break;

		    }

		  }
		  catch (MyException e) {
			System.out.println("Exception!! " + e.getMessage());
			if (e instanceof MyException)
				System.out.println ("Error count:" +
					((MyException)e).errorCount);
			else System.out.println ("e was not an instance of MyException");
		  }
		  catch (IOException e) {
			System.out.println ("IO Exception!");
		  }
		  catch (NumberFormatException e) {
			System.out.println (" parseInt exception!");
		  }
		  finally {

			System.out.println ("This prints for all cases");

		  }
		}
	}
}





