RGAlex

O3. Inventory / O5. Mid Exam Retake

Feb 26th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package MidExam.O5;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class O3Inventory {
  10.     public static void main(String[] args) {
  11.         Scanner scan = new Scanner(System.in);
  12.  
  13.         List<String> items = new ArrayList<>(Arrays.asList(scan.nextLine().split(", ")));
  14.  
  15.         String command = scan.nextLine();
  16.  
  17.         while (!command.equals("Craft!")) {
  18.  
  19.             String[] tokens = command.split(" - ");
  20.  
  21.  
  22.             if (tokens[0].equals("Collect")) {
  23.                 if (items.contains(tokens[1])) {
  24.  
  25.                 } else {
  26.                     String collectItem = tokens[1];
  27.                     items.add(collectItem);
  28.                 }
  29.             } else if (tokens[0].equals("Drop")) {
  30.                 if (items.contains(tokens[1])) {
  31.                     items.remove(tokens[1]);
  32.                 } else {
  33.  
  34.                 }
  35.             } else if (tokens[0].equals("Combine Items")) {
  36.                 String[] item = tokens[1].split(":");
  37.                 String oldItem = item[0];
  38.                 String newItem = item[1];
  39.  
  40.                 for (int i = 0; i < items.size(); i++) {
  41.  
  42.                     String currentItem = items.get(i);
  43.  
  44.                     if (currentItem.equals(oldItem)) {
  45.                         items.add(i + 1, newItem);
  46.                         break;
  47.                     }
  48.                 }
  49.             } else if (tokens[0].equals("Renew")) {
  50.                 if (items.contains(tokens[1])) {
  51.                     String renewItem = tokens[1];
  52.  
  53.                     items.remove(tokens[1]);
  54.                     items.add(renewItem);
  55.                 }
  56.             }
  57.  
  58.             command = scan.nextLine();
  59.         }
  60.  
  61.         for (int i = 0; i <= items.size() - 1; i++) {
  62.  
  63.             if (i != items.size() - 1) {
  64.                 System.out.printf("%s, ", items.get(i));
  65.             } else if (i == items.size() - 1) {
  66.                 System.out.printf("%s ", items.get(i));
  67.             }
  68.         }
  69.  
  70.     }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment