dsa

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

Driver.java (4854B)


      1 package lab4;
      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         ListCDLSBased myList = new ListCDLSBased();
     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                     myList.removeAll();
     31                     break;
     32                 case 5:
     33                     ui.printList(myList);
     34                     break;
     35                 case 6:
     36                     myList = ui.reverseList(myList);
     37                     break;
     38                 case 7:
     39                     ui.exit();
     40                     break;
     41                 default:
     42                     System.out.println("Enter a valid selection.");
     43             }
     44         }while(menuSelection != 8);
     45     }
     46 
     47     private int menuSelect() {
     48         System.out.print("Make your menu selection now: ");
     49         return Integer.parseInt(in.nextLine());
     50     }
     51 
     52     private void addObject(ListCDLSBased myList) {
     53         Object userInput;
     54         int userIndex;
     55         System.out.println("You are now inserting an item into the list");
     56         System.out.print("Enter item: ");
     57         userInput = in.nextLine();
     58         System.out.println(userInput);
     59         System.out.print("Enter position to insert item in: ");
     60         userIndex = Integer.parseInt(in.nextLine());
     61         System.out.println(userIndex);
     62         if (userIndex <= myList.size()) {
     63             myList.add(userIndex, userInput);
     64             System.out.println("Item " + userInput + " inserted in position " + userIndex + " in the list");
     65         }
     66         else {
     67             System.out.println("Position specified is out of range!");
     68         }
     69     }
     70 
     71     private void removeObject(ListCDLSBased myList) {
     72         int userIndex;
     73         System.out.print("Enter position to remove item from ");
     74         userIndex = Integer.parseInt(in.nextLine());
     75         System.out.println(userIndex);
     76         if (userIndex <= myList.size()) {
     77             System.out.println("Item " + myList.get(userIndex) + " removed from position " + userIndex + " in the list");
     78             myList.remove(userIndex);
     79         }
     80         else {
     81             System.out.println("Position specified is out of range!");
     82         }
     83     }
     84 
     85     private void getObject(ListCDLSBased myList) {
     86         int userIndex;
     87         System.out.print("Enter position to retrieve item from: ");
     88         userIndex = Integer.parseInt(in.nextLine());
     89         System.out.println(userIndex);
     90         if (userIndex < myList.size()) {
     91             System.out.println("Item " + myList.get(userIndex) + " retrieved from position " + userIndex + " in the list");
     92         }
     93         else {
     94             System.out.println("Position specified is out of range!");
     95         }
     96     }
     97 
     98     private void printList(ListCDLSBased myList) {
     99         if (myList.size() > 0) {
    100             System.out.println(myList.toString());
    101         }
    102         else {
    103             System.out.println("List is empty.");
    104         }
    105     }
    106 
    107     private ListCDLSBased reverseList(ListCDLSBased myList) {
    108         ListCDLSBased tempList = new ListCDLSBased();
    109         int listSize = myList.size();
    110         if (listSize > 0) {
    111             for (int i = 0; i < listSize;  i++) {
    112                 tempList.add(i, myList.get(listSize - 1 - i));
    113                 System.out.println(tempList.get(i));
    114             }
    115 
    116               //  myList = tempList;
    117 
    118             System.out.println("List has been reversed");
    119             System.out.print("Here is the content: ");
    120             for (int i = 0; i < listSize; i++) {
    121                 System.out.print(tempList.get(i) + " ");
    122             }
    123             System.out.println();
    124         }
    125         else {
    126             System.out.println("List is empty.. nothing to reverse!");
    127         }
    128         return tempList;
    129     }
    130 
    131     private void welcomeMessage() {
    132         System.out.println("Select from the following menu: ");
    133         System.out.println("    1. Insert item to list");
    134         System.out.println("    2. Remove item from list");
    135         System.out.println("    3. Get item from list");
    136         System.out.println("    4. Clear list");
    137         System.out.println("    5. Print size and content of list");
    138         System.out.println("    6. Reverse List");
    139         System.out.println("    7. Exit Program");
    140     }
    141 
    142     private void exit() {
    143         System.out.println("Exiting program...Good Bye");
    144         System.exit(0);
    145     }
    146 }