Advertisement
desislava_topuzakova

FlowerStore

May 15th, 2022
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 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 RegularExam
  8. {
  9. internal class FlowerStore
  10. {
  11. private string name;
  12. private List<Flower> flowers;
  13.  
  14. public FlowerStore(string name)
  15. {
  16. Name = name;
  17. flowers = new List<Flower>();
  18. }
  19.  
  20. public string Name
  21. {
  22. get { return name; }
  23. set
  24. {
  25. if (value.Length < 6)
  26. {
  27. throw new ArgumentException("Invalid flower store name!");
  28. }
  29. name = value;
  30. }
  31. }
  32.  
  33. public void AddFlower(Flower flower)
  34. {
  35. flowers.Add(flower);
  36. }
  37.  
  38. public bool SellFlower(Flower flower)
  39. {
  40. foreach (Flower checkFlower in flowers)
  41. {
  42. if (checkFlower.Type == flower.Type && checkFlower.Color == flower.Color && checkFlower.Price == flower.Price)
  43. {
  44. flowers.Remove(checkFlower);
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50.  
  51. public double CalculateTotalPrice()
  52. {
  53. return flowers.Select(e => e.Price).Sum();
  54. }
  55.  
  56. public Flower GetFlowerWithHighestPrice()
  57. {
  58. return flowers.OrderByDescending(e => e.Price).First();
  59. }
  60.  
  61. public Flower GetFlowerWithLowestPrice()
  62. {
  63. return flowers.OrderBy(e => e.Price).First();
  64. }
  65.  
  66. public void RenameFlowerStore(string newName)
  67. {
  68. Name = newName;
  69. }
  70.  
  71. public void SellAllFlowers()
  72. {
  73. flowers.Clear();
  74. }
  75.  
  76. public override string ToString()
  77. {
  78. var sb = new StringBuilder();
  79.  
  80. if (flowers.Count() > 0)
  81. {
  82. sb.Append($"Flower store {name} has {flowers.Count} flower/s:\n");
  83. sb.Append(string.Join("\n", flowers));
  84. }
  85. else
  86. {
  87. sb.Append($"Flower store {name} has no available flowers.");
  88.  
  89. }
  90. return sb.ToString();
  91. }
  92. }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement