Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main:
- using System;
- namespace StockMarket
- {
- public class StartUp
- {
- static void Main(string[] args)
- {
- // Sample Code Usage:
- // Initialize Investor
- Investor investor = new Investor("Peter Lynch", "p.lynch@gmail.com", 2000m, "Fidelity");
- // Initialize Stock
- Stock ibmStock = new Stock("IBM", "Arvind Krishna", 138.50m, 5000);
- // Print a stock
- Console.WriteLine(ibmStock.ToString());
- // Company: IBM
- // Director: Arvind Krishna
- // Price per share: $138.50
- // Market capitalization: $692500.00
- // Buy a stock
- investor.BuyStock(ibmStock);
- // Sell a stock
- Console.WriteLine(investor.SellStock("IBM", 150.00m)); // "IBM was sold."
- // Add stocks
- Stock teslaStock = new Stock("Tesla", "Elon Musk", 743.17m, 6520);
- Stock amazonStock = new Stock("Amazon", "Jeff Bezos", 3457.17m, 8500);
- Stock twitterStock = new Stock("Twitter", "Jack Dorsey", 59.66m, 11200);
- Stock blizzardStock = new Stock("Activision Blizzard", "Bobby Kotick", 78.53m, 5520);
- // Buy more stocks
- investor.BuyStock(teslaStock);
- investor.BuyStock(amazonStock);
- investor.BuyStock(twitterStock);
- investor.BuyStock(blizzardStock);
- // FindBiggestCompany
- Console.WriteLine(investor.FindBiggestCompany());
- // Company: Tesla
- // Director: Elon Musk
- // Price per share: $743.17
- // Market capitalization: $4845468.40
- // Print investor information
- Console.WriteLine(investor.InvestorInformation());
- // The investor Peter Lynch with a broker Fidelity has stocks:
- // Company: Tesla
- // Director: Elon Musk
- // Price per share: $743.17
- // Market capitalization: $4845468.40
- // Company: Twitter
- // Director: Jack Dorsey
- // Price per share: $59.66
- // Market capitalization: $668192.00
- // Company: Activision Blizzard
- // Director: Bobby Kotick
- // Price per share: $78.53
- // Market capitalization: $433485.60
- }
- }
- }
- // class Stock:
- using System;
- namespace StockMarket
- {
- public class Stock
- {
- public Stock(string companyName, string director, decimal pricePerShare, int totalNumberOfShares)
- {
- CompanyName = companyName;
- Director = director;
- PricePerShare = pricePerShare;
- TotalNumberOfShares = totalNumberOfShares;
- }
- public string CompanyName { get; set; }
- public string Director { get; set; }
- public decimal PricePerShare { get; set; }
- public int TotalNumberOfShares { get; set; }
- public decimal MarketCapitalization
- {
- get
- {
- return PricePerShare * TotalNumberOfShares;
- }
- }
- public override string ToString()
- {
- return $"Company: {CompanyName}{Environment.NewLine}" +
- $"Director: {Director}{Environment.NewLine}" +
- $"Price per share: ${PricePerShare}{Environment.NewLine}" +
- $"Market capitalization: ${ MarketCapitalization}";
- }
- }
- }
- //repository class Investor:
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace StockMarket
- {
- public class Investor
- {
- private readonly Dictionary<string, Stock> portfolio;
- public Investor(string fullName, string emailAddress, decimal moneyToInvest, string brokerName)
- {
- FullName = fullName;
- EmailAddress = emailAddress;
- MoneyToInvest = moneyToInvest;
- BrokerName = brokerName;
- this.portfolio = new Dictionary<string, Stock>();
- }
- public string FullName { get; set; }
- public string EmailAddress { get; set; }
- public decimal MoneyToInvest { get; set; }
- public string BrokerName { get; set; }
- public int Count
- {
- get
- {
- return portfolio.Count;
- }
- }
- public void BuyStock(Stock stock)
- {
- if(stock.MarketCapitalization > 10000 &&
- this.MoneyToInvest >= stock.PricePerShare)
- {
- if (!portfolio.ContainsKey(stock.CompanyName))
- {
- portfolio.Add(stock.CompanyName, stock);
- this.MoneyToInvest -= stock.PricePerShare;
- }
- }
- }
- public string SellStock(string companyName, decimal sellPrice)
- {
- if (portfolio.ContainsKey(companyName))
- {
- if(sellPrice < portfolio[companyName].PricePerShare)
- {
- return $"Cannot sell {companyName}.";
- }
- else
- {
- this.MoneyToInvest += sellPrice;
- portfolio.Remove(companyName);
- return $"{companyName} was sold.";
- }
- }
- else
- {
- return $"{companyName} does not exist.";
- }
- }
- public Stock FindStock(string companyName)
- {
- if (portfolio.ContainsKey(companyName))
- {
- return portfolio[companyName];
- }
- else
- {
- return null;
- }
- }
- public Stock FindBiggestCompany()
- {
- return portfolio.OrderByDescending(x => x.Value.MarketCapitalization).FirstOrDefault().Value;
- }
- public string InvestorInformation()
- {
- StringBuilder sb = new StringBuilder();
- sb.AppendLine($"The investor {this.FullName} with a broker {this.BrokerName} has stocks:");
- foreach (var stock in portfolio)
- {
- sb.AppendLine(stock.Value.ToString());
- }
- return sb.ToString().TrimEnd();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement