dsa

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

Driver.java (5175B)


      1 package lab8;
      2 
      3 /*
      4  * Purpose: Data Structure and Algorithms Lab 8 Problem 1
      5  * Status: Complete and thoroughly tested
      6  * Last update: 04/09/18
      7  * Submitted:  04/09/18
      8  * Comment: test suite and sample run attached
      9  * @author: John Kubach
     10  * @version: 2018.04.09
     11  */
     12 
     13 import java.util.Scanner;
     14 
     15 public class Driver {
     16     Scanner in = new Scanner(System.in);
     17     public static void main(String[] args) {
     18         int userSelection;
     19         Driver ui = new Driver();
     20 
     21         ListArrayBasedPlus<Integer> listArrayPlus = new ListArrayBasedPlus<>();
     22 
     23         ui.listOptions();
     24 
     25         do {
     26             userSelection = ui.menuSelection();
     27             System.out.println(userSelection);
     28             switch (userSelection) {
     29                 case 1:
     30                     ui.insertItem(listArrayPlus);
     31                     break;
     32                 case 2:
     33                     ui.removeItem(listArrayPlus);
     34                     break;
     35                 case 3:
     36                     ui.getItem(listArrayPlus);
     37                     break;
     38                 case 4:
     39                     ui.getSearchInput(listArrayPlus);
     40                     break;
     41                 case 5:
     42                     listArrayPlus.removeAll();
     43                     break;
     44                 case 6:
     45                     ui.printList(listArrayPlus);
     46                     break;
     47                 case 7:
     48                     ui.exitProgram();
     49             }
     50         } while (userSelection != 7);
     51     }
     52 
     53 
     54     public void insertItem(ListArrayBasedPlus<Integer> listArrayPlus) {
     55         int searchKey;
     56         int index;
     57          System.out.println("Your are now entering an item");
     58         System.out.print("  Enter item: ");
     59         searchKey = Integer.parseInt(in.nextLine());
     60         System.out.println(searchKey);
     61 
     62 
     63         index = searchList(listArrayPlus, searchKey);
     64         if (index >= 0) {
     65             listArrayPlus.add(index, searchKey);
     66             System.out.println(searchKey + " added at index " + index);
     67         }
     68         else if (index < 0) {
     69             System.out.println("Item already exists at index " + (-index - 1));
     70         }
     71     }
     72 
     73     /*
     74      * Using method III from class.
     75      */
     76 
     77     public void getSearchInput(ListArrayBasedPlus listArrayPlus) {
     78         int searchKey;
     79         int result;
     80         System.out.print("Enter key: ");
     81         searchKey = Integer.parseInt(in.nextLine());
     82         System.out.println(searchKey);
     83 
     84         result = searchList(listArrayPlus, searchKey);
     85         if (result >= 0) {
     86             System.out.println(searchKey + " not found, stopped at index " + result);
     87         }
     88         else if (result < 0) {
     89             System.out.println("Key found at index " + (-result - 1));
     90         }
     91 
     92     }
     93 
     94     public int searchList(ListArrayBasedPlus<Integer> listArrayPlus, int searchKey) {
     95         int index;
     96         for (index = 0; index < listArrayPlus.size(); index++) {
     97 
     98             System.out.println(listArrayPlus.get(index));
     99             if (searchKey == listArrayPlus.get(index)) {
    100                 return (-index - 1);
    101             }
    102         }
    103         return index;
    104     }
    105 
    106 
    107     public void removeItem(ListArrayBasedPlus listArrayPlus) {
    108         int index;
    109         System.out.print("Enter position to remove item from: ");
    110         index = Integer.parseInt(in.nextLine().trim());
    111         System.out.println(index);
    112 
    113         if (index >= listArrayPlus.size())
    114         {
    115             System.out.println("Index is out of range!");
    116         }
    117         else {
    118             System.out.println("Item " + listArrayPlus.get(index) + " removed from position " + index + " in the list");
    119             listArrayPlus.remove(index);
    120         }
    121     }
    122 
    123 
    124     public void getItem(ListArrayBasedPlus listArrayPlus) {
    125         int index;
    126         System.out.print("Enter position to retrieve from list ");
    127         index = Integer.parseInt(in.nextLine().trim());
    128         System.out.println(index);
    129 
    130         if (index >= listArrayPlus.size()) {
    131             System.out.println("Index is out of range!");
    132         }
    133         else {
    134             System.out.println("Item " + listArrayPlus.get(index) + " retrieved from position " + index + " in the list");
    135         }
    136     }
    137 
    138 
    139 
    140     public void printList(ListArrayBasedPlus listArrayPlus) {
    141         if (listArrayPlus.size() == 0)
    142         {
    143             System.out.println("List is empty");
    144         }
    145         else {
    146             System.out.println(listArrayPlus.toString());
    147         }
    148     }
    149 
    150     public int menuSelection() {
    151         System.out.print("Make your menu selection now: ");
    152         return Integer.parseInt(in.nextLine());
    153     }
    154 
    155     public void listOptions(){
    156         System.out.println("Select from the following menu:");
    157         System.out.println("    1. Insert item to list");
    158         System.out.println("    2. Remove item from list");
    159         System.out.println("    3. Get item from list");
    160         System.out.println("    4. Search for a specified item in the list");
    161         System.out.println("    5. Clear list");
    162         System.out.println("    6. Print size and contents of list");
    163         System.out.println("    7. Exit program");
    164     }
    165 
    166     void exitProgram(){
    167         System.out.println("Exiting program...Good Bye");
    168         System.exit(0);
    169     }
    170 }