ilianrusev

Technology Fundamentals Mid Exam - 10 March 2019 Group 2 - 3

Apr 15th, 2019
61
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. List<String> words = Arrays.stream(scanner.nextLine().split(" "))
  13. .collect(Collectors.toList());
  14.  
  15. String inputCommand = scanner.nextLine();
  16.  
  17. String second = "";
  18. String first = "";
  19. System.out.println();
  20.  
  21. while (!inputCommand.equals("Stop")) {
  22.  
  23. List<String> commands = Arrays.stream(inputCommand.split("\\s+"))
  24. .collect(Collectors.toList());
  25.  
  26.  
  27. if (commands.size() == 2){
  28. first = commands.get(1);
  29. }
  30. if (commands.size() == 3){
  31. first = commands.get(1);
  32. second = commands.get(2);
  33.  
  34. }
  35.  
  36. if (commands.contains("Delete")) {
  37. int indexDelete = Integer.parseInt(first);
  38. if ((words.size() > indexDelete + 1) && (indexDelete + 1 >= 0)){
  39. words.remove((Integer.parseInt(first) + 1));
  40. }
  41. }
  42.  
  43. if (commands.contains("Replace")) {
  44. if (words.contains(second)) {
  45. words.set(words.indexOf(second), first);
  46.  
  47. }
  48. }
  49.  
  50. if (commands.contains("Swap")) {
  51. if (words.contains(first) && words.contains(second)) {
  52. Collections.swap(words,words.indexOf(first),words.indexOf(second));
  53.  
  54. }
  55. }
  56.  
  57. if (commands.contains("Put")) {
  58. int firstInt = Integer.parseInt(second);
  59. if (((firstInt - 1) >= 0) && ((firstInt - 1) <= words.size())){
  60. words.add(firstInt-1,first);
  61. }
  62.  
  63. }
  64.  
  65. if (commands.contains("Sort")) {
  66. Collections.sort(words);
  67. Collections.reverse(words);
  68.  
  69. }
  70. inputCommand = scanner.nextLine();
  71. }
  72.  
  73. System.out.println(words.toString().replaceAll("[\\[\\],]", ""));
  74.  
  75. }
  76. }
RAW Paste Data Copied