
import java.awt.Font;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author Bill Kraynek
 */
public class CountWordsWithWordClass {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        Scanner fileScanner = new Scanner(new File("src/CountWordsWithWordClass.java"));
        fileScanner.useDelimiter("[^A-Za-z]+");
        ArrayList<WordClass> words = new ArrayList<WordClass>();
        int maxWordLength = 0;
        while (fileScanner.hasNext()) {
            WordClass word = new WordClass(fileScanner.next());
            int index = words.indexOf(word);
            if (index == -1) {
                words.add(word);
                if (maxWordLength < word.getWord().length()) {
                    maxWordLength = word.getWord().length();
                }
            } else {
                words.get(index).incrementCount();
            }// end if
        }// end while
        display(words, maxWordLength);
        Collections.sort(words);
        display(words, maxWordLength);
        Collections.sort(words, new CountWordsComparator());
        display(words, maxWordLength);
        Comparator<WordClass> compare = new CountWordsComparator();
    }

    static void display(ArrayList<WordClass> words, int maxWordLength) {
        String blanks = "";
        for (int i = 0; i < maxWordLength + 1; i++) {
            blanks += " ";
        }
        String out = "  Word" + blanks.substring(8) + "Count\n\n";
        for (int i = 0; i < words.size(); i++) {
            int wordLength = words.get(i).getWord().length();
            String countBlanks = "";
            int count = words.get(i).getCount();
            if (count < 100 ) countBlanks = " ";
            if (count < 10 ) countBlanks = "  " ;
            out += words.get(i).getWord() + blanks.substring(wordLength) + countBlanks + count + "\n";
        }// end for
        JTextArea outArea = new JTextArea(out, 40, 30);
        outArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 15));
        JOptionPane.showMessageDialog(null, new JScrollPane(outArea));
    }
}

class WordClass implements Comparable<WordClass> {

    private String word;
    private int count;

    public WordClass(String word) {
        this.word = word;
        count = 1;
    }

    public String getWord() {
        return word;
    }

    public int getCount() {
        return count;
    }

    public void incrementCount() {
        count++;
    }

    public boolean equals(Object rhs) {
        if (rhs == null) {
            return false;
        }
        if (!this.getClass().equals(rhs.getClass())) {
            return false;
        }
        WordClass that = (WordClass) rhs;
        return this.getWord().equals(that.getWord());
    }

    public int hashCode() {
        return word.hashCode();
    }

    public int compareTo(WordClass rhs) {
        return this.getWord().compareTo(rhs.getWord());
    }
}// end WordClass

class CountWordsComparator implements Comparator<WordClass> {

    public int compare(WordClass lhs, WordClass rhs) {
        int countDifference = rhs.getCount() - lhs.getCount();
        if (countDifference != 0) {
            return countDifference;
        }
        return lhs.compareTo(rhs);
    }
}// end WordCountComparator

