import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * Create and use some ArrayList objects (and maybe a LinkedList, too)
 *
 * @author Mark Young (A00000000)
 */
public class ListBasics {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // create some list objects
        List<String> myWords = new LinkedList<>();
        List<Student> myClass = new ArrayList<>();
        List<Integer> myInts = new ArrayList<>();
        List<Double> myDbls = new ArrayList<>();
        List<Double> myOtherDbls = new LinkedList<>();

        // add some values to them
        myWords.add("ten");
        myWords.add("twenty");
        myWords.add("thirty");
        myWords.add("forty");
        myWords.add("25");
        myClass.add(new Student("Bob"));
        myClass.add(new Student("Carol"));
        myInts.add(10);
        myInts.add(20);
        myInts.add(30);
        myDbls.add(10.0);
        myDbls.add(20.0);
        myDbls.add(1, 15.0);        // add at location 1 (2nd position)
        myOtherDbls.add(10.0);
        myOtherDbls.add(20.0);
        myOtherDbls.add(0, 15.0);   // add at location 0 (1st position)

        // CANNOT add items of the wrong type
        // myWords.add(100);
        // myClass.add("Ted");
        // myInts.add(10.5);
        // myDbls.add("thirty");
        // myOtherDbls.add(new Student("Alice"));

        // print them out
        System.out.println("Here are some lists I've created:");
        System.out.println("\tmyWords == " + myWords);
        System.out.println("\tmyClass == " + myClass);
        System.out.println("\tmyInts == " + myInts);
        System.out.println("\tmyDbls == " + myDbls);
        System.out.println("\tmyOtherDbls == " + myOtherDbls);
        pause();
        
        // look at some of the elements
        System.out.println("Let's get some values from them:");
        String word0 = myWords.get(0);
        System.out.println("\tThe 1st word in " + myWords
                + " is " + word0);
        System.out.println("\tThe 3rd word in " + myWords
                + " is " + myWords.get(3 - 1));
        pause();

        // how long is that list?
        System.out.println("What size are these lists?");
        System.out.println("\tThere are " + myWords.size()
                + " elements in " + myWords);
        System.out.println("\tThere are " + myInts.size()
                + " elements in " + myInts);
        pause();

        // add some values (not on the end)
        System.out.println("Adding 15 at position 1 of " + myWords);
        myWords.add(1, "fifteen");
        System.out.println("\tmyWords == " + myWords);
        System.out.println("\tThe 3rd word in " + myWords
                + " is now " + myWords.get(3 - 1));
        pause();

        // check if it has certain values on it
        System.out.println("Checking the contents of these lists:");
        if (myWords.contains("ten")) {
            System.out.println("\t" + myWords + " contains a ten");
        } else {
            System.out.println("\t" + myWords + " DOESN'T contains a ten");
        }
        if (myWords.contains("hundred")) {
            System.out.println("\t" + myWords + " contains a hundred");
        } else {
            System.out.println("\t" + myWords + " DOESN'T contains a hundred");
        }

        // check where those values are
        System.out.println("\tThe word \"twenty\" appears at position "
                + myWords.indexOf("twenty") + " in " + myWords);
        System.out.println("\tThe word \"hundred\" appears at position "
                + myWords.indexOf("hundred") + " in " + myWords);
        pause();

        // remove by position
        System.out.println("Let's remove some items:");
        System.out.print("\t" + myWords + ".remove(0) makes myWords == ");
        myWords.remove(0);
        System.out.println(myWords);
        System.out.print("\t" + myWords + ".remove(3) makes myWords == ");
        myWords.remove(3);
        System.out.println(myWords);

        // remove by value
        myWords.remove("twenty");
        System.out.println("\tAfter removing \"twenty\" "
                + "myWords is " + myWords);
        
        // remove by position and value in a list of Integers
        System.out.println("\tRemoving the item at position 1 "
                + "of " + myInts + " leaves ");
        myInts.remove(1);
        System.out.println("\t\tmyInts == " + myInts);
        System.out.println("\tRemoving the the value 10 "
                + "from " + myInts + " leaves ");
        myInts.remove(Integer.valueOf(10));
        System.out.println("\t\tmyInts == " + myInts);

        // remove by position and value in a list of Doubles
        System.out.println("\tRemoving the item at position 1 "
                + "of " + myOtherDbls + " leaves ");
        myOtherDbls.remove(1);
        System.out.println("\t\tmyOtherDbls == " + myOtherDbls);
        System.out.println("\tRemoving the the value 20.0 "
                + "from " + myOtherDbls + " leaves ");
        myOtherDbls.remove(20.0);
        System.out.println("\t\tmyOtherDbls == " + myOtherDbls);
        System.out.println("\tRemoving the the value 3927.5 "
                + "from " + myOtherDbls + " leaves ");
        myOtherDbls.remove(3927.5);
        System.out.println("\t\tmyOtherDbls == " + myOtherDbls);
        pause();

        // check if a list is empty
        System.out.println("Let's check if lists are empty:");
        if (myWords.isEmpty()) {
            System.out.println("\t" + myWords + " is empty!");
        } else {
            System.out.println("\t" + myWords + " is NOT empty!");
        }
        
        // make a list empty (clear it out)
        myWords.clear();
        if (myWords.isEmpty()) {
            System.out.println("\t" + myWords + " is empty!");
        } else {
            System.out.println("\t" + myWords + " is NOT empty!");
        }
        pause();
    }

    private static final Scanner kbd = new Scanner(System.in);

    /**
     * Waits for the user to press pause (after prompting them to do so).
     */
    private static void pause() {
        System.out.println();
        System.out.print("...press enter...");
        kbd.nextLine();
        System.out.println();
    }
}
