borovaneca

Inventory

Apr 5th, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. package Demo;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. public class Inventory {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.  
  14.         List<String> items = new ArrayList<>(List.of(scanner.readLine().split(", ")));
  15.         String command;
  16.         while (!"Craft!".equals(command = scanner.readLine())) {
  17.             String[] tokens = command.split("\\s-\\s");
  18.  
  19.             if (tokens[0].equals("Collect")) {
  20.                 if (!items.contains(tokens[1])) {
  21.                     items.add(tokens[1]);
  22.                 }
  23.  
  24.             } else if (tokens[0].equals("Drop")) {
  25.                 items.remove(tokens[1]);
  26.  
  27.             } else if (tokens[0].equals("Combine Items")) {
  28.                 String[] oldAndNew = tokens[1].split(":");
  29.                 if (items.contains(oldAndNew[0])) {
  30.                     int index = items.indexOf(oldAndNew[0]) + 1;
  31.                     items.add(index, oldAndNew[1]);
  32.                 }
  33.  
  34.             } else if (tokens[0].equals("Renew")) {
  35.                 if (items.contains(tokens[1])) {
  36.                     items.remove(tokens[1]);
  37.                     items.add(tokens[1]);
  38.                 }
  39.             }
  40.         }
  41.         System.out.println(String.join(", ", items));
  42.     }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment