コード例 #1
0
        public void SellAndBuyShares(int firstClientID, int secondClientID, ClientsSharesEntity shareType, int numberOfSoldShares)
        {
            if (numberOfSoldShares > shareType.Amount)
            {
                throw new ArgumentException($"Cannot sell {numberOfSoldShares} that more than client have {shareType.Amount}");
            }
            if (firstClientID == secondClientID)
            {
                throw new ArgumentException($"Cannot sell shares to yourself");
            }
            ClientsSharesInfo sharesInfo = new ClientsSharesInfo()
            {
                ClientID = shareType.ClientID,
                ShareID  = shareType.ShareID,
                Amount   = -numberOfSoldShares
            };

            clientsSharesService.ChangeClientsSharesAmount(sharesInfo);
            sharesInfo.Amount  *= -1;
            sharesInfo.ClientID = secondClientID;
            clientsSharesService.ChangeClientsSharesAmount(sharesInfo);

            decimal shareCost = (decimal)shareService.GetAllShares().Where(x => x.ShareID == shareType.ShareID).Select(x => x.ShareCost).FirstOrDefault();

            clientService.ChangeMoney(firstClientID, shareCost * numberOfSoldShares);
            clientService.ChangeMoney(secondClientID, -(shareCost * numberOfSoldShares));
        }
コード例 #2
0
        public void ClientsTrade()
        {
            string answer  = "";
            var    clients = requestSender.GetTop10Clients(1, 10, out answer);

            if (clients.Count() > 1)
            {
                var tradingClients = clients.OrderBy(x => Guid.NewGuid()).Take(2).ToList();
                logger.WriteInfo($"Starting operation between {tradingClients[0].ClientID} and {tradingClients[1].ClientID}");
                ClientsSharesEntity shareType = tradingClients[0].ClientsShares.Where(x => x.Amount > 0).OrderBy(x => Guid.NewGuid()).FirstOrDefault();
                if (shareType == null)
                {
                    logger.WriteWarn($"{tradingClients[0].ClientID} not have shares");
                    return;
                }
                int numberOfSoldShares             = uniformRandomiser.Next(1, (int)shareType.Amount);
                TransactionHistoryInfo transaction = new TransactionHistoryInfo()
                {
                    BuyerClientID  = tradingClients[1].ClientID,
                    SellerClientID = tradingClients[0].ClientID,
                    ShareID        = shareType.ShareID,
                    Amount         = numberOfSoldShares
                };
                requestSender.PostMakeDeal(transaction, out answer);
                logger.WriteInfo($"Result: {answer}");
            }
        }
コード例 #3
0
        public HttpResponseMessage Post([FromBody] int[] id)
        {
            ClientsSharesEntity shareInfo = new ClientsSharesEntity()
            {
                ClientID = id[0],
                ShareID  = id[1]
            };

            shareService.RemoveShares(shareInfo);
            return(Request.CreateResponse(HttpStatusCode.OK));
        }
コード例 #4
0
        public void SellAndBuyShares(int firstClientID, int secondClientID, ClientsSharesEntity shareType, int numberOfSoldShares)
        {
            if (numberOfSoldShares > shareType.Amount)
            {
                throw new ArgumentException($"Cannot sell {numberOfSoldShares} that more than client have {shareType.Amount}");
            }
            if (firstClientID == secondClientID)
            {
                throw new ArgumentException($"Cannot sell shares to yourself");
            }
            ClientsSharesEntity sharesInfo = new ClientsSharesEntity()
            {
                ClientID       = shareType.ClientID,
                ShareID        = shareType.ShareID,
                Amount         = shareType.Amount - numberOfSoldShares,
                CostOfOneShare = shareType.CostOfOneShare
            };

            clientsSharesService.UpdateShares(sharesInfo);

            sharesInfo.ClientID = firstClientID;
            var secondClientShares = clientsSharesService.GetAllClientsShares().Where(x => x.ClientID == sharesInfo.ClientID && x.ShareID == sharesInfo.ShareID).FirstOrDefault();

            if (secondClientShares == null)
            {
                ClientsSharesInfo clientsSharesInfo = new ClientsSharesInfo()
                {
                    ClientID = sharesInfo.ClientID,
                    ShareID  = sharesInfo.ShareID,
                    Amount   = numberOfSoldShares
                };
                clientsSharesService.AddShares(clientsSharesInfo);
            }
            else
            {
                sharesInfo.Amount = secondClientShares.Amount + numberOfSoldShares;
                clientsSharesService.UpdateShares(sharesInfo);
            }

            balanceService.ChangeMoney(firstClientID, shareType.CostOfOneShare * numberOfSoldShares);
            balanceService.ChangeMoney(secondClientID, -(shareType.CostOfOneShare * numberOfSoldShares));

            TransactionHistoryInfo operationHistoryInfo = new TransactionHistoryInfo()
            {
                BuyerClientID  = firstClientID,
                SellerClientID = secondClientID,
                ShareID        = shareType.ShareID,
                Amount         = numberOfSoldShares,
                SumOfOperation = shareType.CostOfOneShare * numberOfSoldShares,
                DateTime       = DateTime.Now
            };

            operationHistoryService.Add(operationHistoryInfo);
        }
コード例 #5
0
        public void RemoveShares(ClientsSharesEntity clientsSharesInfo)
        {
            var clientSharesToRemove = clientsSharesRepository.LoadClientsSharesByID(clientsSharesInfo);

            if (clientSharesToRemove == null)
            {
                return;
            }
            clientsSharesRepository.Remove(clientSharesToRemove);
            clientsSharesRepository.SaveChanges();
        }
コード例 #6
0
        public async Task <IHttpActionResult> Post(ClientsSharesEntity share)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            ClientsSharesInfo sharesInfo = new ClientsSharesInfo()
            {
                Amount = share.Amount, ClientID = share.ClientID, CostOfOneShare = share.CostOfOneShare, ShareID = share.ShareID
            };

            shareService.AddShares(sharesInfo);
            return(Created(share));
        }
コード例 #7
0
        public void ClientsTrade()
        {
            var clients = clientService.GetAllClients();

            if (validator.ValidateClientList(clients, logger))
            {
                var tradingClients = clients.OrderBy(x => Guid.NewGuid()).Take(2).ToList();
                if (validator.ValidateTradingClient(tradingClients[0], logger))
                {
                    logger.WriteInfo($"Starting operation between {tradingClients[0].ClientID} and {tradingClients[1].ClientID}");
                    ClientsSharesEntity shareType = tradingClients[0].ClientsShares.Where(x => x.Amount > 0).OrderBy(x => Guid.NewGuid()).First();
                    int numberOfSoldShares        = uniformRandomiser.Next(1, (int)shareType.Amount);
                    tradingOperationService.SellAndBuyShares(tradingClients[0].ClientID, tradingClients[1].ClientID, shareType, numberOfSoldShares);
                    logger.WriteInfo($"Client {tradingClients[0].ClientID} sold {numberOfSoldShares} shares of {shareType.ShareID} to {tradingClients[1].ClientID}");
                    logger.WriteInfo($"Operation successfully ended");
                }
            }
        }
コード例 #8
0
        public string Run(string[] splittedUserInpit, RequestSender requestSender)
        {
            string answer = "";

            if (splittedUserInpit.Length < 5)
            {
                return("Not enough parameters");
            }
            int clientID;

            if (!int.TryParse(splittedUserInpit[1], out clientID))
            {
                return("Cannot parse ID");
            }
            int shareID;

            if (!int.TryParse(splittedUserInpit[2], out shareID))
            {
                return("Cannot parse ID");
            }
            decimal cost;

            if (!decimal.TryParse(splittedUserInpit[3], out cost))
            {
                return("Cannot parse cost");
            }
            int amount;

            if (!int.TryParse(splittedUserInpit[4], out amount))
            {
                return("Cannot parse amount");
            }

            var share = new ClientsSharesEntity()
            {
                ClientID       = clientID,
                ShareID        = shareID,
                CostOfOneShare = cost,
                Amount         = amount
            };

            requestSender.PostUpdateShare(share, out answer);
            return(answer);
        }
コード例 #9
0
 public void AddShares(ClientsSharesInfo clientsSharesInfo)
 {
     if (validator.ValidateShareToClient(clientsSharesInfo))
     {
         var clientsShares = new ClientsSharesEntity()
         {
             ClientID       = clientsSharesInfo.ClientID,
             ShareID        = clientsSharesInfo.ShareID,
             Amount         = clientsSharesInfo.Amount,
             CostOfOneShare = clientsSharesInfo.CostOfOneShare
         };
         var clientSharesToAdd = clientsSharesRepository.LoadClientsSharesByID(clientsShares);
         if (clientSharesToAdd != null)
         {
             return;
         }
         clientsSharesRepository.Add(clientsShares);
         clientsSharesRepository.SaveChanges();
     }
 }
コード例 #10
0
        public void ShouldNotBuyAndSellSharesMoreThanHave()
        {
            //Arrange
            TradingOperationService operationService = new TradingOperationService(
                clientService, shareService, clientsSharesService);
            int firstClientID  = 1;
            int secondClientID = 2;
            ClientsSharesEntity clientsShares = new ClientsSharesEntity()
            {
                ClientID = 1,
                ShareID  = 1,
                Amount   = 10
            };
            int numberOfSoldShares = 100;

            //Act
            operationService.SellAndBuyShares(firstClientID, secondClientID, clientsShares, numberOfSoldShares);
            //Assert
            clientsSharesService.DidNotReceive().ChangeClientsSharesAmount(Arg.Any <ClientsSharesInfo>());
            clientService.DidNotReceive().ChangeMoney(Arg.Any <int>(), Arg.Any <decimal>());
        }
コード例 #11
0
        public int ChangeClientsSharesAmount(ClientsSharesInfo clientsSharesInfo)
        {
            var clientSharesToChange = clientsSharesRepository.LoadClientsSharesByID(clientsSharesInfo);

            if (clientSharesToChange != null)
            {
                clientSharesToChange.Amount += clientsSharesInfo.Amount;
            }
            else
            {
                clientSharesToChange = new ClientsSharesEntity()
                {
                    ShareID  = clientsSharesInfo.ShareID,
                    ClientID = clientsSharesInfo.ClientID,
                    Amount   = clientsSharesInfo.Amount,
                };
                clientsSharesRepository.Add(clientSharesToChange);
            }

            clientsSharesRepository.SaveChanges();
            return((int)clientSharesToChange.Amount);
        }
コード例 #12
0
        public void PostUpdateShare(ClientsSharesEntity shareInfo, out string answer)
        {
            string request = "shares/update";

            PostCommand(request, shareInfo, out answer);
        }
コード例 #13
0
 public ClientsSharesEntity LoadClientsSharesByID(ClientsSharesEntity clientsShares)
 {
     return(dbContext.ClientsShares.Where(x => x.ClientID == clientsShares.ClientID && x.ShareID == clientsShares.ShareID).FirstOrDefault());
 }
コード例 #14
0
 public HttpResponseMessage Post([FromBody] ClientsSharesEntity share)
 {
     shareService.UpdateShares(share);
     return(Request.CreateResponse(HttpStatusCode.OK));
 }
コード例 #15
0
 public void Add(ClientsSharesEntity clientsShares)
 {
     dbContext.ClientsShares.Add(clientsShares);
 }
コード例 #16
0
        public void UpdateShares(ClientsSharesEntity clientsSharesInfo)
        {
            clientsSharesRepository.Update(clientsSharesInfo);

            clientsSharesRepository.SaveChanges();
        }
コード例 #17
0
 public void Remove(ClientsSharesEntity clientsShares)
 {
     dbContext.ClientsShares.Remove(LoadClientsSharesByID(clientsShares));
 }
コード例 #18
0
        public void Update(ClientsSharesEntity clientsShares)
        {
            var shareOld = LoadClientsSharesByID(clientsShares);

            dbContext.Entry(shareOld).CurrentValues.SetValues(clientsShares);
        }