Advertisement
mirozspace

Pr3e0416

Apr 16th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Pr3 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<String> shopsForVisit = Arrays.stream(scanner.nextLine().split(" "))
  11.                 .collect(Collectors.toList());
  12.         int n = Integer.parseInt(scanner.nextLine());
  13.  
  14.         for (int i = 0; i < n; i++) {
  15.             String[] arrCommand = scanner.nextLine().split(" ");
  16.             switch (arrCommand[0]) {
  17.                 case "Include":
  18.                     include(shopsForVisit, arrCommand[1]);
  19.                     break;
  20.                 case "Visit":
  21.                     visit(shopsForVisit, arrCommand[1], Integer.parseInt(arrCommand[2]));
  22.                     break;
  23.                 case "Prefer":
  24.                     prefer(shopsForVisit, Integer.parseInt(arrCommand[1]), Integer.parseInt(arrCommand[2]));
  25.                     break;
  26.                 case "Place":
  27.                     place(shopsForVisit, arrCommand[1], Integer.parseInt(arrCommand[2]));
  28.                     break;
  29.             }
  30.         }
  31.         System.out.println("Shops left:");
  32.         System.out.println(printList(shopsForVisit));
  33.     }
  34.  
  35.     private static void include(List<String> shopForVisit, String shop) {
  36.         shopForVisit.add(shop);
  37.     }
  38.  
  39.     private static void visit(List<String> shopForVisit, String firstOrLast, int numberOfShops) {
  40.         if (shopForVisit.size() >= numberOfShops) {
  41.             if (firstOrLast.equals("first")) {
  42.                 for (int i = 0; i < numberOfShops; i++) {
  43.                     shopForVisit.remove(0);
  44.                 }
  45.             } else if (firstOrLast.equals("last")) {
  46.                 for (int i = 0; i < numberOfShops; i++) {
  47.                     shopForVisit.remove(shopForVisit.size() - 1);
  48.                 }
  49.             }
  50.         }
  51.     }
  52.  
  53.     private static void prefer(List<String> shopForVisit, int index1, int index2) {
  54.  
  55.         if (index1 >= 0 && index1 < shopForVisit.size() - 1
  56.                 && index2 >= 0 && index2 < shopForVisit.size() - 1) {
  57.  
  58.             String shop1 = shopForVisit.get(index1);
  59.             String shop2 = shopForVisit.get(index2);
  60.             shopForVisit.set(index1, shop2);
  61.             shopForVisit.set(index2, shop1);
  62.  
  63.         }
  64.     }
  65.  
  66.     private static void place(List<String> shopForVisit, String shop, int indexShop) {
  67.         if (indexShop >= 0 && indexShop < shopForVisit.size() - 1) {
  68.             shopForVisit.add(indexShop + 1, shop);
  69.         }
  70.     }
  71.  
  72.     private static String printList(List<String> result) {
  73.         return result.toString().replaceAll("[\\[,\\]]", "");
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement