Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 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 Main {
  7.  
  8. public static void main(String[] args) {
  9. final Scanner scanner = new Scanner(System.in);
  10. final List<String> shops = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
  11. final int numberOfCommands = Integer.parseInt(scanner.nextLine());
  12.  
  13. for (int i = 0; i < numberOfCommands; i++) {
  14. final String command = scanner.nextLine();
  15.  
  16. if (command.contains("Include")) {
  17. final String shopName = command.substring(command.indexOf(" ")).trim();
  18. shops.add(shopName);
  19. } else if (command.contains("Visit")) {
  20. final String subCommand = command.replace("Visit", "").trim();
  21. final String firstOrLast = subCommand.substring(0, command.indexOf(" ")).trim();
  22. final int numberOfShopsToVisit = Integer.parseInt(subCommand.substring(subCommand.indexOf(" ") + 1, subCommand.length()));
  23.  
  24. for (int j = 0; j < numberOfShopsToVisit; j++) {
  25. if(numberOfShopsToVisit > shops.size()) {
  26.  
  27. break;
  28. }
  29. if (firstOrLast.equals("first")) {
  30. shops.remove(0);
  31. } else {
  32. shops.remove(shops.size() - 1);
  33. }
  34. }
  35. } else if (command.contains("Prefer")) {
  36. final String subCommand = command.replace("Prefer", "").trim();
  37. final int firstShopIndex = Integer.parseInt(subCommand.substring(0, subCommand.length() - 1).trim());
  38. final int secondShopIndex = Integer.parseInt(subCommand.substring(subCommand.indexOf(" ") + 1));
  39.  
  40. if(firstShopIndex <= shops.size() -1 && secondShopIndex <= shops.size() -1) {
  41. final String firstShop = shops.get(firstShopIndex);
  42. final String secondShop = shops.get(secondShopIndex);
  43.  
  44. shops.set(secondShopIndex, firstShop);
  45. shops.set(firstShopIndex, secondShop);
  46. }
  47. } else if (command.contains("Place")) {
  48. final String subCommand = command.replace("Place", "").trim();
  49. final String shopName = subCommand.substring(0, subCommand.indexOf(" ")).trim();
  50. final int shopIndex = Integer.parseInt(subCommand.substring(subCommand.indexOf(" ") + 1));
  51.  
  52. if(shops.size() - 1 >= shopIndex) {
  53. shops.add(shopIndex + 1, shopName);
  54. }
  55. }
  56. }
  57.  
  58. System.out.println("Shops left:");
  59. System.out.println(shops.toString().replaceAll("[\\[,\\]]", ""));
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement