

/**
 *  Program to convert regular English into Pig Latin using three methods: Main
 *  method, PhraseTranslator and WordTranslator.
 *
 *@author     Jeff
 *@created    May 15, 2003
 *@version    3.14 2002-10-15
 */
public class PigLatin2 {
	/**
	 *  The main program for the PigLatin2 class
	 *
	 *@param  args  The command line arguments
	 */
	public static void main(String[] args) {
		// Main method. It obtains the English sentence and prints the translation.
		ConsoleReader cr = new ConsoleReader(System.in);
		// Imports the Console Reader.
		System.out.print("\tThis program converts English to Pig Latin \n\t Please enter a Phrase in English.\n\n\tEnglish: ");
		String sentence = cr.readLine();
		// Get incoming sentence.
		if (sentence.equalsIgnoreCase("stop")) {
			System.out.println("\n\n\tThank you\n\tPlease come again!");
		}
		// If sentinal "stop" is entered, program terminates.
		while (!sentence.equalsIgnoreCase("stop")) {
			// Outer loop continues processing sentences until sentinal is entered.

			System.out.print("\n\tYou entered: " + sentence + "\n\n\tThe translation is: ");
			// Prepares ConsoleReader for output of translation.
			String pigSentence = PhraseTranslator(sentence);
			// Calls the method PhraseTranslator and gets the translated sentence.
			System.out.print(pigSentence);
			// Prints the translated sentence.
			System.out.print("\n\n\tPlease enter another sentence: ");
			// Asks for more user input.
			sentence = cr.readLine();
			// Acquires user input.
			if (sentence.equalsIgnoreCase("stop")) {
				System.out.println("\n\n\tThank you\n\tPlease come again!");
			}
			// If sentinal is entered, program terminates with a message.
		}
	}


	/**
	 *  Description of the Method
	 *
	 *@param  sentence  Description of the Parameter
	 *@return           Description of the Return Value
	 */
	static String PhraseTranslator(String sentence) {
		// Method header.

		String englishWord = "";
		String pigWord;
		String pigSentence = "";
		// String variables upon which words and sentences are built.
		char letter;
		char space = ' ';
		int index = 0;
		// Primitive data variables used to define elements of input/output.
		while (index < sentence.length()) {
			// Inner loop that builds english words out of the input, breaks them up and sends them for translation.
			englishWord = "";
			// String base upon which the extracted word is built.
			while (index < sentence.length()) {
				// Another inner loop that sorts letters until the end of input is reached.
				letter = sentence.charAt(index);
				// Acquires an individual letter.
				if (letter == (' ')) {
					break;
				}
				// If the letter is a space, the end of the word has been reached.
				if (letter == ('-')) {
					englishWord = (englishWord + letter);
					break;
				}
				// If the word is hyphenated, include the hyphen at the end of the new word.
				else {
					// If the word is not hyphenated, continue building word until the end of the word.
					englishWord = (englishWord + letter);
				}
				index++;
				// Move to next letter.
			}
			pigWord = WordTranslator(englishWord);
			// The new word is sent to the word translator.
			pigSentence = (pigSentence + pigWord);
			// When the word comes back from the translator, it is added to the translated sentence.
			index++;
			// Move to next letter.
		}
		return pigSentence;
		// Sends the translated sentence back to the main method for printing.
	}


	/**
	 *  Description of the Method
	 *
	 *@param  englishWord  Description of the Parameter
	 *@return              Description of the Return Value
	 */
	static String WordTranslator(String englishWord) {
		// This is the word translating method.

		String pigPrefix = "";
		String pigSuffix = "";
		int pigIndex = 0;
		String pigWord = englishWord;
		// Strings upon which the translated word is built.
		String vowels = "AEIOUaeiou";
		String punctuation = ":;,.!?-";
		String pigPunctuation = "";
		// Variables that define the characteristics of each word.
		char pigLetter = pigWord.charAt(pigIndex);
		// Picks the first letter of the word.
		if (vowels.indexOf(pigLetter) >= 0) {
			// Determine if the letter is a vowel.

			while (pigIndex < pigWord.length()) {
				// If the first letter is a vovel, the following loop is repeated until the end of the word.

				pigLetter = pigWord.charAt(pigIndex);
				// Translated word is built using the same letters in the same order as the English word.
				if (punctuation.indexOf(pigLetter) != -1) {
					break;
				}
				// When punctuation is encountered, escape from loop.
				else {
					// If punctuation is not encountered continue reading and translating the input.

					pigPrefix = (pigPrefix + pigLetter);
				}
				pigIndex++;
			}
			if (punctuation.indexOf(pigLetter) != -1) {
				// If punctuation is encountered, add the punctuation to the end of the word.
				pigPunctuation = (pigPunctuation + pigLetter);
			}
			if (pigPunctuation.equals("-")) {
				pigWord = (pigPrefix + pigSuffix + "yay" + pigPunctuation);
			}
			// If that punctuation happens to be a hyphen, do not include a space after the word.
			else {
				// If the punctuation is not a hyphen, put a space after the word.
				pigWord = (pigPrefix + "yay" + pigPunctuation + " ");
			}
		} else {
			// If the word begins with a consonant, do the following.

			pigLetter = pigWord.charAt(pigIndex);
			while ((vowels.indexOf(pigLetter) == -1) && (pigIndex < pigWord.length())) {
				// If the first letter is a consonant, sets up a loop to continue until the end of the word is reached.

				vowels = (vowels + "Y" + "y");
				// In the interior of the word, Y becomes a vowel.
				pigSuffix = (pigSuffix + pigLetter);
				// Places consonants from the front of input to the end of the translation.
				pigIndex++;
				// Increments to next letter.
				pigLetter = pigWord.charAt(pigIndex);
				// Acquires next letter.
				if (punctuation.indexOf(pigLetter) != -1) {
					break;
				}
				// If the next letter is a vowel, skip to next section. Otherwise, go back to top of this loop.
			}
			while ((pigIndex < pigWord.length()) && ((punctuation.indexOf(pigLetter) == -1))) {
				// Sets up a loop to process until the end of each translated word, jumps to next section when punctuation is encountered.

				pigPrefix = (pigPrefix + pigLetter);
				// Examines each letter and builds the prefix of the translated word.
				pigIndex++;
				if (pigIndex == pigWord.length()) {
					break;
				}
				// When the end of the word is reached, breaks out of this loop.
				pigLetter = pigWord.charAt(pigIndex);
			}
			if (punctuation.indexOf(pigLetter) != -1) {
				pigPunctuation += pigLetter;
			}
			// If punctuation is encountered, does the following operations.
			if (pigPunctuation.equals("-")) {
				pigWord = (pigPrefix + pigSuffix + "ay" + pigPunctuation);
			}
			// If the punctuation is a hyphen, includes it at the end of the word with no space.
			else {
				pigWord = (pigPrefix + pigSuffix + "ay" + pigPunctuation + " ");
			}
			// If regular punctuation, puts a space after the word.
		}
		return pigWord;
		// Returns word to caller for printing.
	}
	// End of word translator method.
}

