Driver.java (4437B)
1 package lab2; 2 3 /* 4 * Purpose: Data Structure and Algorithms Lab 2 Problem 1 5 * Status: Complete and thoroughly tested 6 * Last update: 01/31/18 7 * Submitted: 02/01/18 8 * Comment: test suite and sample run attached 9 * @author: John 'Johnny' Kubach 10 * @version: 2018.01.31 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 /* Comment out one of the two following lines to toggle between 22 * Array or ArrayList Implementation 23 */ 24 ListArrayBasedPlus listArrayPlus = new ListArrayBasedPlus(); 25 // ListArrayListBasedPlus listArrayPlus = new ListArrayListBasedPlus(); 26 27 ui.listOptions(); 28 29 do { 30 userSelection = ui.menuSelection(); 31 System.out.println(userSelection); 32 switch (userSelection) { 33 case 1: 34 ui.addItem(listArrayPlus); 35 break; 36 case 2: 37 ui.removeItem(listArrayPlus); 38 break; 39 case 3: 40 ui.getItem(listArrayPlus); 41 break; 42 case 4: 43 listArrayPlus.removeAll(); 44 break; 45 case 5: 46 ui.printList(listArrayPlus); 47 break; 48 case 6: 49 listArrayPlus.reverse(); 50 break; 51 case 7: 52 ui.exitProgram(); 53 } 54 } while (userSelection != 7); 55 } 56 57 public void addItem(ListArrayBasedPlus listArrayPlus) { 58 int index; 59 Object item; 60 System.out.println("Your are now entering an item"); 61 System.out.print(" Enter item: "); 62 item = in.nextLine(); 63 System.out.println(item); 64 System.out.print(" Enter position to insert item in: "); 65 index = Integer.parseInt(in.nextLine().trim()); 66 System.out.println(index); 67 if (index > listArrayPlus.size()) { 68 System.out.println("Position is out of bounds!"); 69 } 70 else { 71 listArrayPlus.add(index, item); 72 System.out.println("Item " + item + " inserted in position " + index + " in the list."); 73 } 74 } 75 76 public void removeItem(ListArrayBasedPlus listArrayPlus) { 77 int index; 78 System.out.print("Enter position to remove item from: "); 79 index = Integer.parseInt(in.nextLine().trim()); 80 System.out.println(index); 81 82 if (index >= listArrayPlus.size()) 83 { 84 System.out.println("Index is out of range!"); 85 } 86 else { 87 System.out.println("Item " + listArrayPlus.get(index) + " removed from position " + index + " in the list"); 88 listArrayPlus.remove(index); 89 } 90 } 91 92 93 public void getItem(ListArrayBasedPlus listArrayPlus) { 94 int index; 95 System.out.print("Enter position to retrieve from list "); 96 index = Integer.parseInt(in.nextLine().trim()); 97 System.out.println(index); 98 99 if (index >= listArrayPlus.size()) { 100 System.out.println("Index is out of range!"); 101 } 102 else { 103 System.out.println("Item " + listArrayPlus.get(index) + " retrieved from position " + index + " in the list"); 104 } 105 } 106 107 public void printList(ListArrayBasedPlus listArrayPlus) { 108 if (listArrayPlus.size() == 0) 109 { 110 System.out.println("List is empty"); 111 } 112 else { 113 System.out.println(listArrayPlus.toString()); 114 } 115 } 116 117 public int menuSelection() { 118 System.out.print("Make your menu selection now: "); 119 return Integer.parseInt(in.nextLine()); 120 } 121 122 public void listOptions(){ 123 System.out.println("Select from the following menu:"); 124 System.out.println(" 1. Insert item to list"); 125 System.out.println(" 2. Remove item from list"); 126 System.out.println(" 3. Get item from list"); 127 System.out.println(" 4. Clear list"); 128 System.out.println(" 5. Print size and contents of list"); 129 System.out.println(" 6. Reverse list"); 130 System.out.println(" 7. Exit program"); 131 } 132 133 void exitProgram(){ 134 System.out.println("Exiting program...Good Bye"); 135 System.exit(0); 136 } 137 138 }