Advertisement
veronikaaa86

03. Inventory

Oct 19th, 2022
1,423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. package examPrep;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class P03Inventory {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         List<String> inventoryList = Arrays.stream(scanner.nextLine().split(", ")).collect(Collectors.toList());
  13.  
  14.         String inputLine = scanner.nextLine();
  15.         while (!inputLine.equals("Craft!")) {
  16.             String[] commandsLine = inputLine.split(" - ");
  17.             String command = commandsLine[0];
  18.  
  19.             switch (command) {
  20.                 case "Collect":
  21.                     String addItem = commandsLine[1];
  22.                     if (!inventoryList.contains(addItem)) {
  23.                         inventoryList.add(addItem);
  24.                     }
  25.                     break;
  26.                 case "Drop":
  27.                     String removeItem = commandsLine[1];
  28.                     inventoryList.remove(removeItem);
  29.                     break;
  30.                 case "Combine Items":
  31.                     String[] items = commandsLine[1].split(":");
  32.                     String oldItem = items[0];
  33.                     String newItem = items[1];
  34.  
  35.                     if (inventoryList.contains(oldItem)) {
  36.                         int indexOldItem = inventoryList.indexOf(oldItem);
  37.                         inventoryList.add(indexOldItem + 1, newItem);
  38.                     }
  39.                     break;
  40.                 case "Renew":
  41.                     String renewItem = commandsLine[1];
  42.                     if (inventoryList.contains(renewItem)) {
  43.                         inventoryList.remove(renewItem);
  44.                         inventoryList.add(renewItem);
  45.                     }
  46.                     break;
  47.             }
  48.  
  49.             inputLine = scanner.nextLine();
  50.         }
  51.  
  52.         System.out.println(String.join(", ", inventoryList));
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement