Advertisement
desislava_topuzakova

Agency.cs

Apr 21st, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6.  
  7. namespace RegularExamOOP
  8. {
  9. internal class Agency
  10. {
  11. private string name;
  12. private List<RealEstate> realEstates;
  13.  
  14. public Agency(string name)
  15. {
  16. Name = name;
  17. realEstates = new List<RealEstate>();
  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 agency name!");
  28. }
  29. name = value;
  30. }
  31. }
  32.  
  33. public void AddRealEstate(RealEstate realEstate)
  34. {
  35. realEstates.Add(realEstate);
  36. }
  37.  
  38. public bool SellRealEstate(RealEstate realEstate)
  39. {
  40. foreach (RealEstate checkRealEstate in realEstates)
  41. {
  42. if (checkRealEstate.Address == realEstate.Address && checkRealEstate.Price == realEstate.Price)
  43. {
  44. realEstates.Remove(checkRealEstate);
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50.  
  51. public double CalculateTotalPrice()
  52. {
  53. return realEstates.Select(e => e.Price).Sum();
  54. }
  55.  
  56. public RealEstate GetRealEstateWithHighestPrice()
  57. {
  58. return realEstates.OrderByDescending(e => e.Price).First();
  59. }
  60.  
  61. public RealEstate GetRealEstateWithLowestPrice()
  62. {
  63. return realEstates.OrderBy(e => e.Price).First();
  64. }
  65.  
  66. public void RenameAgency(string newName)
  67. {
  68. Name = newName;
  69. }
  70.  
  71. public void SellAllRealEstates()
  72. {
  73. realEstates.Clear();
  74. }
  75.  
  76. public override string ToString()
  77. {
  78. var sb = new StringBuilder();
  79.  
  80. if (realEstates.Count() > 0)
  81. {
  82. sb.Append($"Agency {name} has {realEstates.Count} real estate/s:\n");
  83. sb.Append(string.Join("\n", realEstates));
  84. }
  85. else
  86. {
  87. sb.Append($"Agency {name} has no available real estates.");
  88.  
  89. }
  90. return sb.ToString();
  91. }
  92. }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement