Advertisement
meteor4o

JF-MidExam-10March-Group2-02.TheFinalQuest

Jun 28th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5. import static java.lang.Integer.valueOf;
  6. import static java.util.Collections.*;
  7.  
  8. public class FinalQuest {
  9.     public static void main(String[] args) {
  10.  
  11.         Scanner sc = new Scanner(System.in);
  12.  
  13.         List<String> strings = Arrays.stream(sc.nextLine().split(" ")).collect(Collectors.toList());
  14.         String input = sc.nextLine();
  15.  
  16.         while (!input.equals("Stop")) {
  17.             String[] command = input.split(" ");
  18.             switch (command[0]) {
  19.                 case "Delete":
  20.                     int indexDelete = valueOf(command[1]) + 1;
  21.                     if (isValid(strings, indexDelete)) {
  22.                         strings.remove(indexDelete);
  23.                     }
  24.                     break;
  25.                 case "Swap":
  26.                     int index1 = strings.indexOf(command[1]);
  27.                     int index2 = strings.indexOf(command[2]);
  28.                     if (index1 >= 0 && index2 >= 0 && index1 < strings.size() && index2 < strings.size()) {
  29.                         String string1 = command[1];
  30.                         strings.set(index1, command[2]);
  31.                         strings.set(index2, string1);
  32.                     }
  33.                     break;
  34.                 case "Put":
  35.                     int index = Integer.parseInt(command[2]) - 1;
  36.                     if (isValid(strings, index)) {
  37.                         strings.add(index, command[1]);
  38.                     } else if (index == strings.size()) {
  39.                         strings.add(command[1]);
  40.                     }
  41.                     break;
  42.                 case "Sort":
  43.                     Collections.sort(strings);
  44.                     Collections.reverse(strings);
  45.                     break;
  46.                 case "Replace":
  47.                     index = strings.indexOf(command[2]);
  48.                     if (isValid(strings, index)) {
  49.                         strings.set(index, command[1]);
  50.                     }
  51.  
  52.                     break;
  53.  
  54.                 default:
  55.                     throw new IllegalStateException("Unknown" + command[0]);
  56.             }
  57.             input = sc.nextLine();
  58.         }
  59.         for (String word : strings)
  60.             System.out.print(word + " ");
  61.     }
  62.     private static boolean isValid(List<String> strings, int index) {
  63.         return 0 <= index && index < strings.size();
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement