Advertisement
desislava_topuzakova

HatShop.cs

May 28th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace RegularExam
  7. {
  8. internal class HatShop
  9. {
  10. private string name;
  11. private List<Hat> hats;
  12.  
  13. public HatShop(string name)
  14. {
  15. Name = name;
  16. hats = new List<Hat>();
  17. }
  18.  
  19. public string Name
  20. {
  21. get { return name; }
  22. set
  23. {
  24. if (value.Length < 6)
  25. {
  26. throw new ArgumentException("Invalid hat shop name!");
  27. }
  28. name = value;
  29. }
  30. }
  31.  
  32. public void AddHat(Hat Hat)
  33. {
  34. this.hats.Add(Hat);
  35. }
  36.  
  37. public bool SellHat(Hat Hat)
  38. {
  39. foreach (Hat checkHat in this.hats)
  40. {
  41. if (checkHat.Type == Hat.Type && checkHat.Color == Hat.Color && checkHat.Price == Hat.Price)
  42. {
  43. this.hats.Remove(checkHat);
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49.  
  50. public double CalculateTotalPrice()
  51. {
  52. return this.hats.Select(e => e.Price).Sum();
  53. }
  54.  
  55. public Hat GetHatWithHighestPrice()
  56. {
  57. return this.hats.OrderByDescending(e => e.Price).First();
  58. }
  59.  
  60. public Hat GetHatWithLowestPrice()
  61. {
  62. return this.hats.OrderBy(e => e.Price).First();
  63. }
  64.  
  65. public void RenameHatShop(string newName)
  66. {
  67. Name = newName;
  68. }
  69.  
  70. public void SellAllHats()
  71. {
  72. this.hats.Clear();
  73. }
  74.  
  75. public override string ToString()
  76. {
  77. var sb = new StringBuilder();
  78.  
  79. if (this.hats.Count() > 0)
  80. {
  81. sb.Append($"Hat shop {name} has {this.hats.Count} hat/s:\n");
  82. sb.Append(string.Join("\n", this.hats));
  83. }
  84. else
  85. {
  86. sb.Append($"Hat shop {name} has no available hats.");
  87.  
  88. }
  89. return sb.ToString();
  90. }
  91. }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement