Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- import java.util.stream.Collectors;
- import static java.lang.Integer.valueOf;
- import static java.util.Collections.*;
- public class FinalQuest {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- List<String> strings = Arrays.stream(sc.nextLine().split(" ")).collect(Collectors.toList());
- String input = sc.nextLine();
- while (!input.equals("Stop")) {
- String[] command = input.split(" ");
- switch (command[0]) {
- case "Delete":
- int indexDelete = valueOf(command[1]) + 1;
- if (isValid(strings, indexDelete)) {
- strings.remove(indexDelete);
- }
- break;
- case "Swap":
- int index1 = strings.indexOf(command[1]);
- int index2 = strings.indexOf(command[2]);
- if (index1 >= 0 && index2 >= 0 && index1 < strings.size() && index2 < strings.size()) {
- String string1 = command[1];
- strings.set(index1, command[2]);
- strings.set(index2, string1);
- }
- break;
- case "Put":
- int index = Integer.parseInt(command[2]) - 1;
- if (isValid(strings, index)) {
- strings.add(index, command[1]);
- } else if (index == strings.size()) {
- strings.add(command[1]);
- }
- break;
- case "Sort":
- Collections.sort(strings);
- Collections.reverse(strings);
- break;
- case "Replace":
- index = strings.indexOf(command[2]);
- if (isValid(strings, index)) {
- strings.set(index, command[1]);
- }
- break;
- default:
- throw new IllegalStateException("Unknown" + command[0]);
- }
- input = sc.nextLine();
- }
- for (String word : strings)
- System.out.print(word + " ");
- }
- private static boolean isValid(List<String> strings, int index) {
- return 0 <= index && index < strings.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement