Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ExamPre;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- public class Weaponsmith {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> partsWeapon = new ArrayList<>(Arrays.asList(scanner.nextLine().split("\\|")));
- String command = scanner.nextLine();
- while (!command.equals("Done")) {
- String[] token = command.split(" ");
- String type = token[1];
- switch (type) {
- case "Left": {
- int index = Integer.parseInt(token[2]);
- moveLeft(partsWeapon, index);
- }
- break;
- case "Right": {
- int index = Integer.parseInt(token[2]);
- moveRight(partsWeapon, index);
- }
- break;
- case "Even":
- System.out.println(String.join(" ", checkList(partsWeapon)));
- break;
- case "Odd":
- System.out.println(String.join(" ", checkOddList(partsWeapon)));
- break;
- }
- command = scanner.nextLine();
- }
- System.out.print("You crafted ");
- System.out.print(String.join("", partsWeapon));
- System.out.print("!");
- }
- //
- private static List<String> checkOddList(List<String> partsWeapon) {
- List<String> test = new ArrayList<>();
- for (int i = 0; i < partsWeapon.size(); i++) {
- if (i % 2 == 1) {
- test.add(partsWeapon.get(i));
- }
- }
- return test;
- }
- private static List<String> checkList(List<String> partsWeapon) {
- List<String> test = new ArrayList<>();
- for (int i = 0; i < partsWeapon.size(); i++) {
- if (i % 2 == 0) {
- test.add(partsWeapon.get(i));
- }
- }
- return test;
- }
- private static void moveLeft(List<String> partsWeapon, int index) {
- if (isValidIndex(partsWeapon, index - 1)) {
- String word = partsWeapon.get(index - 1);
- partsWeapon.set(index - 1, partsWeapon.get(index));
- partsWeapon.set(index, word);
- }
- }
- private static void moveRight(List<String> partsWeapon, int index) {
- if (isValidIndex(partsWeapon, index + 1)) {
- String word = partsWeapon.get(index + 1);
- partsWeapon.set(index + 1, partsWeapon.get(index));
- partsWeapon.set(index, word);
- }
- }
- public static boolean isValidIndex(List<String> input, int index) {
- return 0 <= index && index < input.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement