Ejemplo n.º 1
0
        public void Run(BussinesService service)
        {
            Random rand = new Random();

            for (int i = 0; i < rand.Next() % 8; i++)
            {/*/
              * Task.Run(() =>
              * {*/
                List <Client> ClientsList = context.Clients.ToList();

                int Buyer = rand.Next() % context.Clients.Count(), Seller = (Buyer + rand.Next() % context.Clients.Count()) % context.Clients.Count();

                service.CreateDeal(ClientsList[Buyer], ClientsList[Seller]);
                //});

                /*
                 * DealHistory deal = new DealHistory();
                 *
                 * List<Client> ClientsList = context.Clients.ToList();
                 * deal.BuyerId = ClientsList[rand.Next() % context.Clients.Count()].Id;
                 * deal.SellerId = (deal.BuyerId + ClientsList[rand.Next() % context.Clients.Count()].Id) % context.Clients.Count();
                 * deal.ShareId = context.Shares.ToList()[rand.Next() % context.Shares.Count()].ShareId;
                 * deal.Amount = rand.Next() % context.ClientsShares.First(s => s.ClientId == deal.SellerId && s.ShareId == deal.ShareId).Amount;
                 *
                 * context.Add(deal);
                 * context.SaveChanges();
                 */
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            using (var dbContext = new TablePerTypeContext())
            {
                XmlConfigurator.Configure();
                ILog          logger        = LogManager.GetLogger("TextLogger");
                LoggerService loggerService = new LoggerService(logger);

                BussinesService bussinesService = new BussinesService(dbContext, loggerService);

                CreateDB(bussinesService, loggerService);

                GoodTradeProducer producer = new GoodTradeProducer(loggerService, bussinesService);
                producer.OnBalanceChanged += bussinesService.NewTradeMade;
                producer.OnBalanceChanged += bussinesService.NewBalanceForSeller;

                Task.Run(() =>
                {
                    producer.Run(dbContext.Clients.Count());
                });

                Console.ReadLine();
                producer.IsContinue = false;
                Console.WriteLine("Stop trading!");
                Console.ReadKey();
            }
        }
Ejemplo n.º 3
0
        static void RunEmitation(BussinesService bussinesService, Interfaces.ILoggable loggerService)
        {
            var shareholders = bussinesService.GetAllShareholders();

            List <Shareholder> shareholdersList = new List <Shareholder>();

            foreach (var shareholder in shareholders)
            {
                shareholdersList.Add(shareholder);
            }

            Random random = new Random();

            var randomShareholderIndex = random.Next(0, shareholdersList.Count());

            var randomShareholderA = shareholdersList[randomShareholderIndex];

            shareholdersList.RemoveAt(randomShareholderIndex);

            randomShareholderIndex = random.Next(0, shareholdersList.Count());

            var randomShareholderB = shareholdersList[randomShareholderIndex];

            var arrayOfSharesTypes = Enum.GetValues(typeof(SharesTypes));

            var randomSharesType = (SharesTypes)arrayOfSharesTypes.GetValue(random.Next(arrayOfSharesTypes.Length));

            var trade = new Trade
            {
                ShareholderId = randomShareholderA.Id,
                BuyerId       = randomShareholderB.Id,
                Value         = random.Next(1, 30),
                ValueType     = randomSharesType
            };

            bussinesService.RegisterNewTrade(
                trade,
                randomShareholderA,
                randomShareholderB);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("log info: ");
            loggerService.Info($"->{trade.ToString()}<-");

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine(trade);
            Console.ForegroundColor = ConsoleColor.DarkGray;

            Console.WriteLine($"Shareholder's " +
                              $"\n\tbalance value: {bussinesService.GetMostWantedBalanceById(randomShareholderA.Id).BalanceValue.ToString()}" +
                              $"\n\tbalance zone: {bussinesService.GetMostWantedBalanceById(randomShareholderA.Id).BalanceZone.ToString()}");

            Console.WriteLine($"Buyer's " +
                              $"\n\tbalance value: {bussinesService.GetMostWantedBalanceById(randomShareholderB.Id).BalanceValue.ToString()}" +
                              $"\n\tbalance zone: {bussinesService.GetMostWantedBalanceById(randomShareholderB.Id).BalanceZone.ToString()}");
        }
Ejemplo n.º 4
0
        public static void AddNewTraiders(BussinesService bussinesService)
        {
            IDataContex dataContext  = bussinesService.GetDataContex();
            Traider     existTraider = dataContext.Traiders.AsEnumerable().LastOrDefault();

            int newID = 0;

            if (existTraider != null)
            {
                newID = existTraider.ID;
            }

            List <Traider> traiders = new List <Traider>()
            {
                new Traider()
                {
                    ID        = ++newID,
                    FirstName = "Black",
                    Surname   = "Whiteson",
                    PhoneNum  = "34872642891"
                },
                new Traider()
                {
                    ID        = ++newID,
                    FirstName = "Red",
                    Surname   = "Greenson",
                    PhoneNum  = "56454"
                },
                new Traider()
                {
                    ID        = ++newID,
                    FirstName = "Homer(Doy!)",
                    Surname   = "Simpson",
                    PhoneNum  = "1241414"
                },
                new Traider()
                {
                    ID        = ++newID,
                    FirstName = "Marge",
                    Surname   = "Simpson",
                    PhoneNum  = "67868313"
                },
                new Traider()
                {
                    ID        = ++newID,
                    FirstName = "Bart(Ay caramba!)",
                    Surname   = "Simpson",
                    PhoneNum  = "231897193"
                }
            };

            foreach (Traider traider in traiders)
            {
                bussinesService.AddNewTraiderWithStarterPack(traider);
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            XmlConfigurator.Configure();

            Container container = new Container(_ =>
            {
                _.For <BussinesService>().Use <BussinesService>();
                _.For <ILoggerService>().Use <LoggerService>().Ctor <ILog>().Is(LogManager.GetLogger("SampletextLogger"));
                _.For <IDataContex>().Use <MyDbContex>().Ctor <string>().Is(@"Integrated Security=SSPI;Persist Security Info=False;User ID=egor;Initial Catalog=MyBD;Data Source=DESKTOP-I6NBG5H\SQLEXPRESS");
            });


            BussinesService bussinesService = container.GetInstance <BussinesService>();
            LoggerService   loggerService   = new LoggerService(LogManager.GetLogger("SampletextLogger"));

            AddNewTraiders(bussinesService);

            bool isContinue = true;

            Task.Run(() =>
            {
                while (isContinue)
                {
                    RunTradeDay(bussinesService, loggerService);
                    Thread.Sleep(1000);
                }
            });

            Console.ReadKey();
            isContinue = false;

            IQueryable <Traider> zeroBalanceTraiders = bussinesService.GetZeroBalanceTraider();

            if (zeroBalanceTraiders.Count() == 0)
            {
                Console.WriteLine("All traiders are good:)");
            }
            if (zeroBalanceTraiders.Count() != 0)
            {
                foreach (Traider bankrot in zeroBalanceTraiders)
                {
                    Console.WriteLine($"Traider ID: {bankrot.ID} looser, his balanse 0");
                }
            }



            Console.WriteLine("Table created");
            Console.ReadLine();
        }
Ejemplo n.º 6
0
        private static void Main(string[] args)
        {
            XmlConfigurator.Configure();
            var logger        = LogManager.GetLogger("SampleTextLogger");
            var loggerService = new LoggerService(logger);

            IUserInput input = new ConsoleUserInput(ConsoleKey.Escape);

            using (var dbContext = new MyDbContext("Data Source=.;Initial Catalog=StockExchangeDB;Integrated Security=True"))
            {
                var bussinesService = new BussinesService(dbContext);

                Database.SetInitializer(new EfInitializer(bussinesService));

                var stockExchange = new SimpleStockExchange(bussinesService);

                loggerService.RunWithExceptionLogging(() =>
                {
                    bussinesService.RegisterNewStockToClient("NoNameCompany", bussinesService.GetAllClients().GetRandom());
                }, isSilent: true);

                loggerService.Warning("Changing any stock's cost.");
                bussinesService.ChangeStockCost("Telegram", bussinesService.GetStockCost("Telegram") + 100);

                loggerService.Warning("All registered clients:\n");
                foreach (var client in bussinesService.GetAllClients())
                {
                    loggerService.Info(client.ToString());
                }

                input.OnUserInputRecieved += (sender, keyInfo) => { if (keyInfo == ConsoleKey.Q)
                                                                    {
                                                                        stockExchange.IsContinue = false;
                                                                    }
                };

                loggerService.Warning("Opening the stock exchange.");
                loggerService.RunWithExceptionLogging(() => {
                    Task.Run(() =>
                    {
                        stockExchange.Run((IDealInfo dealInfo) => { loggerService.Info($"{dealInfo.Seller} have sold {dealInfo.Stock} to  {dealInfo.Buyer} for {dealInfo.Amount}."); });
                    });
                }, isSilent: false);

                input.ListenToUser();
            }
        }
Ejemplo n.º 7
0
        public void TestSetup()
        {
            this.dataContex      = Substitute.For <IDataContex>();
            this.bussinesService = new BussinesService(this.dataContex);
            this.testTraider     = new Traider()
            {
                ID = 1, FirstName = "testName", Surname = "testSurname", PhoneNum = "testPhoneNum"
            };

            balancesList = new List <TraiderBalance>()
            {
                new TraiderBalance {
                    ID = 1, Balance = 0
                },
                new TraiderBalance {
                    ID = 2, Balance = 0
                },
                new TraiderBalance {
                    ID = 3, SimpleType = 20, PreferenceShare = 30, Balance = 2000
                },
                new TraiderBalance {
                    ID = 4, SimpleType = 20, PreferenceShare = 30, Balance = 2000
                }
            }.AsQueryable();

            dataContex.TraiderBalances.Returns(balancesList);

            traidersList = new List <Traider>()
            {
                new Traider {
                    ID = 1
                },
                new Traider {
                    ID = 2
                },
                new Traider {
                    ID = 3
                },
                new Traider {
                    ID = 4
                }
            }.AsQueryable();

            dataContex.Traiders.Returns(traidersList);
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            using (var dbContext = new TPTContext("Data Source=.;Initial Catalog=TablePerTypeExampleDb2;Integrated Security=True"))
            {
                var bussinesService = new BussinesService(dbContext);

                XmlConfigurator.Configure();

                var logger = LogManager.GetLogger("SampleTextLogger");

                var loggerService = new LoggerService(logger);

                var isContinue = true;
                Task.Run(() =>
                {
                    while (isContinue)
                    {
                        RunEmitation(bussinesService, loggerService);
                        Thread.Sleep(1000);
                    }
                });
                Console.ReadKey();
                isContinue = false;

                var shareholders = bussinesService.GetShareholdersWithZeroBalance();

                if (shareholders.Count() < 1)
                {
                    Console.WriteLine("No shareholders with zero balance");
                }
                else
                {
                    foreach (var shareholder in shareholders)
                    {
                        Console.WriteLine($"id: {shareholder.Id}| balance: 0.00");
                    }
                }
            }

            Console.WriteLine("Table Per Type Done");
            Console.ReadLine();
        }
Ejemplo n.º 9
0
        static void CreateDB(BussinesService bussinesService, ILoggerService loggerService)
        {
            loggerService.Info("Create a database...");

            bussinesService.RegisterNewClient("Johann", "Bach", "839404983", 473637849);
            bussinesService.RegisterNewClient("Wolfgang", "Mozart", "6272893", 2638);
            bussinesService.RegisterNewClient("Giuseppe", "Verdi", "896232", 855790);
            bussinesService.RegisterNewClient("Sergei", "Rahmaninov", "213568", 5670);
            bussinesService.RegisterNewClient("Franz", "Schubert", "645689", 54680);

            foreach (var stock in allStocksType)
            {
                if ((stock.Key != "Tatneft") || (stock.Key != "NLMK"))
                {
                    bussinesService.RegisterNewStock(bussinesService.GetClient(1), stock);
                }

                if ((stock.Key != "KAMAZ") || (stock.Key != "Norilsk Nickel"))
                {
                    bussinesService.RegisterNewStock(bussinesService.GetClient(2), stock);
                }

                if ((stock.Key != "DIXY Group") || (stock.Key != "Severstal"))
                {
                    bussinesService.RegisterNewStock(bussinesService.GetClient(3), stock);
                }

                if ((stock.Key != "AvtoVAZ") || (stock.Key != "Yandex"))
                {
                    bussinesService.RegisterNewStock(bussinesService.GetClient(4), stock);
                }

                if ((stock.Key != "Rosneft Oil Company") || (stock.Key != "LUKOIL NK"))
                {
                    bussinesService.RegisterNewStock(bussinesService.GetClient(5), stock);
                }
            }

            loggerService.Info("The database is created. We can start to trading!");
        }
Ejemplo n.º 10
0
        static void RunTradeDay(BussinesService bussinesService, ILoggerService loggerService)
        {
            var traiders = bussinesService.GetMostWantedTraider();

            List <Traider> traidersList = new List <Traider>();

            foreach (Traider traider in traiders)
            {
                traidersList.Add(traider);
            }

            Random  rnd         = new Random();
            int     sellerIndex = rnd.Next(0, traidersList.Count);
            Traider seller      = traidersList.ElementAt(sellerIndex);

            traidersList.RemoveAt(sellerIndex);

            int     buyerIndex = rnd.Next(0, traidersList.Count);
            Traider buyer      = traidersList.ElementAt(buyerIndex);

            var  arrayOfSharesTypes = Enum.GetValues(typeof(SharesType));
            var  randomSharesType   = (SharesType)arrayOfSharesTypes.GetValue(rnd.Next(arrayOfSharesTypes.Length));
            Deal deal = new Deal
            {
                ID_seller  = seller.ID,
                ID_buyer   = buyer.ID,
                Price      = rnd.Next(1, 100),
                SharesType = randomSharesType
            };

            bussinesService.RegisterNewDeal(deal);

            string sellerName = bussinesService.GetMostWantedTraiderNameById(deal.ID_seller).FirstName +
                                bussinesService.GetMostWantedTraiderNameById(deal.ID_seller).Surname;
            string buyerName = bussinesService.GetMostWantedTraiderNameById(deal.ID_buyer).FirstName +
                               bussinesService.GetMostWantedTraiderNameById(deal.ID_buyer).Surname;

            Console.WriteLine($"Deal done!: Seller: {sellerName} | Buyer: {buyerName} | ShareType: {deal.SharesType} | Price: {deal.Price}");
            loggerService.Info($"Seller: {sellerName} | Buyer: {buyerName} | ShareType: {deal.SharesType} | Price: {deal.Price}");
        }
Ejemplo n.º 11
0
 public void TestSetup()
 {
     dataContextRepository = Substitute.For <IDataContextRepository>();
     loggerService         = Substitute.For <ILoggerService>();
     bussinesService       = new BussinesService(dataContextRepository, loggerService);
 }
Ejemplo n.º 12
0
 public void TestSetup()
 {
     this.dataContext     = Substitute.For <IDataContext>();
     this.bussinesService = new BussinesService(this.dataContext);
 }
Ejemplo n.º 13
0
 public EfInitializer(BussinesService bussinesService)
 {
     this.bussinesService = bussinesService;
 }
Ejemplo n.º 14
0
 public GoodTradeProducer(LoggerService loggerService, BussinesService bussinesService) : base(loggerService, bussinesService)
 {
 }
Ejemplo n.º 15
0
 protected TradeProducer(BussinesService bussinesService) => this.bussinesService = bussinesService;
Ejemplo n.º 16
0
        static void Main(string[] args)
        {
            //using (var dbContext = new TablePerHierarchyContext(
            //    "Data Source=.;Initial Catalog=TablePerHierarchyExampleDb;Integrated Security=True"))
            //{
            //    var bussinesService = new BussinesService(dbContext);

            //    bussinesService.RegisterNewEmployeeAndCreateRelatedContact("Abc", "Def");
            //    bussinesService.RegisterNewEmployeeAndCreateRelatedContact("Ghi", "Jkl");

            //    Console.WriteLine("Sucessfully registered employees");

            //    var searchResult = bussinesService.GetMostWantedEmployees("bc")
            //        .Select(s => s.FirstName).ToArray();

            //    Console.WriteLine($"Found resuts: {string.Join(";", searchResult)}");

            //}

            //Console.WriteLine("Table Per Hierarchy Done");
            //Console.ReadLine();



            //Console.WriteLine("========================================================");


            using (var dbContext = new TablePerTypeContext(
                       "Data Source=.;Initial Catalog=TablePerTypeExampleDb2;Integrated Security=True"))
            {
                var bussinesService = new BussinesService(dbContext);

                //bussinesService.RegisterNewEmployeeAndCreateRelatedContact("Abc", "Def");
                //bussinesService.RegisterNewEmployeeAndCreateRelatedContact("Ghi", "Jkl");

                Console.WriteLine("Sucessfully registered employees");

                var searchResult = bussinesService.GetMostWantedEmployees("bc")
                                   .Select(s => s.FirstName).ToArray();

                var searchResult2 = bussinesService.GetMostWantedEmployees("bc")
                                    .Select(s => s.FirstName).ToArray();

                Console.WriteLine($"Found resuts: {string.Join(";", searchResult)}");
            }

            Console.WriteLine("Table Per Type Done");
            Console.ReadLine();



            //Console.WriteLine("========================================================");

            /*
             * using (var dbContext = new TablePerConcreteClass(
             * "Data Source=.;Initial Catalog=TablePerConcreteClass;Integrated Security=True"))
             * {
             *  var bussinesService = new BussinesService(dbContext);
             *
             *  bussinesService.RegisterNewEmployeeAndCreateRelatedContact("Abc", "Def");
             *  bussinesService.RegisterNewEmployeeAndCreateRelatedContact("Ghi", "Jkl");
             *
             *  Console.WriteLine("Sucessfully registered employees");
             *
             *  var searchResult = bussinesService.GetMostWantedEmployees("bc")
             *      .Select(s => s.FirstName).ToArray();
             *
             *  Console.WriteLine($"Found resuts: {string.Join(";", searchResult)}");
             *
             * }*/

            //Console.WriteLine("Table Per Type Done");
            //Console.ReadLine();
        }
Ejemplo n.º 17
0
 public SimpleStockExchange(BussinesService bussinesService)
 {
     this._bussinesService = bussinesService;
 }
Ejemplo n.º 18
0
        public void Initialize()
        {
            dataContext = Substitute.For <IDataContext>();

            var Shares = new List <Share>()
            {
                new Share()
                {
                    ShareId = 0, ShareName = "abc", ShareCost = 20
                },
                new Share()
                {
                    ShareId = 1, ShareName = "def", ShareCost = 35
                }
            };

            dataContext.Shares.Returns(Shares.AsQueryable());

            client1 = new Client()
            {
                Id        = 0,
                FirstName = "TestDroid",
                LastName  = "Test",
                Balance   = 250,
                Number    = "128476617285",
                Zone      = "White"
            };

            client2 = new Client()
            {
                Id        = 1,
                FirstName = "abc",
                LastName  = "dfe",
                Balance   = 250,
                Number    = "813516515",
                Zone      = "Black"
            };

            var WhiteZoneClients = new List <WhiteZoneClient>()
            {
                new WhiteZoneClient()
                {
                    ClientId = client1.Id
                },
                new WhiteZoneClient()
                {
                    ClientId = client2.Id
                },
            };

            dataContext.WhiteZoneClients.Returns(WhiteZoneClients.AsQueryable());

            var OrangeZoneClients = new List <OrangeZoneClient>()
            {
                new OrangeZoneClient()
                {
                    ClientId = 2, Timeout = 25
                }
            };

            dataContext.OrangeZoneClients.Returns(OrangeZoneClients.AsQueryable());

            var BlackZoneClients = new List <BlackZoneClient>()
            {
                new BlackZoneClient()
                {
                    ClientId = 1, Penalty = 150
                }
            };

            dataContext.BlackZoneClients.Returns(BlackZoneClients.AsQueryable());

            var ClientShares = new List <ClientShare>()
            {
                new ClientShare()
                {
                    ClientId = 0, ShareId = 0, Amount = 35
                },
                new ClientShare()
                {
                    ClientId = 0, ShareId = 1, Amount = 25
                },
                new ClientShare()
                {
                    ClientId = 1, ShareId = 0, Amount = 10
                },
                new ClientShare()
                {
                    ClientId = 1, ShareId = 1, Amount = 15
                },
            };

            dataContext.ClientsShares.Returns(ClientShares.AsQueryable());

            bussinesService = Substitute.For <BussinesService>(dataContext, Substitute.For <ILoggerService>());
        }
Ejemplo n.º 19
0
 protected TradeProducer(LoggerService loggerService, BussinesService bussinesService)
 {
     this.loggerService   = loggerService;
     this.bussinesService = bussinesService;
 }
Ejemplo n.º 20
0
 public GoodTradeProducer(BussinesService bussinesService) : base(bussinesService)
 {
 }