Advertisement
IrinaIgnatova

Easter Shopping

Jun 26th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.         String[] line = scanner.nextLine().split(" +");
  14.         List<String> shops = new ArrayList<>();
  15.         for (int i = 0; i < line.length; i++) {
  16.             shops.add(line[i]);
  17.         }
  18.  
  19.         Integer count = Integer.parseInt(scanner.nextLine());
  20.  
  21.         for (int i = 0; i < count; i++) {
  22.             String[] input = scanner.nextLine().split(" +");
  23.             String command = input[0];
  24.  
  25.             if (command.contains("Include")) {
  26.                 String shop = input[1];
  27.                 shops.add(shop);
  28.  
  29.             } else if (command.contains("Visit")) {
  30.                 String firstOrLAst = input[1];
  31.                 int countToRemove = Integer.parseInt(input[2]);
  32.                 if(countToRemove<=shops.size()){
  33.                     if (firstOrLAst.equals("first")) {
  34.                         for (int j = 0; j < countToRemove; j++) {
  35.                             for (int k = 0; k < shops.size(); k++) {
  36.                                 if (k>= 0 && k < shops.size()) {//j<shop
  37.                                     shops.remove(k);
  38.                                     k--;
  39.                                     break; //obhojdame spisyka
  40.                                 }
  41.                             }
  42.                         }
  43.                     } else if (firstOrLAst.equals("last")) {
  44.                         for (int j = 0; j < countToRemove; j++) {
  45.                             for (int k = shops.size() - 1; k >= 0; k--) {
  46.                                 if (k >= shops.size()-1 && k < shops.size()) {//k<shop
  47.                                     shops.remove(k);
  48.                                     break;
  49.                                 }
  50.                             }
  51.                         }
  52.                     }
  53.                 }
  54.  
  55.  
  56.             } else if (command.contains("Prefer")) {
  57.                 int index1 = Integer.parseInt(input[1]);
  58.                 int index2 = Integer.parseInt(input[2]);
  59.                 if (index1 >= 0 && index1 < shops.size()) {
  60.                     if (index2 >= 0 && index2 < shops.size()) {
  61.                         Collections.swap(shops, index1, index2);
  62.                     }
  63.                 }
  64.                // System.out.println(shops);
  65.  
  66.  
  67.             }else if(command.contains("Place")){
  68.                 String shop=input[1];
  69.                 int index=Integer.valueOf(input[2])+1;
  70.                 if(index>=0&&index<shops.size()){
  71.                     shops.add(index,shop);
  72.                 }
  73.  
  74.             }
  75.  
  76.         }
  77.  
  78.             System.out.println("Shops left:");
  79.         for (String shop : shops) {
  80.             System.out.print(shop+" ");
  81.         }
  82.  
  83.  
  84.     }
  85.  
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement