Esempio n. 1
0
        public void UpdateDatabase(IDataInteraction dataProvider, IOutputProvider outputProvider, ILogger logger, int sellerId, int buyerId, int shareId, decimal sharePrice, int purchaseQuantity)
        {
            try
            {
                if (sellerId == 0 || buyerId == 0 || shareId == 0 || sharePrice == 0 || purchaseQuantity == 0)
                {
                    logger.Write("Incorrect data from randomizer");
                    throw new Exception("Incorrect data from randomizer");
                }

                var sellerToChange = dataProvider.GetTrader(sellerId);

                if (sellerToChange != null)
                {
                    sellerToChange.Balance += sharePrice * purchaseQuantity;
                }

                var buyerToChange = dataProvider.GetTrader(buyerId);

                if (buyerToChange != null)
                {
                    buyerToChange.Balance -= sharePrice * purchaseQuantity;
                }

                var sellerShareRecordToChange = dataProvider.GetPortfolio(sellerId, shareId);

                if (sellerShareRecordToChange != null)
                {
                    sellerShareRecordToChange.Quantity -= purchaseQuantity;

                    if (sellerShareRecordToChange.Quantity == 0)
                    {
#if DEBUG
                        outputProvider.WriteLine("Removed share record with 0 quantity");
#endif
                        logger.Write("Removed share record with 0 quantity");

                        dataProvider.RemovePortfolio(sellerShareRecordToChange);
                    }
                }

                if (dataProvider.GetPortfoliosCount(buyerId, shareId) > 0)
                {
                    var buyerShareRecordToChange = dataProvider.GetPortfolio(buyerId, shareId);

                    if (buyerShareRecordToChange != null)
                    {
                        buyerShareRecordToChange.Quantity += purchaseQuantity;
                    }
                }
                else
                {
#if DEBUG
                    outputProvider.WriteLine("Add new record to portfolio");
#endif
                    logger.Write("Add new record to portfolio");

                    dataProvider.AddPortfolio(buyerId, shareId, purchaseQuantity);
                }

                var transaction = dataProvider.AddTransaction(buyerId, sellerId, shareId, sharePrice, purchaseQuantity);

                dataProvider.SaveChanges();

                string message = "Buyer = " + transaction.BuyerId + " Seller = " + transaction.SellerId + " Share name = " + transaction.ShareId +
                                 " Quantity = " + transaction.Quantity + " Price per share = " + transaction.PricePerShare +
                                 " Transaction total = " + transaction.PricePerShare * transaction.Quantity + " Timestamp = " + transaction.DateTime;

                outputProvider.WriteLine(message);
                logger.Write(message);
            }
            catch (Exception e)
            {
                outputProvider.WriteLine(e.Message);
                logger.Write(e.Message);
            }
        }