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 Main {
- public static void main(String[] args) {
- final Scanner scanner = new Scanner(System.in);
- final List<String> shops = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
- final int numberOfCommands = Integer.parseInt(scanner.nextLine());
- for (int i = 0; i < numberOfCommands; i++) {
- final String command = scanner.nextLine();
- if (command.contains("Include")) {
- final String shopName = command.substring(command.indexOf(" ")).trim();
- shops.add(shopName);
- } else if (command.contains("Visit")) {
- final String subCommand = command.replace("Visit", "").trim();
- final String firstOrLast = subCommand.substring(0, command.indexOf(" ")).trim();
- final int numberOfShopsToVisit = Integer.parseInt(subCommand.substring(subCommand.indexOf(" ") + 1, subCommand.length()));
- for (int j = 0; j < numberOfShopsToVisit; j++) {
- if(numberOfShopsToVisit > shops.size()) {
- break;
- }
- if (firstOrLast.equals("first")) {
- shops.remove(0);
- } else {
- shops.remove(shops.size() - 1);
- }
- }
- } else if (command.contains("Prefer")) {
- final String subCommand = command.replace("Prefer", "").trim();
- final int firstShopIndex = Integer.parseInt(subCommand.substring(0, subCommand.length() - 1).trim());
- final int secondShopIndex = Integer.parseInt(subCommand.substring(subCommand.indexOf(" ") + 1));
- if(firstShopIndex <= shops.size() -1 && secondShopIndex <= shops.size() -1) {
- final String firstShop = shops.get(firstShopIndex);
- final String secondShop = shops.get(secondShopIndex);
- shops.set(secondShopIndex, firstShop);
- shops.set(firstShopIndex, secondShop);
- }
- } else if (command.contains("Place")) {
- final String subCommand = command.replace("Place", "").trim();
- final String shopName = subCommand.substring(0, subCommand.indexOf(" ")).trim();
- final int shopIndex = Integer.parseInt(subCommand.substring(subCommand.indexOf(" ") + 1));
- if(shops.size() - 1 >= shopIndex) {
- shops.add(shopIndex + 1, shopName);
- }
- }
- }
- System.out.println("Shops left:");
- System.out.println(shops.toString().replaceAll("[\\[,\\]]", ""));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement