Advertisement
veronikaaa86

03. Moving Target

Jun 21st, 2022
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package examPrep;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class P03MovingTarget {
  10. public static void main(String[] args) {
  11. Scanner scanner = new Scanner(System.in);
  12.  
  13. List<Integer> targetList = Arrays.stream(scanner.nextLine().split(" "))
  14. .map(Integer::parseInt)
  15. .collect(Collectors.toList());
  16.  
  17. String inputLine = scanner.nextLine();
  18. while (!inputLine.equals("End")) {
  19. String[] commandLine = inputLine.split(" ");
  20. String command = commandLine[0];
  21. int index = Integer.parseInt(commandLine[1]);
  22. int value = Integer.parseInt(commandLine[2]);
  23.  
  24. switch (command) {
  25. case "Shoot":
  26. if (validIndex(targetList, index)) {
  27. int currentNum = targetList.get(index);
  28. currentNum = currentNum - value;
  29. if (currentNum <= 0) {
  30. targetList.remove(index);
  31. } else {
  32. targetList.set(index, currentNum);
  33. }
  34. }
  35. break;
  36. case "Add":
  37. if (validIndex(targetList, index)) {
  38. targetList.add(index, value);
  39. } else {
  40. System.out.println("Invalid placement!");
  41. }
  42. break;
  43. case "Strike":
  44. boolean validIndexRadius = targetList.size() - 1 >= index
  45. && targetList.size() - 1 >= index + value
  46. && index - value >= 0;
  47. if (validIndexRadius) {
  48. int radius = value * 2 + 1;
  49. for (int i = 0; i < radius; i++) {
  50. targetList.remove(index - value);
  51. }
  52. } else {
  53. System.out.println("Strike missed!");
  54. }
  55. break;
  56. }
  57.  
  58. inputLine = scanner.nextLine();
  59.  
  60. }
  61.  
  62. List<String> resultList = new ArrayList<>();
  63. for (int target : targetList) {
  64. resultList.add(String.valueOf(target));
  65. }
  66. System.out.println(String.join("|", resultList));
  67. }
  68.  
  69. public static boolean validIndex (List<Integer> list, int index) {
  70. return index <= list.size() - 1 && index >= 0;
  71. }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement