Esempio n. 1
0
        public void ShouldMoveClientToOrangeZoneAfterDeal()
        {
            StockType stockType = new StockType()
            {
                Name = "TestType", Cost = 100
            };
            Stock stock = new Stock()
            {
                Type = stockType
            };
            Client buyer = new Client()
            {
                Name = "Dummy1", Balance = 100
            };
            Client seller = new Client()
            {
                Name = "Dummy2", Balance = 0
            };

            seller.ClientStocks.Add(stock);
            Deal deal = new Deal()
            {
                Seller = seller, Buyer = buyer, Stock = stock, Sum = stock.Type.Cost
            };

            buisnessService.NewDeal(deal);

            Assert.AreEqual(Zone.Orange, buyer.ClientZone);
        }
Esempio n. 2
0
        public void Run()
        {
            IsContinue = true;
            using (var dbContext = new TPTUserDbContext(connectionString))
            {
                var buisnessService = new BuisnessService(dbContext);

                var stockTypes = buisnessService.GetStockTypes().ToList();
                loggerService.Info("Printing info about all clients");
                foreach (var client in buisnessService.GetAllClients())
                {
                    loggerService.Info(client.ToString());
                }

                loggerService.Info("The programm starts in 5 seconds. To exit press 1. Every stock cost will change on each iteration");
                Thread.Sleep(5000);

                Random rnd = new Random();

                while (IsContinue)
                {
                    var  buyer   = buisnessService.GetAllClients().Where(b => b.Balance > 0).GetRandom();
                    var  seller  = buisnessService.GetAllClients().Where(c => c.Id != buyer.Id).Where(c => c.ClientStocksForSale.Count > 0).GetRandom();
                    var  stock   = seller.ClientStocksForSale.GetRandom();
                    var  sum     = stock.Type.Cost;
                    Deal newDeal = new Deal
                    {
                        Buyer  = buyer,
                        Seller = seller,
                        Stock  = stock,
                        Sum    = sum
                    };
                    loggerService.Info($"Deal begins. Buyer is - {buyer.Name} {buyer.Surname}, balance: {buyer.Balance}, seller - {seller.Name} {seller.Surname}, balance: {seller.Balance}\nbuyer buys and seller sells stock of {stock.Type.Name} company for the {stock.Type.Cost}$");
                    buisnessService.NewDeal(newDeal);
                    if (buyer.ClientZone == Zone.Orange)
                    {
                        loggerService.Warn("Attention! Client's balance is 0. Client's ability to buy stocks is suspended");
                    }
                    if (buyer.ClientZone == Zone.Black)
                    {
                        loggerService.Warn("Attention! Client's balance below 0. Client's ability to buy stocks is suspended");
                    }
                    loggerService.Info($"Deal concluded. {buyer.Name}'s(buyer) balance now - {buyer.Balance}, {seller.Name}'s (seller) balance now - {seller.Balance}\n");

                    foreach (var type in stockTypes)
                    {
                        if (type.Cost < 1)
                        {
                            type.Cost += rnd.Next(1, 8);
                        }
                        type.Cost += rnd.Next(-3, 3);
                    }

                    Thread.Sleep(5000);
                }
                loggerService.Info("Shutting down");
            }
        }