import java.util.*;

/**
 * Counts the number of words
 * that start with an uppercase letter
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 3.0
 */
public class UCWordCounter implements LineObserver
{
    private int      lastCount;


    /**
     * Default Constructor
     */
    public UCWordCounter()
    {
       lastCount = 0;
    }



    /**
     * Count the words that start with an uppercase letter
     *
     * @param line   The line containing the words
     * @return       The count
     */
    private int count(String line)
    {
       int                 ucCount;
       StringTokenizer     tokenizer;
       String              letter, letterUC, word;



       ucCount = 0;
       tokenizer = new StringTokenizer(line);

       while (tokenizer.hasMoreTokens()) 
       {
          word = tokenizer.nextToken();
          letter = word.substring(0,1);
          letterUC = letter.toUpperCase();
          if (letter.equals(letterUC)) ++ucCount;
       }
	
       return ucCount;
    }



    /**
     * Handle a line of text
     *
     * @param source   The LineSubject that generated the line
     */
    public void handleLine(LineSubject source)
    {
       String        line;

       line      = source.getLine();
       lastCount = count(line); 
       displayCount();
    }


    /**
     * Display the count
     *
     * Note: Output should be handled by another class.  It is
     * included here to simplify the example.
     */
    private void displayCount()
    {
       System.out.println("Start with uppercase: "+lastCount);
    }
}
