Пример #1
0
        /// <summary>
        /// Buy the specified shares.
        /// </summary>
        /// <param name="tickerSymbol">The ticker symbol.</param>
        /// <param name="shares">The number of shares to buy.</param>
        public void Buy(string tickerSymbol, int shares)
        {
            // get account and equity
            CashAccount account       = new CashAccount(_accountNumber);
            var         equityFactory = EquityFactory.GetInstance();
            Equity      equity        = equityFactory.CreateEquity(tickerSymbol);

            // calculate required credit
            decimal amount  = equity.GetPrice() * shares;
            decimal balance = account.GetAccountBalance();

            if (balance >= amount)
            {
                // withdraw amount
                account.Withdraw(amount);

                // buy stock
                equity.Buy(shares);
                Console.WriteLine("Succesfully bought {0} {1} shares", shares, tickerSymbol);
                Console.WriteLine("New account balance is ${0}", account.GetAccountBalance());
            }
            else
            {
                Console.WriteLine("Insufficient balance ${0}", balance);
            }
        }
Пример #2
0
 /// <summary>
 /// Gets the factory singleton instance.
 /// </summary>
 /// <returns>The singleton instance of the factory.</returns>
 public static EquityFactory GetInstance()
 {
     if (_instance == null)
     {
         _instance = new EquityFactory();
     }
     return(_instance);
 }