Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- */
- package userInterface;
- import java.util.Scanner;
- import eg.edu.alexu.csd.datastructure.stack.cs20.Stack;
- // TODO: Auto-generated Javadoc
- /**
- * The Class StackUI.
- */
- public class StackUI {
- /** The inp. */
- static Scanner inp;
- /** The obj. */
- static Stack obj = new Stack();
- /**
- * The main method.
- *
- * @param args
- * the arguments
- */
- public static void main(String[] args) {
- while (true) {
- printInterface();
- inp = new Scanner(System.in);
- int n = inp.nextInt();
- if (n == 1)
- pushFunc();
- else if (n == 2)
- popFunc();
- else if (n == 3)
- peekFunc();
- else if (n == 4)
- getSizeFunc();
- else if (n == 5)
- emptyFunc();
- else if (n == 6) {
- System.out.println("Program Terminated.");
- break;
- } else
- System.out.println("Please enter a digit from 1-6.");
- }
- }
- /**
- * Pop func.
- */
- private static void popFunc() {
- if (obj.isEmpty())
- System.out.println("Stack is currently empty");
- else {
- System.out.print("The element popped is: ");
- System.out.println(obj.pop());
- }
- }
- /**
- * Peek func.
- */
- private static void peekFunc() {
- if (obj.isEmpty())
- System.out.println("Stack is currently empty");
- else {
- System.out.print("The top element is: ");
- System.out.println(obj.peek());
- }
- }
- /**
- * Empty func.
- */
- private static void emptyFunc() {
- if (obj.isEmpty())
- System.out.println("Yes, Stack is currently empty.");
- else
- System.out.println("No, Stack is not empty.");
- }
- /**
- * Gets the size func.
- *
- * @return the size func
- */
- private static void getSizeFunc() {
- System.out.print("Stack size = ");
- System.out.println(obj.size());
- }
- /**
- * Push func.
- */
- private static void pushFunc() {
- System.out.print("Please enter an element to be pushed into the stack: ");
- inp = new Scanner(System.in);
- Object newObj = inp.nextInt();
- obj.push(newObj);
- System.out.println("Element pushed into the stack.");
- }
- /**
- * Prints the interface.
- */
- public static void printInterface() {
- System.out.println("Please choose an action");
- System.out.println("-----------------------");
- System.out.println("1- Push");
- System.out.println("2- Pop");
- System.out.println("3- Peek");
- System.out.println("4- Get Size");
- System.out.println("5- Check if empty");
- System.out.println("6- Exit Program");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement