
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;

import java.util.Collections;
import java.util.Scanner;

/**
 * Use various loops on lists.
 *
 * @author s9980739
 */
public class ListManipulations {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        List<String> myWords = new LinkedList<>();

        // add some values to it (on the end)
        myWords.add("ten");
        myWords.add("twenty");
        myWords.add("Thirty");
        myWords.add("forty");
        myWords.add("Fifty");
        myWords.add("twenty");
        System.out.println("Here is a list: " + myWords);
        pause();

        // add some values (not on the end)
        System.out.println("Adding 15 @ location 1, then 45 @ location 5.");
        myWords.add(1, "fifteen");
        myWords.add(5, "forty-five");
        System.out.println("Here is the list: " + myWords);
        pause();

        // sort the list
        System.out.println("Sorting the list.");
        Collections.sort(myWords, String.CASE_INSENSITIVE_ORDER);
        System.out.println("Here is the list: " + myWords);
        pause();

        // print and manipulate the list in multiple ways
        printListCounted(myWords);
        pause();

        // manipulation
        System.out.println("Beeping out the F-words.");
        beepFWords(myWords);
        printListCounted(myWords);
        pause();

        // print the List in various ways
        printListForEach(myWords);
        pause();
        printListWithIterator(myWords);
        pause();
    }

    /**
     * Print the elements of a List of String in four columns 15 characters
     * wide.
     *
     * @param theList the list to print
     */
    public static void printListCounted(List<String> theList) {
        System.out.println("Printing the list using a count-control loop.");
        for (int i = 0; i < theList.size(); ++i) {
            if (i % 4 == 0) {
                System.out.println();
            }
            System.out.printf("%15s", theList.get(i));
        }
        System.out.println();
    }

    /**
     * Remove all words starting with 'f' from the given list.
     *
     * @param theList the list to remove f-words from
     */
    public static void removeFWords(List<String> theList) {
        ListIterator<String> it = theList.listIterator();
        while (it.hasNext()) {
            String x = it.next();
            if (x.startsWith("f")) {
                it.remove();
            }
        }
    }

    /**
     * Add a warning before all words starting with 'f' in the given list.
     *
     * @param theList the list to remove f-words from
     */
    public static void addFWords(List<String> theList) {
        ListIterator<String> it = theList.listIterator();
        while (it.hasNext()) {
            String x = it.next();
            if (x.startsWith("f")) {
                it.add("f-WORD_ALERT!");
            }
        }
    }

    /**
     * Beep out all words starting with 'f' in the given list.
     *
     * @param theList the list to remove f-words from
     */
    public static void beepFWords(List<String> theList) {
        ListIterator<String> it = theList.listIterator();
        while (it.hasNext()) {
            String x = it.next();
            if (x.startsWith("f")) {
                it.set("BEEEEEP!");
            }

        }

    }

    /**
     * Print the elements of a List of String in four columns 15 characters
     * wide.
     *
     * @param theList the list to print
     */
    public static void printListForEach(List<String> theList) {
        System.out.println("Printing the list using a for-each loop.");
        // TO DO
        System.out.println("... for you to do! ...");
    }

    /**
     * Print the elements of a List of String in four columns 15 characters
     * wide.
     *
     * @param theList the list to print
     */
    public static void printListWithIterator(List<String> theList) {
        System.out.println("Printing the list using a list iterator.");
        ListIterator<String> it = theList.listIterator();
        final int ON_EACH_LINE = 4;
        int soFarThisLine = 0;
        while (it.hasNext()) {
            if (soFarThisLine == ON_EACH_LINE) {
                System.out.println();
                soFarThisLine = 0;
            }
            String x = it.next();
            System.out.printf("%15s", x);
            ++soFarThisLine;
        }
        System.out.println();
    }

    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();
    }

}
