Advertisement
jwrbg

ad

Mar 28th, 2019
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. package TechModule;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class TheFinalQuestExam {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         List<String> inputInformation = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
  14.  
  15.         String command = scanner.nextLine();
  16.  
  17.         while (!command.equals("Stop")) {
  18.             List<String> enteredCommand = Arrays.stream(command.split(" ")).collect(Collectors.toList());
  19.             String action = enteredCommand.get(0);
  20.             switch (action) {
  21.                 case "Delete":
  22.                     int a = (Integer.parseInt(enteredCommand.get(1)) + 1);
  23.                     if (a < inputInformation.size()) {
  24.                         inputInformation.remove(a);
  25.                     }
  26.                     break;
  27.                 case "Swap":
  28.                     boolean wordOne = false;
  29.                     boolean wordTwo = false;
  30.                     for (int i = 0; i < inputInformation.size(); i++) {
  31.  
  32.                         if (inputInformation.get(i).contains(enteredCommand.get(1))) {
  33.                             wordOne = true;
  34.                         }
  35.                         if (inputInformation.get(i).contains(enteredCommand.get(2))) {
  36.                             wordTwo = true;
  37.                         }
  38.                         if (wordOne && wordTwo) {
  39.                             int b = inputInformation.indexOf(enteredCommand.get(2));
  40.                             int c = inputInformation.indexOf(enteredCommand.get(1));
  41.  
  42.                             inputInformation.set(c, enteredCommand.get(2));
  43.                             inputInformation.set(b, enteredCommand.get(1));
  44.                             break;
  45.  
  46.                         }
  47.                     }
  48.                     break;
  49.                 case "Put":
  50.                     if (Integer.parseInt(enteredCommand.get(2)) > 0 && Integer.parseInt(enteredCommand.get(2)) < inputInformation.size()) {
  51.                         inputInformation.add((Integer.parseInt(enteredCommand.get(2)) - 1), enteredCommand.get(1));
  52.                     }
  53.                     break;
  54.                 case "Sort":
  55.                     Collections.reverse(inputInformation);
  56.                     break;
  57.                 case "Replace":
  58.                     boolean isHere = false;
  59.                     for (int i = 0; i < inputInformation.size(); i++) {
  60.                         if (enteredCommand.get(2).contains(inputInformation.get(i))) {
  61.                             isHere = true;
  62.                         }
  63.                         if (isHere) {
  64.                             int d = inputInformation.indexOf(enteredCommand.get(2));
  65.                             inputInformation.set(d, enteredCommand.get(1));
  66.                             break;
  67.                         }
  68.  
  69.                     }
  70.                     break;
  71.             }
  72.  
  73.             command = scanner.nextLine();
  74.         }
  75.  
  76.         for (String word : inputInformation) {
  77.             System.out.print(word + " ");
  78.  
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement