Teo_Yanchev

C# Fundamentals - 03. Inventory - MidExam 29.02.2020 G1

Oct 27th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03._Inventory
  6. {
  7. class Invenotry
  8. {
  9. static void Main()
  10. {
  11. List<string> inventory = Console.ReadLine()
  12. .Split(", ")
  13. .ToList();
  14.  
  15. string command = Console.ReadLine();
  16. while (command != "Craft!")
  17. {
  18. string[] currentCommand = command.Split(" - ");
  19. string instruction = currentCommand[0];
  20.  
  21.  
  22. if (instruction == "Collect")
  23. {
  24. string item = currentCommand[1];
  25.  
  26. if (!inventory.Contains(item))
  27. {
  28. inventory.Add(item);
  29. }
  30. }
  31.  
  32. else if (instruction == "Drop")
  33. {
  34. string item = currentCommand[1];
  35.  
  36. if (inventory.Contains(item))
  37. {
  38. inventory.Remove(item);
  39. }
  40. }
  41.  
  42. else if (instruction == "Combine Items")
  43. {
  44. string[] items = currentCommand[1].Split(':');
  45. string oldItem = items[0];
  46. string newItem = items[1];
  47.  
  48. if (inventory.Contains(oldItem))
  49. {
  50. int index = inventory.IndexOf(oldItem) + 1;
  51. inventory.Insert(index, newItem);
  52. }
  53. // NB!! NB !!
  54. }
  55.  
  56. else if (instruction == "Renew")
  57. {
  58. string item = currentCommand[1];
  59.  
  60. if (inventory.Contains(item))
  61. {
  62. inventory.Remove(item);
  63. inventory.Add(item);
  64. }
  65. }
  66.  
  67. command = Console.ReadLine();
  68. }
  69.  
  70. Console.WriteLine(string.Join(", ", inventory));
  71. }
  72. }
  73. }
  74.  
Add Comment
Please, Sign In to add comment