

import java.util.*;
import java.io.*;

public class CodeBreaker {

    // declare instance variables here









    // This is for part (B)
    // Input: a coded sentence
    // Output: a decoded English sentence

    public String translateSentence (String codedSentence) {

	StringBuffer words = new StringBuffer();
	StringTokenizer st = new StringTokenizer(codedSentence, " ");

	while (st.hasMoreTokens()) {

	    // fill in code to parse the input coded sentence and translate it

	}
	return words.toString();

    }

    // This is for part (C)
    // Input: an English  sentence
    // Output: a coded sentence

    public String codeSentence (String sentence) {

	StringBuffer code = new StringBuffer();
	StringTokenizer st = new StringTokenizer(sentence, " ");

	while (st.hasMoreTokens()) {

	    // fill in code to parse the input sentence and code it
	}
	return code.toString();
    }


    // Read in the codes and their translations; store in an appropriate data structure

    public  void buildTranslaterFromFile(String FileName) {
	String str;
	try {
	    BufferedReader fin = new BufferedReader(new FileReader (FileName));

	    while ((str=fin.readLine())!=null) {

		StringTokenizer st = new StringTokenizer(str, "|");

                // fill in code to read the file into the data structure

	    }
	    fin.close();
	}
	catch (Exception ioe) {
	    System.err.println("CodeBreaker: " + ioe.toString());
	    System.exit(1);
	}
    }


    // this is code for the input/output
    // it lets the user switch between coding and translating sentences

    // NOTE: THIS DOES NOT WORK IN TEXTPAD IF YOU HAVE
    // AUTOMATIC CAPTURE SET -- YOU HAVE TO UNSET THIS
    // IN THE CONFIGURE MENU.  YOU KNOW YOU DID IT RIGHT
    // WHEN YOU SEE THE BLACK BACKGROUND WHEN YOU RUN THE CODE.

    public void tryCode () throws Exception {

             String inputLine ="";
             InputStreamReader isr = new InputStreamReader(System.in);
             BufferedReader keyboard = new BufferedReader(isr);
             System.out.println("Type C^D or quit to exit, type switch to change modes.");
             boolean translate = true;
             System.out.print("Translate Sentence > ");
             while ((inputLine = keyboard.readLine()) != null) {
                    if (inputLine.equals("quit"))
                    break;
                    if (inputLine.equals("switch")) {
                    if (translate == true) {
                        translate = false;
                        System.out.print("Code Sentence > ");
                    } else {
                        translate = true;
                        System.out.print("Translate Sentence > ");
                    }
                    } else if (translate) {
                             System.out.println("Translation is: " +
                           this.translateSentence(inputLine));
                     System.out.print("Translate Sentence > ");
                    } else {
                             System.out.println("Coded sentence is: " +
                           this.codeSentence(inputLine));
                     System.out.print("Code Sentence > ");
                    }
        }
    }


    // Try out some translations

    public static void main (String args[]) {

	String translation;
	CodeBreaker code = new CodeBreaker();

	code.buildTranslaterFromFile("codefile.txt");

        translation = "vertebral lug contravention derision Sterno chignon blister ubiquity escape Himalaya";
	System.out.println("For sentence " + translation);

	System.out.println("Translation is: " +
			   code.translateSentence(translation));

        translation = "pacify presto incentive fir pink tragicomic Sterno chignon Bengali Willis lew renegotiable";

	System.out.println("For sentence " + translation);
	System.out.println("Translation is: " +
			   code.translateSentence(translation));


    }

}
