Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class TheFinalQuest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> inputInformation = Arrays.stream(scanner.nextLine().split("\\s+")).collect(Collectors.toList());
- String command = scanner.nextLine();
- while (!command.equals("Stop")) {
- List<String> enteredCommand = Arrays.stream(command.split("\\s+")).collect(Collectors.toList());
- String action = enteredCommand.get(0);
- switch (action) {
- case "Delete":
- int a = (Integer.parseInt(enteredCommand.get(1)) + 1);
- if (a < inputInformation.size()) {
- inputInformation.remove(a);
- }
- break;
- case "Swap":
- boolean wordOne = false;
- boolean wordTwo = false;
- for (int i = 0; i < inputInformation.size(); i++) {
- if (inputInformation.get(i).contains(enteredCommand.get(1))) {
- wordOne = true;
- }
- if (inputInformation.get(i).contains(enteredCommand.get(2))) {
- wordTwo = true;
- }
- if (wordOne && wordTwo) {
- int b = inputInformation.indexOf(enteredCommand.get(2));
- int c = inputInformation.indexOf(enteredCommand.get(1));
- inputInformation.set(c, enteredCommand.get(2));
- inputInformation.set(b, enteredCommand.get(1));
- break;
- }
- }
- break;
- case "Put":
- if (Integer.parseInt(enteredCommand.get(2)) > 0 && Integer.parseInt(enteredCommand.get(2)) < inputInformation.size()) {
- inputInformation.add((Integer.parseInt(enteredCommand.get(2)) - 1), enteredCommand.get(1));
- }
- break;
- case "Sort":
- Collections.reverse(inputInformation);
- break;
- case "Replace":
- boolean isHere = false;
- for (int i = 0; i < inputInformation.size(); i++) {
- if (enteredCommand.get(2).contains(inputInformation.get(i))) {
- isHere = true;
- }
- if (isHere) {
- int d = inputInformation.indexOf(enteredCommand.get(2));
- inputInformation.set(d, enteredCommand.get(1));
- break;
- }
- }
- break;
- }
- command = scanner.nextLine();
- }
- for (String word : inputInformation) {
- System.out.print(word + " ");
- }
- }
- }
Add Comment
Please, Sign In to add comment