dsa

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

Driver2.java (4102B)


      1 package lab8;
      2 
      3 import java.util.Scanner;
      4 public class Driver2 {
      5 
      6     Scanner in = new Scanner(System.in);
      7     public static void main(String[] args) {
      8         int userSelection;
      9         Driver2 ui = new Driver2();
     10 
     11         AscendinglyOrderedStringList orderedList = new AscendinglyOrderedStringList();
     12         ui.listOptions();
     13 
     14         do {
     15             userSelection = ui.menuSelection();
     16             System.out.println(userSelection);
     17             switch (userSelection) {
     18                 case 1:
     19                     ui.insertItem(orderedList);
     20                     break;
     21                 case 2:
     22                     ui.removeItem(orderedList);
     23                     break;
     24                 case 3:
     25                     ui.getItem(orderedList);
     26                     break;
     27                 case 4:
     28                     ui.searchList(orderedList);
     29                     break;
     30                 case 5:
     31                     orderedList.clear();
     32                     break;
     33                 case 6:
     34                     ui.printList(orderedList);
     35                     break;
     36                 case 7:
     37                     ui.exitProgram();
     38             }
     39         } while (userSelection != 7);
     40     }
     41 
     42 
     43 
     44     public void insertItem(AscendinglyOrderedStringList orderedList) {
     45         String searchKey;
     46         System.out.println("Your are now entering an item");
     47         System.out.print("  Enter item: ");
     48         searchKey = in.nextLine();
     49         System.out.println(searchKey);
     50         orderedList.add(searchKey);
     51     }
     52 
     53 
     54 
     55     public void removeItem(AscendinglyOrderedStringList orderedList) {
     56         int index;
     57         System.out.print("Enter position to remove item from: ");
     58         index = Integer.parseInt(in.nextLine().trim());
     59         System.out.println(index);
     60 
     61         if (index >= orderedList.size())
     62         {
     63             System.out.println("Index is out of range!");
     64         }
     65         else {
     66             System.out.println("Item " + orderedList.get(index) + " removed from position " + index + " in the list");
     67             orderedList.remove(index);
     68         }
     69     }
     70 
     71 
     72     public void getItem(AscendinglyOrderedStringList orderedList) {
     73         int index;
     74         System.out.print("Enter position to retrieve from list ");
     75         index = Integer.parseInt(in.nextLine().trim());
     76         System.out.println(index);
     77 
     78         if (index >= orderedList.size()) {
     79             System.out.println("Index is out of range!");
     80         }
     81         else {
     82             System.out.println("Item " + orderedList.get(index) + " retrieved from position " + index + " in the list");
     83         }
     84     }
     85 
     86     public void searchList(AscendinglyOrderedStringList orderedList) {
     87         String key;
     88         int index;
     89         System.out.print("Enter key to search: ");
     90         key = in.nextLine().trim();
     91 
     92         index = orderedList.search(key);
     93         if (index > 0) {
     94             System.out.println("Key " + key + " found at index " + (index-1));
     95         }
     96         else{
     97 
     98             System.out.println("Key could not be found, stopped at index " + (-index));
     99             }
    100         }
    101 
    102 
    103     public void printList(AscendinglyOrderedStringList orderedList) {
    104         if (orderedList.size() == 0)
    105         {
    106             System.out.println("List is empty");
    107         }
    108         else {
    109             System.out.println(orderedList.toString());
    110         }
    111     }
    112 
    113     public int menuSelection() {
    114         System.out.print("Make your menu selection now: ");
    115         return Integer.parseInt(in.nextLine());
    116     }
    117 
    118     public void listOptions(){
    119         System.out.println("Select from the following menu:");
    120         System.out.println("    1. Insert item to list");
    121         System.out.println("    2. Remove item from list");
    122         System.out.println("    3. Get item from list");
    123         System.out.println("    4. Search for a specified item in the list");
    124         System.out.println("    5. Clear list");
    125         System.out.println("    6. Print size and contents of list");
    126         System.out.println("    7. Exit program");
    127     }
    128 
    129     void exitProgram(){
    130         System.out.println("Exiting program...Good Bye");
    131         System.exit(0);
    132     }
    133 
    134 }