Advertisement
yanchevilian

Inventory from 05. Programming Fundamentals Mid Exam

Mar 31st, 2021
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _03._Inventory
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             List<string> collectingItems = new List<string>(Console.ReadLine().Split(", ", StringSplitOptions.RemoveEmptyEntries));
  11.             List<string> commandArr = new List<string>();
  12.             while (true)
  13.             {
  14.                 commandArr = new List<string>(Console.ReadLine().Split(" - ", StringSplitOptions.RemoveEmptyEntries));
  15.                 string command = commandArr[0].ToLower();
  16.                 if (command.ToLower() == "craft!")
  17.                 {
  18.                     break;
  19.                 }
  20.                 string item = commandArr[1];
  21.                 int index = 0;
  22.                 if (collectingItems.Contains(item))
  23.                 {
  24.                     index = collectingItems.IndexOf(item);
  25.                 }
  26.                 switch (command)
  27.                 {
  28.                     case "collect":
  29.                         string collectItem = commandArr[1];
  30.                         if (collectingItems.Contains(collectItem))
  31.                         {
  32.                             continue;
  33.                         }
  34.                         else if (0 <= index && index < collectingItems.Count)
  35.                         {
  36.                             collectingItems.Add(collectItem);
  37.                         }
  38.                         break;
  39.                     case "drop":
  40.                         string dropItem = commandArr[1];
  41.                         if (collectingItems.Contains(dropItem) && (0 <= index && index < collectingItems.Count))
  42.                         {
  43.                             collectingItems.Remove(dropItem);
  44.                         }
  45.                         else
  46.                         {
  47.                             continue;
  48.                         }
  49.                         break;
  50.                     case "combine items":
  51.                         string[] items = commandArr[1].Split(":", StringSplitOptions.RemoveEmptyEntries);
  52.                         string oldItem = items[0];
  53.                         string newItem = items[1];
  54.                         int currentIndex = collectingItems.IndexOf(oldItem);
  55.                         if (collectingItems.Contains(oldItem) && (currentIndex >= 0 && currentIndex < collectingItems.Count))
  56.                         {
  57.                             collectingItems.Insert(currentIndex + 1, newItem);
  58.                         }
  59.                         else
  60.                         {
  61.                             continue;
  62.                         }
  63.                         break;
  64.                     case "renew":
  65.                         string renewItem = commandArr[1];
  66.                         if (collectingItems.Contains(renewItem) && (0 <= index && index < collectingItems.Count))
  67.                         {
  68.                             collectingItems.Remove(renewItem);
  69.                             collectingItems.Add(renewItem);
  70.                         }
  71.                         break;
  72.                     default:
  73.                         break;
  74.                 }
  75.             }
  76.             Console.WriteLine(string.Join(", ", collectingItems));
  77.         }
  78.     }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement