Advertisement
CR7CR7

coffee

Oct 25th, 2022
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. 1import java.util.Arrays;
  2. 2import java.util.Collections;
  3. 3import java.util.List;
  4. 4import java.util.Scanner;
  5. 5import java.util.stream.Collectors;
  6. 6.   
  7. 7public class CoffeeLover {
  8. 8.      public static void main(String[] args) {
  9. 9.          Scanner scan = new Scanner(System.in);
  10. 10.  
  11. 11.         List<String> coffees = Arrays.stream(scan.nextLine().split("\\s+")).collect(Collectors.toList());
  12. 12.         int commandsCount = Integer.parseInt(scan.nextLine());
  13. 13.  
  14. 14.         for (int i = 0; i < commandsCount; i++) {
  15. 15.             String[] command = scan.nextLine().split("\\s+");
  16. 16.  
  17. 17.             switch (command[0]) {
  18. 18.                 case "Include":
  19. 19.                     coffees.add(command[1]);
  20. 20.                     break;
  21. 21.                 case "Remove":
  22. 22.                     int removedCoffeesCnt = Integer.parseInt(command[2]);
  23. 23.                     if (removedCoffeesCnt <= coffees.size()) {
  24. 24.                         if (command[1].equals("first")) {
  25. 25.                             for (int g = 0; g < removedCoffeesCnt; g++) {
  26. 26.                                 coffees.remove(0);
  27. 27.                             }
  28. 28.                         } else if (command[1].equals("last")) {
  29. 29.                             for (int g = 0; g < removedCoffeesCnt; g++) {
  30. 30.                                 coffees.remove(coffees.size() - 1);
  31. 31.                             }
  32. 32.                         }
  33. 33.                     }
  34. 34.                     break;
  35. 35.                 case "Prefer":
  36. 36.                     int firstIndex = Integer.parseInt(command[1]);
  37. 37.                     int secondIndex = Integer.parseInt(command[2]);
  38. 38.  
  39. 39.                     try {
  40. 40.                         String firstCoffee = coffees.get(firstIndex);
  41. 41.                         coffees.set(firstIndex, coffees.get(secondIndex));
  42. 42.                         coffees.set(secondIndex, firstCoffee);
  43. 43.                     } catch (Exception e) {}
  44. 44.                     break;
  45. 45.                 case "Reverse":
  46. 46.                     Collections.reverse(coffees);
  47. 47.                     break;
  48. 48.             }
  49. 49.         }
  50. 50.         System.out.println("Coffees:");
  51. 51.         for (String coffee:coffees) {
  52. 52.             System.out.print(coffee + " ");
  53. 53.         }
  54. 54.     }
  55. 55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement