Advertisement
beinsaduno

03. Inventory

Feb 17th, 2021
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 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 E03Inventory {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<String> list = Arrays.stream(scanner.nextLine().split(", "))
  11.                 .collect(Collectors.toList());
  12.  
  13.         String command = scanner.nextLine();
  14.  
  15.         while (!"Craft!".equals(command)) {
  16.             String[] commandParts = command.split(" - ");
  17.             String firstCommand = commandParts[0];
  18.             String item = commandParts[1];
  19.             int index = list.indexOf(item);
  20.  
  21.             switch (firstCommand) {
  22.                 case "Collect":
  23.                     if (list.contains(item)) {
  24.                         break;
  25.                     } else if (index < 0) {
  26.                         list.add(item);
  27.                     }
  28.                     break;
  29.                 case "Drop":
  30.                     if (0 <= index && index < list.size()) {
  31.                         list.remove(item);
  32.                     }
  33.                     break;
  34.                 case "Combine Items":
  35.                     String[] splitCombine = item.split(":");
  36.                     String oldItem = splitCombine[0];
  37.                     String newItem = splitCombine[1];
  38.                     index = list.indexOf(oldItem);
  39.                     if (list.contains(oldItem)) {
  40.                         for (int i = 0; i < list.size(); i++) {
  41.                             if (list.get(i).equals(oldItem) && 0 <= index && index < list.size()) {
  42.                                 if (i == list.size() - 1) {
  43.                                     list.add(newItem);
  44.                                     break;
  45.                                 }
  46.                                 list.set(i + 1, newItem);
  47.                             }
  48.                         }
  49.                     }
  50.                     break;
  51.                 case "Renew":
  52.                     if (list.contains(item) && 0 <= index && index < list.size()) {
  53.                         list.remove(item);
  54.                         list.add(item);
  55.                     }
  56.                     break;
  57.                 default:
  58.                     break;
  59.             }
  60.             command = scanner.nextLine();
  61.         }
  62.         System.out.println(list.toString().replaceAll("[\\[\\]]", ""));
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement