dsa

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

Driver.java (7794B)


      1 package lab3;
      2 
      3 import java.util.Scanner;
      4 
      5 public class Driver {
      6     Scanner in = new Scanner(System.in);
      7     public static void main(String[] args) {
      8         int menuSelection;
      9 
     10         Driver ui = new Driver();
     11         MyListReferenceBased myList = new MyListReferenceBased();
     12 
     13         ui.welcomeMessage();
     14 
     15         do {
     16             menuSelection = ui.menuSelect();
     17             System.out.println(menuSelection);
     18 
     19             switch (menuSelection) {
     20                 case 1:
     21                     ui.addObject(myList);
     22                     break;
     23                 case 2:
     24                     ui.removeObject(myList);
     25                     break;
     26                 case 3:
     27                     ui.getObject(myList);
     28                     break;
     29                 case 4:
     30                     ui.removeAll(myList);
     31                     break;
     32                 case 5:
     33                     ui.printList(myList);
     34                     break;
     35                 case 6:
     36                     ui.deleteLargestObject(myList);
     37                     break;
     38                 case 7:
     39                     myList = ui.reverseList(myList);
     40                     break;
     41                 case 8:
     42                     ui.exit();
     43                     break;
     44                 case 9:
     45                     ui.deleteSmallestObject(myList);
     46                     break;
     47                 case 10:
     48                     ui.compareStrings(myList);
     49                     break;
     50                 default:
     51                     System.out.println("Enter a valid selection.");
     52             }
     53         }while(menuSelection != 8);
     54     }
     55 
     56     private int menuSelect() {
     57         System.out.print("Make your menu selection now: ");
     58         return Integer.parseInt(in.nextLine());
     59     }
     60 
     61     private void addObject(MyListReferenceBased myList) {
     62         Object userInput;
     63         int userIndex;
     64         System.out.println("You are now inserting an item into the list");
     65         System.out.print("Enter item: ");
     66         userInput = in.nextLine();
     67         System.out.println(userInput);
     68         System.out.print("Enter position to insert item in: ");
     69         userIndex = Integer.parseInt(in.nextLine());
     70         System.out.println(userIndex);
     71         if (userIndex <= myList.size()) {
     72             myList.add(userIndex, userInput);
     73             System.out.println("Item " + userInput + " inserted in position " + userIndex + " in the list");
     74         }
     75         else {
     76             System.out.println("Position specified is out of range!");
     77         }
     78     }
     79 
     80     private void removeObject(MyListReferenceBased myList) {
     81         int userIndex;
     82         System.out.print("Enter position to remove item from ");
     83         userIndex = Integer.parseInt(in.nextLine());
     84         System.out.println(userIndex);
     85         if (userIndex <= myList.size()) {
     86             System.out.println("Item " + myList.get(userIndex) + " removed from position " + userIndex + " in the list");
     87             myList.remove(userIndex);
     88         }
     89         else {
     90             System.out.println("Position specified is out of range!");
     91         }
     92     }
     93 
     94     private void getObject(MyListReferenceBased myList) {
     95         int userIndex;
     96         System.out.print("Enter position to retrieve item from: ");
     97         userIndex = Integer.parseInt(in.nextLine());
     98         System.out.println(userIndex);
     99         if (userIndex <= myList.size()) {
    100             System.out.println("Item " + myList.get(userIndex) + " retrieved from position " + userIndex + " in the list");
    101         }
    102         else {
    103             System.out.println("Position specified is out of range!");
    104         }
    105     }
    106 
    107     private void removeAll(MyListReferenceBased myList) {
    108         myList.removeAll();
    109     }
    110 
    111     private void printList(MyListReferenceBased myList) {
    112         if (myList.size() > 0) {
    113             System.out.println(myList.toString());
    114         }
    115         else {
    116             System.out.println("List is empty.");
    117         }
    118     }
    119 
    120     private void deleteLargestObject(MyListReferenceBased myList) {
    121         int largestIndex = 0;
    122         String largestString = "";
    123         String currentString;
    124         if (myList.size() > 0) {
    125             for (int i = 0; i < myList.size(); i++) {
    126                 currentString = myList.get(i).toString();
    127                 if (currentString.compareTo(largestString) > 0 && largestString.compareTo(currentString) < 0 || largestString.equals("")) {
    128                     largestIndex = i;
    129                     largestString = myList.get(largestIndex).toString();
    130                 }
    131             }
    132             System.out.println("Largest item " + largestString + " deleted.");
    133             myList.remove(largestIndex);
    134         }
    135         else {
    136             System.out.println("List empty, nothing to delete!");
    137         }
    138     }
    139 
    140     private void deleteSmallestObject(MyListReferenceBased myList) {
    141         int smallestIndex = 0;
    142         String smallestString = "";
    143         String currentString;
    144         if (myList.size() >0) {
    145             for (int i = 0; i < myList.size(); i++) {
    146                 currentString = myList.get(i).toString();
    147                 if (currentString.compareTo(smallestString) < 0 && smallestString.compareTo(currentString) > 0 || smallestString.equals("")) {
    148                     smallestIndex = i;
    149                     smallestString = myList.get(smallestIndex).toString();
    150                 }
    151             }
    152             System.out.println("Smallest item " + smallestString + " deleted.");
    153             myList.remove(smallestIndex);
    154         }
    155         else {
    156             System.out.println("List empty, nothing to delete!");
    157         }
    158     }
    159 
    160     private void compareStrings(MyListReferenceBased myList) {
    161         int userIndex, difference;
    162         String str1, str2;
    163         System.out.print("Enter index of first string: ");
    164         userIndex = Integer.parseInt(in.nextLine());
    165         System.out.println(userIndex);
    166         str1 = myList.get(userIndex).toString();
    167         System.out.print("Enter index of second string: ");
    168         userIndex = Integer.parseInt(in.nextLine());
    169         System.out.println(userIndex);
    170         str2 = myList.get(userIndex).toString();
    171         difference = str1.compareTo(str2);
    172         System.out.println("The difference between these two strings is " + difference);
    173     }
    174 
    175     private MyListReferenceBased reverseList(MyListReferenceBased myList) {
    176         MyListReferenceBased tempList = new MyListReferenceBased();
    177         int listSize = myList.size();
    178         if (listSize > 0) {
    179             for (int i = 0; i < listSize;  i++) {
    180                 tempList.add(i, myList.get(listSize - 1 - i));
    181             }
    182 
    183             //  myList = tempList;
    184 
    185             System.out.println("List has been reversed");
    186             System.out.print("Here is the content: ");
    187             for (int i = 0; i < listSize; i++) {
    188                 System.out.print(tempList.get(i) + " ");
    189             }
    190             System.out.println();
    191         }
    192         else {
    193             System.out.println("List is empty.. nothing to reverse!");
    194         }
    195         return tempList;
    196 
    197     }
    198 
    199     private void welcomeMessage() {
    200         System.out.println("Select from the following menu: ");
    201         System.out.println("    1. Insert item to list");
    202         System.out.println("    2. Remove item from list");
    203         System.out.println("    3. Get item from list");
    204         System.out.println("    4. Clear list");
    205         System.out.println("    5. Print size and content of list");
    206         System.out.println("    6. Delete largest item in list");
    207         System.out.println("    7. Reverse list");
    208         System.out.println("    8. Exit program");
    209         System.out.println("    9. Delete smallest item in list (EXTRA)");
    210         System.out.println("    10. Compare two strings (EXTRA)");
    211     }
    212 
    213     private void exit() {
    214         System.out.println("Exiting program...Good Bye");
    215         System.exit(0);
    216     }
    217 }