Advertisement
BojidarDosev

zad2

Feb 16th, 2025
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9.  
  10. Scanner scan = new Scanner(System.in);
  11.  
  12. List<String> coffees = Arrays.asList(scan.nextLine().split(" ")).stream().collect(Collectors.toList());
  13.  
  14. int n = Integer.parseInt(scan.nextLine());
  15.  
  16. while (n> 0) {
  17. String[] command = scan.nextLine().split(" ");
  18. switch (command[0]) {
  19. case "Include":
  20. coffees.addLast(command[1]);
  21. break;
  22. case "Remove":
  23. int count = Integer.parseInt(command[2]);
  24. while(count>0) {
  25. if (command[1].equals("first") && count <= coffees.size()) {
  26. coffees.removeFirst();
  27. } else if (command[1].equals("last") && count <= coffees.size()) {
  28. coffees.removeLast();
  29. }
  30. count--;
  31. }
  32. break;
  33. case "Prefer":
  34. int index1 = Integer.parseInt(command[1]);
  35. int index2 = Integer.parseInt(command[2]);
  36. if(index1 == coffees.size() || index2 == coffees.size()) {
  37. break;
  38. }
  39. if (index1 >= 0 && index1 < coffees.size() && index2 >= 0 && index2 < coffees.size()) {
  40. // Swap elements correctly
  41. String temp = coffees.get(index1);
  42. coffees.set(index1, coffees.get(index2));
  43. coffees.set(index2, temp);
  44. }
  45. break;
  46. case "Reverse":
  47. Collections.reverse(coffees);
  48. break;
  49. }
  50.  
  51. n--;
  52. }
  53.  
  54. System.out.println("Coffees:");
  55.  
  56. for (String coffee : coffees) {
  57. System.out.print(coffee + " ");
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement