예제 #1
0
        static void Main(string[] args)
        {
            var pd           = new PortfolioDisplay();
            var myPortfolio  = new Portfolio(pd);
            var hisPortfolio = new Portfolio(pd);

            var googleStock = new Stock("Google", 200M);
            var vestasStock = new Stock("Vestas", 45M);

            myPortfolio.AddStock(googleStock, 50);
            myPortfolio.AddStock(vestasStock, 95);
            hisPortfolio.AddStock(googleStock, 20);

            googleStock.Value = 55M;
            vestasStock.Value = 20M;

            Console.WriteLine($"{nameof(myPortfolio)} has {myPortfolio.Total} value");
            Console.WriteLine($"{nameof(hisPortfolio)} has {hisPortfolio.Total} value");

            while (true)
            {
                StockMarket.RefreshStocks();
                Thread.Sleep(10000);
            }
        }
예제 #2
0
 /// <summary>
 /// The value changed, and the method implemented from interface, this takes a <see cref="Subject"/> Abstraction
 /// and prints the changed value. This method is crucial for the Observer pattern.
 /// </summary>
 /// <para>
 /// Implemented as using push methodology, but doesn't directly use the internal state and only using it retrieve a
 /// notification for a new update and passing itself to the output, therefore using the pull methodology instead.
 /// </para>
 /// <param name="value">This parameter is the internal state of the Subject received. This ensures that the Observer knows the state</param>
 public void ValueChanged(Subject value)
 {
     // Checks global variable if notifications are allowed.
     if (StockMarket.PortfolioNotifications)
     {
         PortfolioDisplay.PrintInformation(this);
     }
 }
예제 #3
0
        /// <summary>
        /// Dummy code to test application
        /// </summary>
        public static void Main()
        {
            var pd           = new PortfolioDisplay();
            var portfolios   = new List <IPortfolio>();
            var myPortfolio  = new Portfolio("MyPortfolio", pd);
            var hisPortfolio = new Portfolio("HisPortfolio", pd);

            portfolios.Add(myPortfolio);
            portfolios.Add(hisPortfolio);


            var googleStock = new Stock("Google", 200.25M);
            var vestasStock = new Stock("Vestas", 150.75M);

            new Thread(UpdateStock);

            myPortfolio.AddStock(googleStock, 51);
            myPortfolio.AddStock(vestasStock, 95);
            hisPortfolio.AddStock(googleStock, 22);

            while (true)
            {
                StockMarket.RefreshStocks();

                Console.WriteLine("Select a portfolio");
                foreach (var portfolio in portfolios)
                {
                    Console.WriteLine($"{portfolio.Name}");
                }

                Console.WriteLine("Type the name of the portfolio to select it");
                var input = Console.ReadLine();
                var index = -1;
                foreach (var portfolio in portfolios)
                {
                    if (input == portfolio.Name)
                    {
                        index = portfolios.IndexOf(portfolio);
                    }
                }

                if (index == -1)
                {
                    continue;
                }
                Console.WriteLine();
                bool cont = true;
                while (cont)
                {
                    Console.WriteLine("Type 'B' to Buy, 'S' To Sell or C to Cancel");
                    char transactionInput = (char)Console.Read();

                    switch (transactionInput)
                    {
                    case 'B':
                        StockMarket.BuyStock(portfolios[index]);
                        break;

                    case 'S':
                        StockMarket.SellStock(portfolios[index]);
                        break;

                    case 'C':
                        cont = false;
                        break;

                    default:
                        Console.WriteLine();
                        break;
                    }
                }
            }
        }
예제 #4
0
 public Portfolio(PortfolioDisplay portfolioDisplay)
 {
     PortfolioDisplay = portfolioDisplay ?? throw new ArgumentNullException(nameof(portfolioDisplay));
 }
예제 #5
0
 public void ValueChanged(ISubject value)
 {
     PortfolioDisplay.PrintInformation(this);
 }