Advertisement
beinsaduno

03. Inventory 100/100

Feb 17th, 2021
900
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 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) + 1;
  39.                     if (list.contains(oldItem)) {
  40.                         list.add(index, newItem);
  41.                     }
  42.                     break;
  43.                 case "Renew":
  44.                     if (list.contains(item) && 0 <= index && index < list.size()) {
  45.                         list.remove(item);
  46.                         list.add(item);
  47.                     }
  48.                     break;
  49.                 default:
  50.                     break;
  51.             }
  52.             command = scanner.nextLine();
  53.         }
  54.         System.out.println(list.toString().replaceAll("[\\[\\]]", ""));
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement