Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 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 EasterShopping {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. List<String> listShops = Arrays.stream(scanner.nextLine().split("\\s+")).collect(Collectors.toList());
  12. int n = Integer.parseInt(scanner.nextLine());
  13.  
  14. for (int i = 0; i < n; i++) {
  15. String input = scanner.nextLine();
  16.  
  17. String[] tokens = input.split("\\s+");
  18. String command = tokens[0];
  19. switch (command) {
  20. case "Include": {
  21. String shop = tokens[1];
  22. listShops.add(shop);
  23. break;
  24. }
  25. case "Visit": {
  26. String firtsLast = tokens[1];
  27. int numberOfShop = Integer.parseInt(tokens[2]);
  28.  
  29. if (numberOfShop <= listShops.size()) {
  30. if (firtsLast.equals("first")) {
  31. listShops.subList(0, numberOfShop).clear();
  32. // тук не трябва да премахваш само един елемент, както ти го беше направил.
  33. // трябва от началото до number of shop
  34.  
  35. } else if (firtsLast.equals("last")) {
  36. listShops.subList(listShops.size() - numberOfShop, listShops.size()).clear();
  37.  
  38. }
  39. }
  40. break;
  41. }
  42. case "Prefer": {
  43. int shopIndex1 = Integer.parseInt(tokens[1]);
  44. int shopIndex2 = Integer.parseInt(tokens[2]);
  45.  
  46. if (checkShopIndexExist(shopIndex1, shopIndex2, listShops.size())) {
  47. Collections.swap(listShops, shopIndex1, shopIndex2); // лесен начин за смяна местата на индекси :) наскоро го открих
  48. }
  49. break;
  50. }
  51. case "Place": {
  52. String shop = tokens[1];
  53.  
  54. int shopIndex = Integer.parseInt(tokens[2]);
  55. if (shopIndex + 1 < listShops.size()) {
  56. listShops.add(shopIndex + 1, shop);
  57. }
  58. break;
  59. }
  60. }
  61. }
  62. System.out.println("Shops left:");
  63. System.out.println(String.join(" ", listShops));
  64. }
  65.  
  66. private static boolean checkShopIndexExist(int shopIndex1, int shopIndex2, int size) {
  67. return shopIndex1 >= 0 && shopIndex1 < size && shopIndex2 >= 0 && shopIndex2 < size; // проврка за валидност на индексите
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement