/*
 * File: Processor.java
 * class stores text, checks and corrects the placement
 * of articles a and an, and prints the corrected test.
 */
 
public class Processor {

   private String line;
   
   private String output;
   private int count;
   private boolean itsA;
   
   public Processor (String l) {
      line = l;
   }
   
   /*
    * Check placement of articles, 
    * correct any errors in placement.
    */
    
   public boolean process() {
   
      output = "";
      count = 0;
      int nextLetter;
      
      while (count < line.length()) {
      
         // Stop at the next a.
         while (count < line.length() 
               && ! line.substring(count, count + 1).equalsIgnoreCase("A")) {
            output = output + line.substring(count, count + 1);
            count ++;
         }
         
         // Check if the end of the line of text has been reached.
         if (count == line.length())
         
            return true;
            
         else {
         
            // Otherwise it must be an a so add the a.
            output = output + line.substring(count, count + 1);
            count ++;
         }
         
         // Check that the a is not just part of a larger word.
         if (count == 1 || line.substring(count - 2, count - 1).equals(" ")) {
         
            // Check that it is an a.
            if (count == line.length() || 
               (count < line.length() && 
               line.substring (count, count + 1).equals(" "))) {
               itsA = true;
               nextLetter = findNextLetter (count);
               // Check whether there is no following word on this line.
               if (nextLetter == line.length())
                  return false;
               // Check whether the following word starts with a vowel.
               // If so correct the text.
               if (isVowel (line.substring (nextLetter, nextLetter + 1)))
                  output = output + "n";
            }
            
            // Check that the an is not just part of a larger word.
            if ((count + 1 == line.length() && line.substring(count, count + 1).equals("n"))
               || (count + 1 < line.length() && line.substring(count, count + 1).equals("n") && line.substring(count + 1, count + 2).equals(" "))) {
               itsA = false;
               nextLetter = findNextLetter (count + 1);
               // Check whether there is no following word on this line.
               if (nextLetter == line.length())
                  return false;
               // Check whether the following word does not start with a vowel.
               // If so correct the text.
               if (! isVowel (line.substring (nextLetter, nextLetter + 1)))
                  count ++;
            }
                           
         }
      }
      return true;
   }
   
   /* 
    * Create a printalbe line of text.
    * @return the line of corrected text.
    */
    
   public String toString() {

      return output;
   }

   /* 
    * Deal with articles at the end of a line of text
    * by checking the first letter of the next line.
    */
     
   public void finish (String l) {

      int firstLetter = findNextLetter(0);
      
      if (isVowel(l.substring (firstLetter, firstLetter + 1)) && itsA)
   
         output = output + "n" + line.substring (count);
                  
      else if (! isVowel(l.substring (firstLetter, firstLetter + 1)) && ! itsA)
                  
         output = output + line.substring (count + 1);
      else
         output = output + line.substring (count);
   }

   /* 
    * Check if a letter is a vowel.
    */
    
   private boolean isVowel (String letter) {

      return "aeiou".indexOf (letter) > -1;
   }

   /*
    * Find the next letter (the next non-space.)
    */
   private int findNextLetter (int nextLetter) {

      while (nextLetter < line.length()
            && line.substring(nextLetter, nextLetter + 1).equals(" ")) 
         nextLetter++;

      return nextLetter;              
   }
}

