dsa

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

DriverDeq.java (3387B)


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