Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Pr3 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> shopsForVisit = Arrays.stream(scanner.nextLine().split(" "))
- .collect(Collectors.toList());
- int n = Integer.parseInt(scanner.nextLine());
- for (int i = 0; i < n; i++) {
- String[] arrCommand = scanner.nextLine().split(" ");
- switch (arrCommand[0]) {
- case "Include":
- include(shopsForVisit, arrCommand[1]);
- break;
- case "Visit":
- visit(shopsForVisit, arrCommand[1], Integer.parseInt(arrCommand[2]));
- break;
- case "Prefer":
- prefer(shopsForVisit, Integer.parseInt(arrCommand[1]), Integer.parseInt(arrCommand[2]));
- break;
- case "Place":
- place(shopsForVisit, arrCommand[1], Integer.parseInt(arrCommand[2]));
- break;
- }
- }
- System.out.println("Shops left:");
- System.out.println(printList(shopsForVisit));
- }
- private static void include(List<String> shopForVisit, String shop) {
- shopForVisit.add(shop);
- }
- private static void visit(List<String> shopForVisit, String firstOrLast, int numberOfShops) {
- if (shopForVisit.size() >= numberOfShops) {
- if (firstOrLast.equals("first")) {
- for (int i = 0; i < numberOfShops; i++) {
- shopForVisit.remove(0);
- }
- } else if (firstOrLast.equals("last")) {
- for (int i = 0; i < numberOfShops; i++) {
- shopForVisit.remove(shopForVisit.size() - 1);
- }
- }
- }
- }
- private static void prefer(List<String> shopForVisit, int index1, int index2) {
- if (index1 >= 0 && index1 < shopForVisit.size() - 1
- && index2 >= 0 && index2 < shopForVisit.size() - 1) {
- String shop1 = shopForVisit.get(index1);
- String shop2 = shopForVisit.get(index2);
- shopForVisit.set(index1, shop2);
- shopForVisit.set(index2, shop1);
- }
- }
- private static void place(List<String> shopForVisit, String shop, int indexShop) {
- if (indexShop >= 0 && indexShop < shopForVisit.size() - 1) {
- shopForVisit.add(indexShop + 1, shop);
- }
- }
- private static String printList(List<String> result) {
- return result.toString().replaceAll("[\\[,\\]]", "");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement