// StackTest.java
// Program to test java.util.Stack.
package chapter36;

import java.util.Stack;
import java.util.EmptyStackException;

public class StackTest
{
    public StackTest()
    {
        // Create an empty Stack of type Number
        Stack< Number > stack = new Stack< Number >();

        // create numbers to store in the stack
        Long longNumber = 12L;
        Integer intNumber = 34567;
        Float floatNumber = 1.0F;
        Double doubleNumber = 1234.5678;

        // Stack method push adds object to top of Stack
        stack.push( longNumber ); // push a long
        printStack( stack );
        stack.push( intNumber ); // push an int
        printStack( stack );
        stack.push( floatNumber ); // push a float
        printStack( stack );
        stack.push( doubleNumber ); // push a double
        printStack( stack );

        // remove items from stack
        Number removedObject = null;

        // Stack method pop removes element from top of Stack
        removedObject = stack.pop(); // use pop method
        System.out.printf( "%s popped\n", removedObject );
        printStack( stack );

        // Stack method pop removes element from top of Stack
        removedObject = stack.pop(); // use pop method
        System.out.printf( "%s popped\n", removedObject );
        printStack( stack );

    } // end StackTest constructor

    private void printStack( Stack< Number > stack )
    {
        // Stack method isEmpty returns true if Stack is empty
        if ( stack.isEmpty() )
            System.out.print( "stack is empty\n\n" ); // the stack is empty
        else  // stack is not empty
        {
            System.out.print( "stack contains: " );

            // iterate through the elements
            for ( Number number : stack )
                System.out.printf( "%s ", number );

            System.out.print( "(top) \n\n" ); // indicates top of the stack
        } // end else
    } // end method printStack

    public static void main( String args[] )
    {
        new StackTest();
    } // end main
} // end class StackTest

