Class OpenMessage

Object
OpenMessage
All Implemented Interfaces:
Message

public class OpenMessage extends Object implements Message
  • Field Details

  • Constructor Details

  • Method Details

    • getBody

      public String getBody()
      Gets the body of the message
      Specified by:
      getBody in interface Message
      Returns:
      String body
    • getTo

      public String getTo()
      Gets the "to" portion of the message
      Specified by:
      getTo in interface Message
      Returns:
      String to
    • getSubject

      public String getSubject()
      Gets the subject of the message
      Specified by:
      getSubject in interface Message
      Returns:
      String subject
    • getFrom

      public String getFrom()
      Gets the "from" portion of the message
      Specified by:
      getFrom in interface Message
      Returns:
      String from
    • search

      public boolean search(String term, String part)
      Searches for a particular term in a part of the message. Implemented in Lab 10.
      Specified by:
      search in interface Message
      Parameters:
      term - String term to check
      part - Portion of OpenMessage to check in
      Returns:
      boolean value of the term being in the part (not worrying about case)
    • searchSubject

      public boolean searchSubject(String term)
      Searches for a message by subject. Implemented in Lab 10.
      Specified by:
      searchSubject in interface Message
      Parameters:
      term - term used to see if contained in subject
      Returns:
      boolean result if term was in the subject
    • searchTo

      public boolean searchTo(String term)
      Searches for a message by "to" portion of the message. Implemented in Lab 10.
      Specified by:
      searchTo in interface Message
      Parameters:
      term - term used to see if contained in "to" portion
      Returns:
      boolean result if term was in the "to" portion
    • searchFrom

      public boolean searchFrom(String term)
      Searches for a message by "from" portion of the message. Implemented in Lab 10.
      Specified by:
      searchFrom in interface Message
      Parameters:
      term - term used to see if contained in "from" portion
      Returns:
      boolean result if term was in the "from" portion
    • toString

      public String toString()
      Creates and returns a String that better represents our OpenMessage object. In Lab 10, you created the messageToString() method in MessageView that printed the "TO:", "FROM:", "SUBJECT:", and "BODY:" information when called in MessageApp. This works, but since you've now learned about classes and their toString() methods let's get rid of the messageToString() method in Message View and copy the contents here, in OpenMessage's toString(). Notice that you can now call getTo(), etc. directly, and don't need an object to call it on because we are inside that object! For example, instead of adding "TO: " + m.getTo() + "\n" to your String or StringBuilder, you can add "TO: " + getTo() + "\n". Everything else will remain the same. Keep in mind that to print this information, you will be calling the toString() method on an OpenMessage object, not on the MessageView object, view in MessageApp.
      Overrides:
      toString in class Object
      Returns:
      String message
    • encrypt

      public String encrypt(String message, Key key)
      Encrypts a String using a Key. Lab 12's encryption will use the Caesar Cipher.

      Example output:

                 OpenMessage message1 = new OpenMessage("Cheshire Cat", "Quick Rabbit", "Important Meeting", "Don't be late for our very important date!");
                 Key key = new Key(1, 5);
                 Key key2 = new Key(2, 4);
                 System.out.println(message1.encrypt(message1.toString(), key));
                 System.out.println(message1.encrypt(message1.toString(), key2));
                 
      • First line prints YT?%Hmjxmnwj%HfyKWTR?%Vznhp%WfggnyXZGOJHY?%Nrutwyfsy%RjjynslGTI^?%Its,y%gj%qfyj%ktw%tzw%{jw~%nrutwyfsy%ifyj&
      • Second line prints XS>$Gliwlmvi$GexJVSQ>$Uymgo$VeffmxWYFNIGX>$Mqtsvxerx$QiixmrkFSH]>$Hsr+x$fi$pexi$jsv$syv$ziv}$mqtsvxerx$hexi%
      HINT: Remember type-casting and the relationship between ints and chars.
      Parameters:
      message - String message to encrypt
      key - Key used to encrypt the String
      Returns:
      encrypted String
    • decrypt

      public String decrypt(String message, Key key)
      Decrypts a String using a Key. Lab 12's decryption will reverse the Caesar Cipher.

      Example output:

               OpenMessage message1 = new OpenMessage("Cheshire Cat", "Quick Rabbit", "Important Meeting", "Don't be late for our very important date!");
               Key key1 = new Key(1, 4);
               //encrypting message
               String encryptedMessage = message1.encrypt(message1.toString(), key1);
               System.out.println(encryptedMessage);
               //decrypting message
               System.out.println(message1.decrypt(encryptedMessage, key1));
               /* Prints:
                   TO: Cheshire Cat
                   FROM: Quick Rabbit
                   SUBJECT: Important Meeting
                   BODY: Don't be late for our very important date!
            
      Encrypted message prints XS>$Gliwlmvi$GexJVSQ>$Uymgo$VeffmxWYFNIGX>$Mqtsvxerx$QiixmrkFSH]>$Hsr+x$fi$pexi$jsv$syv$ziv}$mqtsvxerx$hexi%. You can re-write your encrypt() code backwards, or you can call encrypt() but change the parameters to stay DRY!
      Parameters:
      message - String message to decrypt
      key - Key used to decrypt the String
      Returns:
      decrypted String
    • main

      public static void main(String[] args)