Advertisement
IrinaIgnatova

The Final Quest

Jun 28th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.  
  14.         String[] line = scanner.nextLine().split(" +");
  15.         List<String> words = new ArrayList<>();
  16.         for (int i = 0; i < line.length; i++) {
  17.             words.add(line[i]);
  18.  
  19.         }
  20.         String command = scanner.nextLine();
  21.         while (!command.equals("Stop")) {
  22.             String[] tokens = command.split(" +");
  23.             String instruction = tokens[0];
  24.  
  25.             if (instruction.equals("Swap")) {
  26.                 String firstWord = tokens[1];
  27.                 int firstIndex = words.indexOf(firstWord);
  28.                 String secondWord = tokens[2];
  29.                 int secondIndex = words.indexOf(secondWord);
  30.                 if (firstIndex >= 0 && firstIndex < words.size()) {
  31.                     if (secondIndex >= 0 && secondIndex < words.size()) {
  32.                         Collections.swap(words, firstIndex, secondIndex);
  33.                     }
  34.                 }
  35.  
  36.  
  37.             } else if (instruction.equals("Replace")) {
  38.                 String word1 = tokens[1];
  39.                 String word2 = tokens[2];
  40.                 int index2 = words.indexOf(word2);
  41.                 if (index2 >= 0 && index2 < words.size()) {
  42.                     words.set(index2, word1);
  43.                 }
  44.  
  45.             } else if (instruction.equals("Delete")) {
  46.                 int index = Integer.valueOf(tokens[1]);
  47.                 int indexToRemove = index + 1;
  48.  
  49.                 if (indexToRemove >= 0 && indexToRemove < words.size()) {
  50.                     String wordToremove = words.get(indexToRemove);
  51.                     words.remove(wordToremove);
  52.                 }
  53.  
  54.             } else if (instruction.equals("Put")) {
  55.                 String wordToAdd = tokens[1];
  56.                 int indexToUse = Integer.parseInt(tokens[2]) - 1;
  57.  
  58.                 if (indexToUse == words.size()) {
  59.                     words.add(wordToAdd);
  60.                 } else if (indexToUse >= 0 && indexToUse < words.size()) {
  61.  
  62.                     words.add(indexToUse, wordToAdd);
  63.  
  64.                 }
  65.  
  66.             } else if (instruction.equals("Sort")) {//descending order
  67.                 Collections.sort(words);
  68.                 Collections.reverse(words);
  69.             }
  70.  
  71.  
  72.             command = scanner.nextLine();
  73.         }
  74.         for (String word : words) {
  75.             System.out.print(word + " ");
  76.         }
  77.  
  78.  
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement