Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class EasterShopping {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> listShops = Arrays.stream(scanner.nextLine().split("\\s+")).collect(Collectors.toList());
- int n = Integer.parseInt(scanner.nextLine());
- for (int i = 0; i < n; i++) {
- String input = scanner.nextLine();
- String[] tokens = input.split("\\s+");
- String command = tokens[0];
- switch (command) {
- case "Include": {
- String shop = tokens[1];
- listShops.add(shop);
- break;
- }
- case "Visit": {
- String firtsLast = tokens[1];
- int numberOfShop = Integer.parseInt(tokens[2]);
- if (numberOfShop <= listShops.size()) {
- if (firtsLast.equals("first")) {
- listShops.subList(0, numberOfShop).clear();
- // тук не трябва да премахваш само един елемент, както ти го беше направил.
- // трябва от началото до number of shop
- } else if (firtsLast.equals("last")) {
- listShops.subList(listShops.size() - numberOfShop, listShops.size()).clear();
- }
- }
- break;
- }
- case "Prefer": {
- int shopIndex1 = Integer.parseInt(tokens[1]);
- int shopIndex2 = Integer.parseInt(tokens[2]);
- if (checkShopIndexExist(shopIndex1, shopIndex2, listShops.size())) {
- Collections.swap(listShops, shopIndex1, shopIndex2); // лесен начин за смяна местата на индекси :) наскоро го открих
- }
- break;
- }
- case "Place": {
- String shop = tokens[1];
- int shopIndex = Integer.parseInt(tokens[2]);
- if (shopIndex + 1 < listShops.size()) {
- listShops.add(shopIndex + 1, shop);
- }
- break;
- }
- }
- }
- System.out.println("Shops left:");
- System.out.println(String.join(" ", listShops));
- }
- private static boolean checkShopIndexExist(int shopIndex1, int shopIndex2, int size) {
- return shopIndex1 >= 0 && shopIndex1 < size && shopIndex2 >= 0 && shopIndex2 < size; // проврка за валидност на индексите
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement