Advertisement
desislava_topuzakova

01. Activation Key

Mar 20th, 2024
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. package ExamPrep;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ActivationKey_01 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String activationKey = scanner.nextLine(); //ключ за активация
  10. String command = scanner.nextLine(); //команди, които трябва да изпълня върху activationKey
  11.  
  12. while (!command.equals("Generate")) {
  13.  
  14. String [] commandParts = command.split(">>>");
  15. String commandName = commandParts[0]; //"Contains", "Flip", "Slice"
  16.  
  17. switch(commandName) {
  18. case "Contains":
  19. //command = "Contains>>>def".split(">>>")-> ["Contains", "def"]
  20. String subString = commandParts[1];
  21. //проверка дали в activationKey се съдържа subString
  22. if (activationKey.contains(subString)) {
  23. //subString се съдържа в activationKey
  24. System.out.printf("%s contains %s%n", activationKey, subString);
  25. } else {
  26. //subString НЕ се съдържа в activationKey
  27. System.out.println("Substring not found!");
  28. }
  29. break;
  30. case "Flip":
  31. //command = "Flip>>>Upper/Lower>>>{startIndex}>>>{endIndex}".split(">>>") -> ["Flip", "Upper/Lower", "{startIndex}", "{endIndex}"]
  32. String type = commandParts[1]; //"Upper", "Lower"
  33. int startIndexForReplace = Integer.parseInt(commandParts[2]);
  34. int endIndexForReplace = Integer.parseInt(commandParts[3]);
  35. //1. взимаме текста между [startIndex, endIndex)
  36. String textForReplace = activationKey.substring(startIndexForReplace, endIndexForReplace);
  37. String newText = "";
  38. //2. променяме го спрямо type
  39. if (type.equals("Upper")) {
  40. newText = textForReplace.toUpperCase();
  41. } else if (type.equals("Lower")) {
  42. newText = textForReplace.toLowerCase();
  43. }
  44. //3. вмъкваме го на мястото на предишния
  45. activationKey = activationKey.replace(textForReplace, newText);
  46. System.out.println(activationKey);
  47. break;
  48. case "Slice":
  49. //command = "Slice>>>2>>>5".split(">>>")-> ["Slice","2", "5"]
  50. int startIndex = Integer.parseInt(commandParts[1]);
  51. int endIndex = Integer.parseInt(commandParts[2]);
  52. //изтривам всички символи от startIndex до endIndex (не е вкл.) на activationKey
  53. StringBuilder sb = new StringBuilder(activationKey);
  54. sb.delete(startIndex, endIndex);
  55. activationKey = sb.toString();
  56. System.out.println(activationKey);
  57. break;
  58. }
  59. command = scanner.nextLine();
  60. }
  61.  
  62. System.out.println("Your activation key is: " + activationKey);
  63.  
  64.  
  65. }
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement