Advertisement
Dido09

Decoder

Mar 31st, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. package FinalExamPrep;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Charity {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String encrypted = scanner.nextLine();
  10. String command = scanner.nextLine();
  11.  
  12. while (!command.equals("Finish")) {
  13. String[] commandParts = command.split(" ");
  14. String commandName = commandParts[0];
  15.  
  16. switch (commandName) {
  17. case "Replace":
  18. encrypted = replace(encrypted, commandParts[1].charAt(0), commandParts[2].charAt(0));
  19. break;
  20. case "Cut":
  21. int startIndex = Integer.parseInt(commandParts[1]);
  22. int endIndex = Integer.parseInt(commandParts[2]);
  23. encrypted = cut(encrypted, startIndex, endIndex);
  24. break;
  25. case "Make":
  26. encrypted = make(encrypted, commandParts[1]);
  27. break;
  28. case "Check":
  29. check(encrypted, commandParts[1]);
  30. break;
  31. case "Sum":
  32. int startSumIndex = Integer.parseInt(commandParts[1]);
  33. int endSumIndex = Integer.parseInt(commandParts[2]);
  34. sum(encrypted, startSumIndex, endSumIndex);
  35. break;
  36. }
  37. command = scanner.nextLine();
  38. }
  39. }
  40.  
  41. private static void sum(String current, int startIndex, int endIndex) {
  42. int len = current.length();
  43. if (!isValidIndex(startIndex, len)) {
  44. System.out.println("Invalid indexes!");
  45. return;
  46. } else if (!isValidIndex(endIndex, len)) {
  47. System.out.println("Invalid indexes!");
  48. return;
  49. }
  50.  
  51. int sum = 0;
  52.  
  53.  
  54. for (int i = startIndex; i <= endIndex; i++ ){
  55. sum += current.charAt(i);
  56.  
  57. }
  58.  
  59. System.out.println(sum);
  60. }
  61.  
  62. private static void check(String current, String search) {
  63. int searchIndex = current.indexOf(search);
  64.  
  65. if (searchIndex == - 1){
  66. System.out.println("Message doesn't contain " + search);
  67. }else{
  68. System.out.println("Message contains " + search);
  69. }
  70. }
  71.  
  72. private static String make(String current, String targetCase) {
  73. String result;
  74. if (targetCase.equals("Upper")) {
  75. result = current.toUpperCase();
  76. } else {
  77. result = current.toLowerCase();
  78. }
  79.  
  80. System.out.println(result);
  81.  
  82. return result;
  83. }
  84.  
  85. private static String cut(String current, int startIndex, int endIndex) {
  86. int len = current.length();
  87. if (!isValidIndex(startIndex, len)) {
  88. System.out.println("Invalid indexes!");
  89. return current;
  90. } else if (!isValidIndex(endIndex, len)) {
  91. System.out.println("Invalid indexes!");
  92. return current;
  93. }
  94.  
  95. String firstPart = current.substring(0, startIndex);
  96. String secondPart = current.substring(endIndex + 1, len);
  97.  
  98. String result = firstPart + secondPart;
  99.  
  100. System.out.println(result);
  101.  
  102. return result;
  103.  
  104. }
  105.  
  106. private static boolean isValidIndex(int toCheck, int length) {
  107. return toCheck >= 0 && toCheck < length;
  108. }
  109.  
  110. private static String replace(String current, char searchFor, char replaceWith) {
  111. String result = current.replace(searchFor, replaceWith);
  112.  
  113. System.out.println(result);
  114.  
  115. return result;
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement