dsa

Data Structures & Algorithms - Spring 2018
Log | Files | Refs | README

DriverQueue.java (2453B)


      1 package lab6;
      2 
      3 import java.util.Scanner;
      4 
      5 public class DriverQueue {
      6 
      7     Scanner in = new Scanner(System.in);
      8     public static void main(String[]args) {
      9         int userSelection;
     10         DriverQueue ui = new DriverQueue();
     11         Queue<Object> myQueue = new Queue<>();
     12 
     13         ui.listOptions();
     14 
     15         do {
     16             userSelection = ui.menuSelection();
     17             System.out.println(userSelection);
     18             switch (userSelection) {
     19                 case 1:
     20                     ui.insertItem(myQueue);
     21                     break;
     22                 case 2:
     23                     ui.removeItem(myQueue);
     24                     break;
     25                 case 3:
     26                     ui.peek(myQueue);
     27                     break;
     28                 case 4:
     29                     ui.clear(myQueue);
     30                     break;
     31                 case 5:
     32                     ui.display(myQueue);
     33                     break;
     34                 case 6:
     35                     ui.exitProgram();
     36                     break;
     37             }
     38         } while (userSelection != 0);
     39 
     40     }
     41 
     42     public void insertItem(Queue myQueue) {
     43         Object newItem;
     44         System.out.print("Enter item: ");
     45         newItem = in.nextLine();
     46         myQueue.enqueue(newItem);
     47         System.out.println("Item " + newItem + " has been added to the back");
     48 
     49     }
     50 
     51     public void removeItem(Queue myQueue) {
     52         System.out.println("Item " + myQueue.dequeue() + " has been removed from the front");
     53     }
     54 
     55     public void peek(Queue myQueue) {
     56         System.out.println(myQueue.peek());
     57     }
     58 
     59     public void clear(Queue myQueue) {
     60         myQueue.dequeueAll();
     61     }
     62 
     63     public void display(Queue myQueue) {
     64         System.out.println(myQueue.toString());
     65     }
     66 
     67     public int menuSelection() {
     68         System.out.print("Make your menu selection now: ");
     69         return Integer.parseInt(in.nextLine().trim());
     70     }
     71 
     72     public void listOptions() {
     73         System.out.println("Select from the following menu:");
     74         System.out.println("    1. Insert item in back of Queue");
     75         System.out.println("    2. Remove item from front of Queue");
     76         System.out.println("    3. Display front of Queue");
     77         System.out.println("    4. Clear Queue");
     78         System.out.println("    5. Display contents of Queue");
     79         System.out.println("    6. Exit");
     80     }
     81 
     82     void exitProgram(){
     83         System.out.println("Exiting program...Good Bye");
     84         System.exit(0);
     85     }
     86 }
     87