desislava_topuzakova

Investor

Jun 17th, 2022
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace StockMarket
  7. {
  8. public class Investor
  9. {
  10. //полета (характеристики)
  11. private List<Stock> portfolio;
  12. private string fullName;
  13. private string emailAddress;
  14. private decimal moneyToInvest;
  15. private string brokerName;
  16.  
  17. //getters and setters
  18. public List<Stock> Portfolio { get => portfolio; set => portfolio = value; }
  19. public string FullName { get => fullName; set => fullName = value; }
  20. public string EmailAddress { get => emailAddress; set => emailAddress = value; }
  21. public decimal MoneyToInvest { get => moneyToInvest; set => moneyToInvest = value; }
  22. public string BrokerName { get => brokerName; set => brokerName = value; }
  23.  
  24. //constructor
  25. public Investor(string fullName, string emailAddress, decimal moneyToInvest, string brokerName)
  26. {
  27. Portfolio = new List<Stock>();
  28. FullName = fullName;
  29. EmailAddress = emailAddress;
  30. MoneyToInvest = moneyToInvest;
  31. BrokerName = brokerName;
  32. }
  33.  
  34. //methods
  35. public int Count
  36. {
  37. get => this.portfolio.Count;
  38. }
  39.  
  40. public void BuyStock(Stock stock)
  41. {
  42. if (stock.MarketCapitalization >= 10000 && moneyToInvest >= stock.PricePerShare)
  43. {
  44. this.portfolio.Add(stock);
  45. MoneyToInvest -= stock.PricePerShare;
  46. }
  47. }
  48.  
  49. public string SellStock(string companyName, decimal sellPrice)
  50. {
  51. foreach (Stock stock in this.portfolio)
  52. {
  53. if (stock.CompanyName == companyName)
  54. {
  55. //намирам дадената стока
  56. //не мога да я продам: продажната цена < цена за споделяне
  57. if (sellPrice < stock.PricePerShare)
  58. {
  59. return "Cannot sell " + companyName + ".";
  60. }
  61. else
  62. {
  63. //продавам
  64. this.portfolio.Remove(stock);
  65. MoneyToInvest += sellPrice;
  66. return companyName + " was sold.";
  67.  
  68. }
  69. }
  70. }
  71.  
  72. //не намирам такава стока
  73. return companyName + " does not exist.";
  74. }
  75.  
  76. public Stock FindStock(string companyName)
  77. {
  78. foreach (Stock stock in this.portfolio)
  79. {
  80. if (stock.CompanyName == companyName)
  81. {
  82. return stock;
  83. }
  84. }
  85.  
  86. return null;
  87. }
  88.  
  89. public Stock FindBiggestCompany()
  90. {
  91. if (this.portfolio.Count == 0)
  92. {
  93. return null;
  94. }
  95.  
  96. //вариант 1:
  97. return this.Portfolio
  98. .OrderByDescending(x => x.MarketCapitalization)
  99. .FirstOrDefault();
  100.  
  101.  
  102.  
  103. //вариант 2:
  104. /*Stock maxStock = null;
  105. decimal maxValue = 0; //най-големия market capitalization
  106. foreach(Stock stock in this.portfolio)
  107. {
  108. if (stock.MarketCapitalization > maxValue)
  109. {
  110. maxValue = stock.MarketCapitalization;
  111. maxStock = stock;
  112. }
  113. }
  114.  
  115. return maxStock;*/
  116. }
  117.  
  118. public string InvestorInformation()
  119. {
  120. StringBuilder sb = new StringBuilder();
  121.  
  122. sb.AppendLine($"The investor {FullName} with a broker {BrokerName} has stocks:");
  123. foreach (Stock stock in this.portfolio)
  124. {
  125. sb.AppendLine(stock.ToString());
  126. }
  127. return sb.ToString();
  128. }
  129.  
  130.  
  131.  
  132.  
  133. }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment