Yargi

SecretChat

Jul 1st, 2020
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SecretChat {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         MessegaDecrypter decrypter = new MessegaDecrypter(sc.nextLine());
  7.         String input;
  8.         while (!"Reveal".equals(input = sc.nextLine())){
  9.             decrypter.interpreter(input);
  10.         }
  11.         decrypter.printMessage();
  12.     }
  13. }
  14.  
  15. class MessegaDecrypter{
  16.  
  17.     private String msg;
  18.  
  19.     public MessegaDecrypter(String msg){
  20.         this.msg = msg;
  21.     }
  22.  
  23.     public void interpreter(String command){
  24.  
  25.         String[] token = command.split(":\\|:");
  26.  
  27.         switch (token[0]){
  28.             case "InsertSpace":
  29.                 insertSpace(Integer.parseInt(token[1]));
  30.                 break;
  31.  
  32.             case "Reverse":
  33.                 cutReverseAndConcat(token[1]);
  34.                 break;
  35.  
  36.             case "ChangeAll":
  37.                 replace(token[1], token[2]);
  38.                 break;
  39.         }
  40.     }
  41.  
  42.     public void printMessage(){
  43.         System.out.printf("You have a new text message: %s", msg);
  44.     }
  45.  
  46.     private void insertSpace(int index){
  47.  
  48.         msg = msg.substring(0, index) + " " + msg.substring(index);
  49.         System.out.println(msg);
  50.     }
  51.  
  52.     private void cutReverseAndConcat(String toCut){
  53.  
  54.         if (msg.contains(toCut)) {
  55.             msg = msg.replace(toCut, "").concat(new StringBuilder(toCut).reverse().toString());
  56.             System.out.println(msg);
  57.         }
  58.         else {
  59.             System.out.println("error");
  60.         }
  61.     }
  62.  
  63.     private void replace(String toReplace, String replaceWith){
  64.         msg = msg.replaceAll(toReplace, replaceWith);
  65.         System.out.println(msg);
  66.     }
  67. }
Add Comment
Please, Sign In to add comment