Guest User

Untitled

a guest
Dec 13th, 2019
179
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. cage.cs:
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Rabbits
  8. {
  9. public class Cage
  10. {
  11. private List<Rabbit> data;
  12.  
  13. public Cage(string name, int capacity)
  14. {
  15. this.Name = name;
  16. this.Capacity = capacity;
  17. this.data = new List<Rabbit>();
  18. }
  19. public string Name { get; private set; }
  20. public int Capacity { get; private set; }
  21. public int Count => this.data.Count();//getter Count
  22.  
  23. //adds an entity to the data if ima mqsto//////
  24. public void Add(Rabbit rabbit)
  25. {
  26. if (this.data.Count < this.Capacity)
  27. {
  28. data.Add(rabbit);
  29. }
  30.  
  31. }
  32. //removes a rabbit by given name, if such exists, and returns bool///
  33. public bool RemoveRabbit(string name)
  34. {
  35. return this.data.Remove(this.data.FirstOrDefault(x => x.Name == name));
  36. }
  37. //removes all rabbits by given species//
  38. public void RemoveSpecies(string species)
  39. {
  40. this.data.RemoveAll(x => x.Species == species);
  41. }
  42. //sell (set its Available property to false without removing it from the collection) the first rabbit with the given name, also return the rabbit///
  43. public Rabbit SellRabbit(string name)//pyrviq zaek s dadeno ime
  44. {
  45. Rabbit sell = this.data.FirstOrDefault(x => x.Name == name);
  46. sell.Available = false;
  47. return sell;
  48. }
  49. //sells (set their Available property to false without removing them from the collection) and returns all rabbits from that species as an array///
  50. public Rabbit[] SellRabbitsBySpecies(string species)
  51. {
  52. List<Rabbit> rabbits = new List<Rabbit>();
  53. rabbits = this.data.Where(x => x.Species == species).ToList();
  54. for (int i = 0; i < rabbits.Count; i++)
  55. {
  56. rabbits[i].Available = false;
  57. }
  58. return rabbits.ToArray();
  59. }
  60. ///returns a string in the following format, including only not sold rabbits//vkl samo neprodadeni zaici
  61. public string Report()
  62. {
  63. StringBuilder sb = new StringBuilder();
  64.  
  65. sb.AppendLine($"Rabbits available at {this.Name}:");
  66.  
  67. foreach (var rab in this.data)
  68. {
  69. if (rab.Available == true)
  70. {
  71. sb.AppendLine($"{rab}");
  72. }
  73. }
  74. return sb.ToString().TrimEnd();
  75. }
  76.  
  77. }
  78. }
  79. ------------------------------------------------------------------------------
  80. rabbit.cs:
  81. using System;
  82. using System.Collections.Generic;
  83. using System.Text;
  84.  
  85. namespace Rabbits
  86. {
  87. public class Rabbit
  88. {
  89. public Rabbit(string name, string species)
  90. {
  91. this.Name = name;
  92. this.Species = species;
  93. this.Available = true;
  94. }
  95.  
  96. public string Name { get; private set; }
  97. public string Species { get; private set; }
  98. public bool Available { get; set; }
  99.  
  100. public override string ToString()
  101. {
  102. return $"Rabbit ({ this.Species}): {this.Name}";
  103. }
  104. }
  105. }
  106. ----------------------------------------------------------------------------------------------
  107. startUp.cs:
  108. namespace Rabbits
  109. {
  110. using System;
  111. using System.Linq;
  112.  
  113. public class StartUp
  114. {
  115. public static void Main()
  116. {
  117. //Initialize the repository (Cage)
  118. Cage cage = new Cage("Wildness", 20);
  119. //Initialize entity
  120. Rabbit rabbit = new Rabbit("Fluffy", "Blanc de Hotot");
  121. //Print Rabbit
  122. Console.WriteLine(rabbit); //Rabbit (Blanc de Hotot): Fluffy
  123.  
  124. //Add Rabbit
  125. cage.Add(rabbit);
  126. Console.WriteLine(cage.Count); //1
  127. //Remove Rabbit
  128. cage.RemoveRabbit("Rabbit Name"); //false
  129.  
  130. Rabbit secondRabbit = new Rabbit("Bunny", "Brazilian");
  131. Rabbit thirdRabbit = new Rabbit("Jumpy", "Cashmere Lop");
  132. Rabbit fourthRabbit = new Rabbit("Puffy", "Cashmere Lop");
  133. Rabbit fifthRabbit = new Rabbit("Marlin", "Brazilian");
  134.  
  135. //Add Rabbits
  136. cage.Add(secondRabbit);
  137. cage.Add(thirdRabbit);
  138. cage.Add(fourthRabbit);
  139. cage.Add(fifthRabbit);
  140.  
  141. //Sell Rabbit by name
  142. Console.WriteLine(cage.SellRabbit("Bunny")); //Rabbit (Brazilian): Bunny
  143. //Sell Rabbit by species
  144. Rabbit[] soldSpecies = cage.SellRabbitsBySpecies("Cashmere Lop");
  145. Console.WriteLine(string.Join(", ", soldSpecies.Select(f => f.Name))); //Jumpy, Puffy
  146.  
  147. Console.WriteLine(cage.Report());
  148. //Rabbits available at Wildness:
  149. //Rabbit (Blanc de Hotot): Fluffy
  150. //Rabbit (Brazilian): Marlin
  151. }
  152. }
  153. }
RAW Paste Data