/**
 *
 * @author Stephen Hole
 * @param <S>
 */
public class GenStack<S> {
    //This class implements a Stack data structure that can hold 10 elements
    int genStackSize = 10;
    int tos;
    S[] tempData = (S[]) new Object[genStackSize];
    //Stack class constructor
    GenStack () {
        //Intialise the top-of-stack
        tos = -1;
    }
    void push (S item) {
        if (tos == 9) {
            System.out.println("Stack is full!");
        } else {
            //increment tos and then use
            tempData[++tos] = item;
        }
    }
    public S pop() {
        if (tos < 0) {
            System.out.println("The Stack is empty");
            return null;
        } else {
            return tempData[tos--];
        }
    }
}

==============================================================================================================

/**
 *
 * @author Stephen Hole
 */
public class Product {
    private int productId;
    private String productName;
    private double productCost;
    
    Product(int productId, String productName, double productCost) {
        this.productId = productId;
        this.productName = productName;
        this.productCost = productCost;
    }
    
    public int getProductId() {
        return productId;
    }
    
    public void setProductId(int productId) {
        this.productId = productId;
    }
    
    public String getProductName() {
        return productName;
    }
    
    public void setProductName(String productName) {
        this.productName = productName;
    }
    
    public double getProductCost() {
        return productCost;
    }
    
    public void setProductCost(double productCost) {
        this.productCost = productCost;
    }
}

========================================================================================

/**
 *
 * @author soac-sh
 */
import java.util.Scanner;

public class StackGenericObjects {

    public static void main(String[] args) {
        //Using the GENERIC Stack for Integer and Strings
        GenStack myStack1 = new GenStack();
        GenStack myStack2 = new GenStack();
        GenStack myProductStack = new GenStack();
        Scanner scan = new Scanner(System.in);
        
        
        String[] myWords = {"One","Two","Three","Four","Five","Six",
            "Seven","Eight","Nine","Ten"};
        
        //push some Products on to the stack
        for (int i = 0; i < 2; i++) {
            System.out.print("Enter the Product ID: ");
            int prodId = scan.nextInt();
            System.out.println("Enter Product Name: ");
            String prodName = scan.next();
            System.out.println("Enter Product Cost: ");
            double prodCost = scan.nextDouble();
            System.out.println();
            System.out.println("Creating new Product Instance ");
            Product myProduct = new Product(prodId, prodName, prodCost);
            myProductStack.push(myProduct);
        }
        
        //Push some numbers onto the Stack
        for (int i = 0; i < 10; i++) myStack1.push(i);
        //Push some Strings onto the Stack
        for (int i = 0; i < 10; i++) myStack2.push(myWords[i]);
        
        //Pop those numbers back off the Stack
        System.out.println("Stack numbers in the GENERIC myStack1:");
        for (int i = 0; i < 10; i++) System.out.println(myStack1.pop());
        //Pop those Strings back off the stack
        System.out.println("Stack Strings in the GENERIC myStack2:");
        for (int i = 0; i < 10; i++) System.out.println(myStack2.pop());
        
        //Using a Integer based Stack
        Stack myStack3 = new Stack();
        Stack myStack4 = new Stack();
        
        //Push some numbers onto the Stack
        for (int i = 0; i < 10; i++) myStack3.push(i);
        for (int i = 0; i < 10; i++) myStack4.push(i);
        
        //Pop those numbers back off the Stack
        System.out.println("Stack numbers in myStack3:");
        for (int i = 0; i < 10; i++) System.out.println(myStack3.pop());
        
        System.out.println("Stack numbers in myStack4:");
        for (int i = 0; i < 10; i++) System.out.println(myStack4.pop());
        
        //Pop those Products from the stack
        System.out.println("Stack Products in myProductStack:");
        
        for (int i = 0; i < 2; i++) {
            
            Product myStackProduct = (Product)myProductStack.pop();
            int prodId = myStackProduct.getProductId();
            String prodName = myStackProduct.getProductName();
            double prodCost = myStackProduct.getProductCost();
            System.out.println("Product ID: " + prodId + " Product Name: " + prodName + " Product Cost: " + prodCost);
        }
    }
    
}
