public void ShouldEditClientStockAmount() { // Arrange ClientStockService clientStockService = new ClientStockService(clientStockTableRepository); clientStockTableRepository.ContainsByPK(1, 1).Returns(true); int clientId = 1; int stockId = 1; int amount = 5; ClientStock clientStock = new ClientStock() { ClientID = 1, StockID = 1, Quantity = 1000, }; // Act clientStockService.EditClientStocksAmount(clientId, stockId, amount); // Assert clientStockTableRepository.Received(1).FindByPK(clientId, stockId); clientStock.Quantity += amount; clientStockTableRepository.Received(1).SaveChanges(); }
public override bool ContainsDTO(TEntity entity) { ClientStock clientStock = entity; return (this.db.ClientStocks .Any(c => c.ClientID == clientStock.ClientID && c.StockID == clientStock.StockID)); }
public ClientStock GetRandomClientStock(int clientID) { Random random = new Random(); var clientStocks = this.GetAllClientStocks(clientID); int stocksAmount = clientStocks.Count(); if (stocksAmount == 0) { throw new NullReferenceException("There are no stocks to select from"); } int number = random.Next(0, stocksAmount - 1); ClientStock clientStock = clientStocks.ToList()[number]; return(clientStock); }
public void AddClientStockToDB(ClientStockInfo args) { var clientstockToAdd = new ClientStock() { ClientID = args.ClientId, StockID = args.StockId, Quantity = args.Amount }; if (this.unitOfWork.ClientStocks.Get(c => c.ClientID == args.ClientId && c.StockID == args.StockId).Count() != 0) { throw new ArgumentException("This clientstock exists. Can't continue"); } ; this.unitOfWork.ClientStocks.Add(clientstockToAdd); this.unitOfWork.Save(); }
public void AddClientStock(ClientStockInfo args) { var clientstockToAdd = new ClientStock() { ClientID = args.ClientId, StockID = args.StockId, Quantity = args.Amount }; if (this.tableRepository.ContainsDTO(clientstockToAdd)) { throw new ArgumentException("This clientstock exists. Can't continue"); } ; this.tableRepository.Add(clientstockToAdd); this.tableRepository.SaveChanges(); }
public void EditClientStocksAmount(int clientId, int stockId, int amountToAdd) { if (!this.tableRepository.ContainsByPK(clientId, stockId)) { AddClientStock(new ClientStockInfo() { ClientId = clientId, StockId = stockId, Amount = amountToAdd }); } else { ClientStock clientStock = this.GetEntityByCompositeID(clientId, stockId); clientStock.Quantity += amountToAdd; this.tableRepository.SaveChanges(); } }
public void EditClientStocksAmount(int clientId, int stockId, int amountToAdd) { if (this.unitOfWork.ClientStocks.Get(c => c.StockID == stockId && c.ClientID == clientId).Count() == 0) { AddClientStockToDB(new ClientStockInfo() { ClientId = clientId, StockId = stockId, Amount = amountToAdd }); } else { ClientStock clientStock = this.GetEntityByCompositeID(clientId, stockId); clientStock.Quantity += amountToAdd; this.unitOfWork.ClientStocks.Update(clientStock); this.unitOfWork.Save(); } }
public void RunTraiding() { int loopcount = 10; for (int i = 0; i < loopcount; i++) { int amountInLotForSale = 10; //Select random saler Client saler = GetRandomClient(); //Select random stock for saler ClientStock clstock = GetRandomClientStock(saler.ClientID); if (clstock == null || clstock.Quantity < 10) { continue; } //determine amount for sale int lotsAmount = clstock.Quantity / amountInLotForSale; Random random = new Random(); int amountForSale = random.Next(1, lotsAmount) * amountInLotForSale; orderService.AddOrder(new OrderInfo() { ClientId = clstock.ClientID, StockId = clstock.StockID, Quantity = amountForSale, OrderType = OrderInfo.OrdType.Sale }); this.logger.Info($"Order for sale stock {clstock.StockID} for client {clstock.ClientID} has been added to DB"); Order salerOrder = orderService.LastOrder(); Client customer; do { customer = GetRandomClient(); }while (customer.ClientID == saler.ClientID); orderService.AddOrder(new OrderInfo() { ClientId = customer.ClientID, StockId = clstock.StockID, Quantity = amountForSale, OrderType = OrderInfo.OrdType.Purchase }); this.logger.Info($"Order for purchasing stock {clstock.StockID} for client {customer.ClientID} has been added to DB"); Order customerOrder = orderService.LastOrder(); DateTime dealDateTime = DateTime.Now; decimal dealPrice = priceHistoryService.GetStockPriceByDateTime(new PriceArguments() { StockId = clstock.StockID, DateTimeLookUp = dealDateTime }); clientService.EditClientBalance(saler.ClientID, (dealPrice * amountForSale)); this.logger.Info($"Client {saler.ClientID} balance has been increased by {(dealPrice * amountForSale)}"); clientService.EditClientBalance(customer.ClientID, (-1 * dealPrice * amountForSale)); this.logger.Info($"Client {customer.ClientID} balance has been reduced by {(dealPrice * amountForSale)}"); clientStockService.EditClientStocksAmount(saler.ClientID, clstock.StockID, -amountForSale); this.logger.Info($"Client {saler.ClientID} stock {salerOrder.StockID} amount has been reduced on {amountForSale}"); clientStockService.EditClientStocksAmount(customer.ClientID, clstock.StockID, amountForSale); this.logger.Info($"Client {customer.ClientID} stock {salerOrder.StockID} amount has been increased on {amountForSale}"); transactionHistoryService.AddTransactionInfo(new TransactionInfo() { CustomerOrderId = customerOrder.OrderID, SalerOrderId = salerOrder.OrderID, TrDateTime = dealDateTime }); this.logger.Info($"Transaction has been added to DB"); orderService.SetIsExecuted(salerOrder); this.logger.Info($"Saler's order {salerOrder.OrderID} status has been set as 'IsExecuted'"); orderService.SetIsExecuted(customerOrder); this.logger.Info($"Customer's order {customerOrder.OrderID} status has been set as 'IsExecuted'"); this.logger.Info($"Deal is finished"); this.SimulatePriceChange(salerOrder.StockID, dealPrice, dealDateTime); this.logger.Info($"Stock {salerOrder.StockID} price has been changed'"); Thread.Sleep(10000); } }