Advertisement
desislava_topuzakova

AuthorSolution

Jun 21st, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Exam
  8. {
  9. public class Fridge
  10. {
  11. private Product head;
  12. private Product tail;
  13. private int count;
  14.  
  15. private class Product
  16. {
  17. private string name;
  18. private Product next;
  19.  
  20. public Product(string name)
  21. {
  22. this.name = name;
  23. }
  24.  
  25. public string Name
  26. {
  27. get { return name; }
  28. set { name = value; }
  29. }
  30.  
  31. public Product Next
  32. {
  33. get { return next; }
  34. set { next = value; }
  35. }
  36.  
  37. public override string ToString()
  38. {
  39. return "Product " + name;
  40. }
  41. }
  42.  
  43. public Fridge()
  44. {
  45.  
  46. }
  47.  
  48. public int Count
  49. {
  50. get { return count; }
  51. set { count = value; }
  52. }
  53.  
  54. public void AddProduct(string ProductName)
  55. {
  56. Product Product = new Product(ProductName);
  57.  
  58. if (this.head == null)
  59. {
  60. this.head = Product;
  61. this.tail = Product;
  62. }
  63. else
  64. {
  65. Product currentLast = this.tail;
  66. currentLast.Next = Product;
  67. this.tail = Product;
  68. }
  69. this.Count++;
  70. }
  71.  
  72. public string[] CookMeal(int start, int end)
  73. {
  74. int count = 0;
  75. List<string> usedProducts = new List<string>();
  76.  
  77. Product current = this.head;
  78.  
  79. while (current != null && count < end)
  80. {
  81. if (count >= start)
  82. {
  83. usedProducts.Add(current.Name);
  84. }
  85.  
  86. current = current.Next;
  87. count++;
  88. }
  89. return usedProducts.ToArray();
  90. }
  91.  
  92. public string RemoveProductByIndex(int index)
  93. {
  94. int count = 0;
  95. Product current = this.head;
  96.  
  97. while (current != null)
  98. {
  99. if (index == 0)
  100. {
  101. this.head = current.Next;
  102. this.Count--;
  103. return current.Name;
  104. }
  105.  
  106. if (count + 1 == index)
  107. {
  108. String name = current.Next.Name;
  109.  
  110. if(current.Next.Next == null)
  111. {
  112. this.tail = current;
  113. this.Count--;
  114. current.Next = null;
  115. return name;
  116. }
  117.  
  118. current.Next = current.Next.Next;
  119. this.Count--;
  120.  
  121. return name;
  122. }
  123.  
  124. current = current.Next;
  125. count++;
  126. }
  127. return null;
  128. }
  129.  
  130. public string RemoveProductByName(string name)
  131. {
  132. Product current = this.head;
  133.  
  134. while (current != null)
  135. {
  136. if (current.Name == name)
  137. {
  138. this.head = current.Next;
  139. this.Count--;
  140. return current.Name;
  141. }
  142.  
  143. if (current.Next.Name == name)
  144. {
  145. if (current.Next.Next == null)
  146. {
  147. this.tail = current;
  148. this.Count--;
  149. current.Next = null;
  150. return name;
  151. }
  152.  
  153. current.Next = current.Next.Next;
  154. this.Count--;
  155.  
  156. return name;
  157. }
  158. current = current.Next;
  159. }
  160. return null;
  161. }
  162.  
  163. public bool CheckProductIsInStock(string name)
  164. {
  165. Product current = this.head;
  166.  
  167. while (current != null)
  168. {
  169. if (current.Name == name)
  170. {
  171. return true;
  172. }
  173.  
  174. current = current.Next;
  175. }
  176. return false;
  177. }
  178.  
  179. public string[] ProvideInformationAboutAllProducts()
  180. {
  181. String[] info = new string[this.Count];
  182. int index = 0;
  183.  
  184. Product current = this.head;
  185.  
  186. while (current != null)
  187. {
  188. info[index++] = current.ToString();
  189. current = current.Next;
  190. }
  191.  
  192. return info;
  193. }
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement