//-----------------------------------------------------------------  
// Insert standard header information here.
//-----------------------------------------------------------------  
/* SimpleCaesar will implement a simple caesar cipher.
 * It uses a character array, cipher, to contain the mapping for enciphering.
 * The array is indexed 0..25 where each index represents the distance 
 * of the letter from the beginning of the alphabet.
 * The magic "c - 'a'" that you see throughout the code is 
 * a convenient way of finding the distance a lowercase character 
 * is from the beginning of the alphabet.
 */

import cs1.Keyboard;

public class StartCaesar {
  private final static int SIZE = 26;             // 26 letters in alphabet
  private static char[] cipher = new char[SIZE];  // one element per letter

  // ---------------------------------------------------------------------
  // main method
  //
  // User Input:      amount of offset, as a positive integer
  //                  String to be enciphered
  // Program Output:  String as enciphered
  //
  //  REQUIRES STUDENT MODIFICATION
  //    Needs only three relatively minor changes to do the basic task.
  //    Needs additional code to determine if there is
  //      another String to be encrypted, and to act accordingly.
  //
  // ---------------------------------------------------------------------
  
  public static void main(String[] args)
  { int offset;                           // the KEY for how much to shift
    String plain;                         // the original plaintext message

    // -----  GET THE "KEY" TO USE (i.e., the offset value) ----- 

    System.out.println("\tRevise the program so it will ask the user to ");  // omit when done
    System.out.print("Enter the encryption key to use: ");
    offset = 0;         // modify to obtain user input; limit it to valid values, if you can

    // -----  USE THE "KEY" (i.e., build cipher[]) ----- 

    makeCipher(offset);

    // -----  GET THE "PLAINTEXT" (i.e., the message to encode) ----- 

    plain = getPlainText();

    // -----  DISPLAY THE MESSAGE IN ITS SECRET FORM:

    System.out.println("\n\tRevise the program so it will display ");        // omit when done
    System.out.println("The secret version of your text:");       
    showSecret(plain);
  }

// ----- DEVELOP THESE METHODS and MODIFY COMMENTS APPROPRIATELY -----

  // ---------------------------------------------------------------------
  // check method
  //
  // in this demo, it just returns the letter it received
  // in your program, it should check if the character it received
  //   - is a letter, then encode it (depending on whether it's lower or upper case)
  //   - is not a letter, simply return it, as the method currently does.
  //
  // REQUIRES STUDENT MODIFICATION
  // ---------------------------------------------------------------------

  public static char check (char c)
  { char temp;
    temp = c;
    return temp;
  }
  
  // ---------------------------------------------------------------------
  // encode method
  //
  // in this demo, it just returns the letter it received
  // in your program, it should accept a lower case letter, and return the 
  //                  secretly encoded version of that letter
  //
  // REQUIRES STUDENT MODIFICATION
  // ---------------------------------------------------------------------
  public static char encode (char c)
  { return c;
  }
  
  // ---------------------------------------------------------------------
  // encodeUpper method
  //
  // in this demo, it just returns the letter it received
  // in your program, it should accept an upper case letter, convert that 
  //                  to lower case, encode the lower case letter, convert
  //                  that back to upper case, and return that letter
  //
  // REQUIRES STUDENT MODIFICATION
  //
  // Note:  it is possible to do all of this work inside the encode() method.
  // But you should use this method, to practice writing and calling methods.
  // ---------------------------------------------------------------------
  public static char encodeUpper(char c)
  { char temp;
    temp = c;
    return temp;
  }
  
  // ---------------------------------------------------------------------
  // makeCipher method
  //
  // in this demo, it does nothing
  // in your program, it should create the cipher[] array, to hold the 
  //                  letters to substitute for each character in plain text
  // note that it will have to handle both offset keys in [0, 25] (simple)
  //                  and ones outside that range (Hint: % is again useful,
  //                  but it's not sufficient on its own to achieve this.)
  //
  // REQUIRES STUDENT MODIFICATION
  // ---------------------------------------------------------------------
  public static void makeCipher(int key)
  { key = key;
  }


  // ---------------------------------------------------------------------
  // getPlainText method
  //
  // THIS METHOD IS A "HACK" -- PLEASE USE THIS CODE AS-IS.  DO NOT MODIFY IT!
  //
  // Because the Keyboard class is slightly brain-damaged, 
  // and may think it's reached the end of our String before its real end,
  // we keep reading pieces and appending them to the previous input
  // until the users *clearly* indicate the end of their input 
  // (i.e., for those who care, since K.readString() returns a null string 
  // at the end of the input, we copy until the read results in "null").
  // REPEAT: 101-STUDENTS SHOULD USE THIS METHOD AS PROVIDED BY THE INSTRUCTOR.

  // ---------------------------------------------------------------------
  public static String getPlainText()
  { String s, text;               // we need local versions of these vars
    System.out.println("Enter your plaintext, and end with ^D (or ^Z on DOS):");
    text = new String("");
    do 
    { s = Keyboard.readString();
      if ( s != null )
      {
        // Append the new line to the text string.  
        // Also append a newline ('\n') character 
        // to replace the one that Keyboard.readString() consumes
        text = text + s +'\n';
      }
    } while ( s != null );
    return text;
  
  }

  // ---------------------------------------------------------------------
  // showSecret method
  //
  // This method reads through the input one character at a time,
  // sends each character off to be encoded, and then prints the
  // character returned from the encoding process.
  //
  // Since, at the moment, the encoding methods do nothing, 
  //        at the moment, this simply returns the original message.
  // Once you get the earlier methods working, you should be able to 
  //        just use this method as-is (i.e., all the changes go above).
  // ---------------------------------------------------------------------

  public static void showSecret(String text)
  {
    char c;                                  // a temporary character variable
    for(int i=0; i < text.length(); i++)     // for each character in the input
    { c = check(text.charAt(i));             // GET ENCODED LETTER INSTEAD OF ORIGINAL
      System.out.print(c);                   // print out the letter
    }
  }

}