Advertisement
Threed90

StockMarket

Oct 23rd, 2021
1,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.38 KB | None | 0 0
  1. // main:
  2. using System;
  3.  
  4. namespace StockMarket
  5. {
  6.     public class StartUp
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             // Sample Code Usage:
  11.  
  12.             // Initialize Investor
  13.             Investor investor = new Investor("Peter Lynch", "p.lynch@gmail.com", 2000m, "Fidelity");
  14.  
  15.             // Initialize Stock
  16.             Stock ibmStock = new Stock("IBM", "Arvind Krishna", 138.50m, 5000);
  17.  
  18.             // Print a stock
  19.             Console.WriteLine(ibmStock.ToString());
  20.  
  21.             // Company: IBM
  22.             // Director: Arvind Krishna
  23.             // Price per share: $138.50
  24.             // Market capitalization: $692500.00
  25.  
  26.             // Buy a stock
  27.             investor.BuyStock(ibmStock);
  28.  
  29.             // Sell a stock
  30.             Console.WriteLine(investor.SellStock("IBM", 150.00m)); // "IBM was sold."
  31.                                                                    // Add stocks
  32.             Stock teslaStock = new Stock("Tesla", "Elon Musk", 743.17m, 6520);
  33.             Stock amazonStock = new Stock("Amazon", "Jeff Bezos", 3457.17m, 8500);
  34.             Stock twitterStock = new Stock("Twitter", "Jack Dorsey", 59.66m, 11200);
  35.             Stock blizzardStock = new Stock("Activision Blizzard", "Bobby Kotick", 78.53m, 5520);
  36.  
  37.             // Buy more stocks
  38.             investor.BuyStock(teslaStock);
  39.             investor.BuyStock(amazonStock);
  40.             investor.BuyStock(twitterStock);
  41.             investor.BuyStock(blizzardStock);
  42.  
  43.             // FindBiggestCompany
  44.             Console.WriteLine(investor.FindBiggestCompany());
  45.  
  46.             // Company: Tesla
  47.             // Director: Elon Musk
  48.             // Price per share: $743.17
  49.             // Market capitalization: $4845468.40
  50.  
  51.             // Print investor information
  52.             Console.WriteLine(investor.InvestorInformation());
  53.  
  54.             // The investor Peter Lynch with a broker Fidelity has stocks:
  55.             // Company: Tesla
  56.             // Director: Elon Musk
  57.             // Price per share: $743.17
  58.             // Market capitalization: $4845468.40
  59.             // Company: Twitter
  60.             // Director: Jack Dorsey
  61.             // Price per share: $59.66
  62.             // Market capitalization: $668192.00
  63.             // Company: Activision Blizzard
  64.             // Director: Bobby Kotick
  65.             // Price per share: $78.53
  66.             // Market capitalization: $433485.60
  67.  
  68.         }
  69.     }
  70. }
  71.  
  72.  
  73. // class Stock:
  74. using System;
  75.  
  76. namespace StockMarket
  77. {
  78.     public class Stock
  79.     {
  80.         public Stock(string companyName, string director, decimal pricePerShare, int totalNumberOfShares)
  81.         {
  82.             CompanyName = companyName;
  83.             Director = director;
  84.             PricePerShare = pricePerShare;
  85.             TotalNumberOfShares = totalNumberOfShares;
  86.         }
  87.  
  88.         public string CompanyName { get; set; }
  89.         public string Director { get; set; }
  90.         public decimal PricePerShare { get; set; }
  91.         public int TotalNumberOfShares { get; set; }
  92.         public decimal MarketCapitalization
  93.         {
  94.             get
  95.             {
  96.                 return PricePerShare * TotalNumberOfShares;
  97.             }
  98.         }
  99.  
  100.         public override string ToString()
  101.         {
  102.             return $"Company: {CompanyName}{Environment.NewLine}" +
  103.                 $"Director: {Director}{Environment.NewLine}" +
  104.                 $"Price per share: ${PricePerShare}{Environment.NewLine}" +
  105.                 $"Market capitalization: ${ MarketCapitalization}";
  106.  
  107.         }
  108.     }
  109. }
  110.  
  111.  
  112. //repository class Investor:
  113.  
  114. using System.Collections.Generic;
  115. using System.Linq;
  116. using System.Text;
  117.  
  118. namespace StockMarket
  119. {
  120.     public class Investor
  121.     {
  122.         private readonly Dictionary<string, Stock> portfolio;
  123.  
  124.         public Investor(string fullName, string emailAddress, decimal moneyToInvest, string brokerName)
  125.         {
  126.             FullName = fullName;
  127.             EmailAddress = emailAddress;
  128.             MoneyToInvest = moneyToInvest;
  129.             BrokerName = brokerName;
  130.             this.portfolio = new Dictionary<string, Stock>();
  131.         }
  132.  
  133.         public string FullName { get; set; }
  134.         public string EmailAddress { get; set; }
  135.         public decimal MoneyToInvest { get; set; }
  136.         public string BrokerName { get; set; }
  137.  
  138.         public int Count
  139.         {
  140.             get
  141.             {
  142.                 return portfolio.Count;
  143.             }
  144.         }
  145.  
  146.  
  147.         public void BuyStock(Stock stock)
  148.         {
  149.             if(stock.MarketCapitalization > 10000 &&
  150.                 this.MoneyToInvest >= stock.PricePerShare)
  151.             {
  152.                 if (!portfolio.ContainsKey(stock.CompanyName))
  153.                 {
  154.                     portfolio.Add(stock.CompanyName, stock);
  155.  
  156.                     this.MoneyToInvest -= stock.PricePerShare;
  157.                 }
  158.             }
  159.         }
  160.  
  161.         public string SellStock(string companyName, decimal sellPrice)
  162.         {
  163.             if (portfolio.ContainsKey(companyName))
  164.             {
  165.                 if(sellPrice < portfolio[companyName].PricePerShare)
  166.                 {
  167.                     return $"Cannot sell {companyName}.";
  168.                 }
  169.                 else
  170.                 {
  171.                     this.MoneyToInvest += sellPrice;
  172.                     portfolio.Remove(companyName);
  173.                     return $"{companyName} was sold.";
  174.                 }
  175.             }
  176.             else
  177.             {
  178.                 return $"{companyName} does not exist.";
  179.             }
  180.         }
  181.  
  182.         public Stock FindStock(string companyName)
  183.         {
  184.             if (portfolio.ContainsKey(companyName))
  185.             {
  186.                 return portfolio[companyName];
  187.             }
  188.             else
  189.             {
  190.                 return null;
  191.             }
  192.         }
  193.  
  194.         public Stock FindBiggestCompany()
  195.         {
  196.             return portfolio.OrderByDescending(x => x.Value.MarketCapitalization).FirstOrDefault().Value;
  197.         }
  198.  
  199.         public string InvestorInformation()
  200.         {
  201.             StringBuilder sb = new StringBuilder();
  202.  
  203.             sb.AppendLine($"The investor {this.FullName} with a broker {this.BrokerName} has stocks:");
  204.  
  205.             foreach (var stock in portfolio)
  206.             {
  207.                 sb.AppendLine(stock.Value.ToString());
  208.             }
  209.  
  210.             return sb.ToString().TrimEnd();
  211.         }
  212.     }
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement