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)); }
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}"); } }
public HttpResponseMessage Post([FromBody] int[] id) { ClientsSharesEntity shareInfo = new ClientsSharesEntity() { ClientID = id[0], ShareID = id[1] }; shareService.RemoveShares(shareInfo); return(Request.CreateResponse(HttpStatusCode.OK)); }
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); }
public void RemoveShares(ClientsSharesEntity clientsSharesInfo) { var clientSharesToRemove = clientsSharesRepository.LoadClientsSharesByID(clientsSharesInfo); if (clientSharesToRemove == null) { return; } clientsSharesRepository.Remove(clientSharesToRemove); clientsSharesRepository.SaveChanges(); }
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)); }
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"); } } }
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); }
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(); } }
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>()); }
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); }
public void PostUpdateShare(ClientsSharesEntity shareInfo, out string answer) { string request = "shares/update"; PostCommand(request, shareInfo, out answer); }
public ClientsSharesEntity LoadClientsSharesByID(ClientsSharesEntity clientsShares) { return(dbContext.ClientsShares.Where(x => x.ClientID == clientsShares.ClientID && x.ShareID == clientsShares.ShareID).FirstOrDefault()); }
public HttpResponseMessage Post([FromBody] ClientsSharesEntity share) { shareService.UpdateShares(share); return(Request.CreateResponse(HttpStatusCode.OK)); }
public void Add(ClientsSharesEntity clientsShares) { dbContext.ClientsShares.Add(clientsShares); }
public void UpdateShares(ClientsSharesEntity clientsSharesInfo) { clientsSharesRepository.Update(clientsSharesInfo); clientsSharesRepository.SaveChanges(); }
public void Remove(ClientsSharesEntity clientsShares) { dbContext.ClientsShares.Remove(LoadClientsSharesByID(clientsShares)); }
public void Update(ClientsSharesEntity clientsShares) { var shareOld = LoadClientsSharesByID(clientsShares); dbContext.Entry(shareOld).CurrentValues.SetValues(clientsShares); }