Advertisement
Guest User

03. Inventory

a guest
Feb 15th, 2021
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Demo
  7. {
  8.     class Program
  9.     {
  10.  
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             // Read The Input
  15.  
  16.             List<string> inventory = Console.ReadLine()
  17.                 .Split(", ", StringSplitOptions.RemoveEmptyEntries)
  18.                 .ToList();
  19.  
  20.  
  21.             while (true)
  22.             {
  23.                 string line = Console.ReadLine();
  24.                 if (line == "Craft!")
  25.                 {
  26.                     break;
  27.                 }
  28.  
  29.                 string[] command = line
  30.                     .Split(" - ", StringSplitOptions.RemoveEmptyEntries)
  31.                     .ToArray();
  32.                 string firstCommand = command[0];
  33.                
  34.  
  35.                 switch (firstCommand)
  36.                 {
  37.                     case "Collect":
  38.                         if (!inventory.Contains(command[1]))
  39.                         {
  40.                             inventory.Add(command[1]);
  41.                         }
  42.                         break;
  43.  
  44.                     case "Drop":
  45.                         if (inventory.Contains(command[1]))
  46.                         {
  47.                             inventory.Remove(command[1]);
  48.                         }
  49.                         break;
  50.  
  51.                     case "Combine Items":
  52.                         string oldItem = string.Empty;
  53.                         string newItem = string.Empty;
  54.  
  55.                         for (int i = 1; i < command.Length; i++)
  56.                         {
  57.                             string[] combineItem = command[i]
  58.                                 .Split(":", StringSplitOptions.RemoveEmptyEntries);
  59.  
  60.                             oldItem = combineItem[0];
  61.                             newItem = combineItem[1];  
  62.                         }
  63.  
  64.                         if (inventory.Contains(oldItem))
  65.                         {
  66.                             for (int i = 0; i < inventory.Count; i++)
  67.                             {
  68.                                 if (inventory[i] == oldItem)
  69.                                 {
  70.                                    int index = i + 1;
  71.                                     if (index > 0)
  72.                                     {
  73.                                         inventory.Insert(index, newItem);
  74.                                     }
  75.                                     break;
  76.                                 }
  77.                             }
  78.                         }
  79.                         break;
  80.  
  81.                     case "Renew":
  82.                         if (inventory.Contains(command[1]))
  83.                         {
  84.                             string firstElement = inventory[0];
  85.  
  86.                             inventory.Add(command[1]);
  87.                             inventory.Remove(firstElement);
  88.                         }
  89.                         break;
  90.                 }
  91.  
  92.             }
  93.  
  94.             Console.WriteLine(string.Join(", ", inventory));
  95.            
  96.  
  97.         }
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement