Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. /*
  2.  *
  3.  */
  4. package userInterface;
  5.  
  6. import java.util.Scanner;
  7.  
  8. import eg.edu.alexu.csd.datastructure.stack.cs20.Stack;
  9.  
  10. // TODO: Auto-generated Javadoc
  11. /**
  12.  * The Class StackUI.
  13.  */
  14. public class StackUI {
  15.  
  16.     /** The inp. */
  17.     static Scanner inp;
  18.  
  19.     /** The obj. */
  20.     static Stack obj = new Stack();
  21.  
  22.     /**
  23.      * The main method.
  24.      *
  25.      * @param args
  26.      *            the arguments
  27.      */
  28.     public static void main(String[] args) {
  29.         while (true) {
  30.             printInterface();
  31.             inp = new Scanner(System.in);
  32.             int n = inp.nextInt();
  33.  
  34.             if (n == 1)
  35.                 pushFunc();
  36.             else if (n == 2)
  37.                 popFunc();
  38.             else if (n == 3)
  39.                 peekFunc();
  40.             else if (n == 4)
  41.                 getSizeFunc();
  42.             else if (n == 5)
  43.                 emptyFunc();
  44.             else if (n == 6) {
  45.                 System.out.println("Program Terminated.");
  46.                 break;
  47.             } else
  48.                 System.out.println("Please enter a digit from 1-6.");
  49.         }
  50.  
  51.     }
  52.  
  53.     /**
  54.      * Pop func.
  55.      */
  56.     private static void popFunc() {
  57.         if (obj.isEmpty())
  58.             System.out.println("Stack is currently empty");
  59.         else {
  60.             System.out.print("The element popped is: ");
  61.             System.out.println(obj.pop());
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * Peek func.
  67.      */
  68.     private static void peekFunc() {
  69.         if (obj.isEmpty())
  70.             System.out.println("Stack is currently empty");
  71.         else {
  72.             System.out.print("The top element is: ");
  73.             System.out.println(obj.peek());
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * Empty func.
  79.      */
  80.     private static void emptyFunc() {
  81.         if (obj.isEmpty())
  82.             System.out.println("Yes, Stack is currently empty.");
  83.         else
  84.             System.out.println("No, Stack is not empty.");
  85.     }
  86.  
  87.     /**
  88.      * Gets the size func.
  89.      *
  90.      * @return the size func
  91.      */
  92.     private static void getSizeFunc() {
  93.         System.out.print("Stack size = ");
  94.         System.out.println(obj.size());
  95.     }
  96.  
  97.     /**
  98.      * Push func.
  99.      */
  100.     private static void pushFunc() {
  101.         System.out.print("Please enter an element to be pushed into the stack: ");
  102.         inp = new Scanner(System.in);
  103.         Object newObj = inp.nextInt();
  104.         obj.push(newObj);
  105.         System.out.println("Element pushed into the stack.");
  106.     }
  107.  
  108.     /**
  109.      * Prints the interface.
  110.      */
  111.     public static void printInterface() {
  112.         System.out.println("Please choose an action");
  113.         System.out.println("-----------------------");
  114.         System.out.println("1- Push");
  115.         System.out.println("2- Pop");
  116.         System.out.println("3- Peek");
  117.         System.out.println("4- Get Size");
  118.         System.out.println("5- Check if empty");
  119.         System.out.println("6- Exit Program");
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement