// Name : Matthew Reeves // This is a program to test the methods in the stack class import java.util.Scanner; import ListPkg.Stack; import ListPkg.LinkedList; import MenuPkg.Menu; import ExceptionPkg.EmptyListException; import java.util.InputMismatchException; public class StackTest { //main method, includes the codes to send menu options to //Menu so menu can be displayed and inputs read/validated/accepted public static void main( String args[] ) { Scanner input=new Scanner(System.in); Stack stacker=new Stack("testStack"); String menuOpts[]={"Quit","Push an item on the stack","Pop an item off the stack","Return the top item from from the stack","Print the stack","Check the length of the stack","Check if the stack is empty","Clear the stack"}; Menu displayMenu=new Menu(menuOpts); System.out.println("\nThank you for using the stack test program!\n"); int choice; boolean continueLoop=true; //calls appropriate methods based on user's input choice do { System.out.println("\tPlease choose from the following options\n"); choice=displayMenu.runMenu(); double value; Double wrappedValue; try { switch (choice) { case 1: System.out.print("Please enter the value to push: "); value=input.nextDouble(); wrappedValue=value; stacker.push(wrappedValue); System.out.println("\nAdded to the stack!"); System.out.printf("\nThe current stack is:\n%s",stacker.toString()); break; case 2: if (stacker.isEmpty()) { System.out.println("Stack is empty - no value removed\n"); } else { wrappedValue=(Double)stacker.pop(); System.out.println("\nValue removed from stack: " + wrappedValue); System.out.printf("\nThe current stack is:\n%s",stacker.toString()); } break; case 3: if (stacker.isEmpty()) { System.out.println("Stack is empty\n"); } else { System.out.printf("The top value on the stack is: %s\n\n",stacker.top()); } break; case 4: if (stacker.isEmpty()) { System.out.println("Stack is empty\n"); } else { System.out.printf("The current stack is:\n%s",stacker.toString()); } break; case 5: System.out.printf("Number of elements currently in the stack: %d\n\n",stacker.lengthIs()); break; case 6: if (stacker.isEmpty()) { System.out.println("The stack is empty\n"); break; } else { System.out.println("The stack is NOT empty\n"); break; } case 7: stacker.Clear(); System.out.println("The stack has been cleared!\n"); break; case 0: continueLoop=false; break; } } catch (InputMismatchException inputMismatchException) { System.err.printf("\nException: %s\n",inputMismatchException); System.out.println("\nYou must enter a valid double value to add to the list, please try again.\n"); input.nextLine(); continue; } }while(continueLoop); } }