Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. package ExamPre;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class Weaponsmith {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11. List<String> partsWeapon = new ArrayList<>(Arrays.asList(scanner.nextLine().split("\\|")));
  12.  
  13. String command = scanner.nextLine();
  14.  
  15. while (!command.equals("Done")) {
  16. String[] token = command.split(" ");
  17. String type = token[1];
  18.  
  19.  
  20. switch (type) {
  21.  
  22. case "Left": {
  23. int index = Integer.parseInt(token[2]);
  24. moveLeft(partsWeapon, index);
  25. }
  26. break;
  27. case "Right": {
  28. int index = Integer.parseInt(token[2]);
  29. moveRight(partsWeapon, index);
  30. }
  31. break;
  32. case "Even":
  33. System.out.println(String.join(" ", checkList(partsWeapon)));
  34. break;
  35. case "Odd":
  36. System.out.println(String.join(" ", checkOddList(partsWeapon)));
  37.  
  38.  
  39. break;
  40. }
  41. command = scanner.nextLine();
  42.  
  43.  
  44. }
  45. System.out.print("You crafted ");
  46. System.out.print(String.join("", partsWeapon));
  47. System.out.print("!");
  48.  
  49.  
  50. }
  51.  
  52. //
  53. private static List<String> checkOddList(List<String> partsWeapon) {
  54. List<String> test = new ArrayList<>();
  55. for (int i = 0; i < partsWeapon.size(); i++) {
  56. if (i % 2 == 1) {
  57. test.add(partsWeapon.get(i));
  58. }
  59.  
  60. }
  61.  
  62.  
  63. return test;
  64. }
  65.  
  66. private static List<String> checkList(List<String> partsWeapon) {
  67. List<String> test = new ArrayList<>();
  68. for (int i = 0; i < partsWeapon.size(); i++) {
  69. if (i % 2 == 0) {
  70. test.add(partsWeapon.get(i));
  71. }
  72.  
  73. }
  74.  
  75. return test;
  76. }
  77.  
  78. private static void moveLeft(List<String> partsWeapon, int index) {
  79. if (isValidIndex(partsWeapon, index - 1)) {
  80. String word = partsWeapon.get(index - 1);
  81. partsWeapon.set(index - 1, partsWeapon.get(index));
  82. partsWeapon.set(index, word);
  83. }
  84. }
  85.  
  86. private static void moveRight(List<String> partsWeapon, int index) {
  87. if (isValidIndex(partsWeapon, index + 1)) {
  88. String word = partsWeapon.get(index + 1);
  89. partsWeapon.set(index + 1, partsWeapon.get(index));
  90. partsWeapon.set(index, word);
  91. }
  92.  
  93. }
  94.  
  95. public static boolean isValidIndex(List<String> input, int index) {
  96. return 0 <= index && index < input.size();
  97.  
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement