public void PutMoneyToBalance(ArgumentsForPutMoneyToBalance args) { if (!clientTableRepository.ContainsById(args.ClientId)) { throw new ArgumentException($"Client with Id {args.ClientId} doesn't exist"); } ClientEntity clientToChangeBalance = clientTableRepository.GetById(args.ClientId); clientToChangeBalance.Balance += args.AmountToPut; clientTableRepository.Change(clientToChangeBalance); clientTableRepository.SaveChanges(); }
public void ShouldPutMoneyToBalance() { //Arrange ArgumentsForPutMoneyToBalance args = new ArgumentsForPutMoneyToBalance() { AmountToPut = 100M, ClientId = 1 }; var balanceBeforeChange = clientTableRepository.GetById(1).Balance; //Act clientTableRepository.ContainsById(args.ClientId).Returns(true); clientsService.PutMoneyToBalance(args); //Assert clientTableRepository.Received(1).Change(Arg.Is <ClientEntity>(s => s.Id == args.ClientId && s.Balance == balanceBeforeChange + args.AmountToPut)); clientTableRepository.Received(1).SaveChanges(); }
private void ValidateArguments(TransactionArguments args) { if (args.Quantity <= 0) { throw new ArgumentException($"Wrong quantity : {args.Quantity}"); } if (args.SellerId == args.BuyerId) { throw new ArgumentException($"Seller and buyer have the same Id"); } if (!clientTableRepository.ContainsById(args.SellerId)) { throw new ArgumentException($"Client with Id {args.SellerId} doesn't exist"); } if (!clientTableRepository.ContainsById(args.BuyerId)) { throw new ArgumentException($"Client with Id {args.BuyerId} doesn't exist"); } if (!sharesTableRepository.ContainsById(args.SharesId)) { throw new ArgumentException($"Shares with Id {args.SharesId} don't exist"); } var buyer = clientTableRepository.GetById(args.BuyerId); if (buyer.Balance <= 0) { throw new ArgumentException(string.Format($"Buyer is in the {0} zone", buyer.Balance < 0 ? "Black" : "Orange")); } var seller = clientTableRepository.GetById(args.SellerId); foreach (var item in seller.Portfolio) { if (item.Shares.Id == args.SharesId && item.Quantity >= args.Quantity) { return; } } throw new ArgumentException($"Not enough shares to sell"); }
public string GetStateOfClient(int clientId) { string result = string.Empty; if (clientTableRepository.ContainsById(clientId)) { decimal clientBalance = clientTableRepository.GetBalance(clientId); if (clientBalance > 0) { result = $"This client belongs to Green zone."; } else if (clientBalance == 0) { result = $"This client belongs to Orange zone."; } else if (clientBalance < 0) { result = $"This client belongs to Black zone."; } return(result); } result = "Such client doesn't exist in DataBase."; return(result); }
public void Initialize() { clientTableRepository = Substitute.For <IClientTableRepository>(); sharesTableRepository = Substitute.For <ISharesTableRepository>(); clientSharesTableRepository = Substitute.For <IClientSharesTableRepository>(); transactionHistoryTableRepository = Substitute.For <ITransactionHistoryTableRepository>(); clientTableRepository.ContainsById(1).Returns(true); clientTableRepository.ContainsById(2).Returns(true); clientTableRepository.ContainsById(3).Returns(false); sharesTableRepository.ContainsById(1).Returns(true); sharesTableRepository.ContainsById(2).Returns(true); sharesTableRepository.ContainsById(3).Returns(true); sharesTableRepository.ContainsById(4).Returns(false); clientTableRepository.GetById(1).Returns(new ClientEntity() { Id = 1, Balance = 0M, Name = "Seller", Portfolio = new List <ClientSharesEntity>() { new ClientSharesEntity() { Id = 1, Quantity = 50, Shares = new SharesEntity { Id = 1, Price = 10M, SharesType = "TypeA" } }, new ClientSharesEntity() { Id = 2, Quantity = 10, Shares = new SharesEntity { Id = 3, Price = 15M, SharesType = "TypeC" } } } }); clientTableRepository.GetById(2).Returns(new ClientEntity() { Id = 2, Balance = 100M, Name = "Buyer", Portfolio = new List <ClientSharesEntity>() { new ClientSharesEntity() { Id = 2, Quantity = 0, Shares = new SharesEntity { Id = 2, Price = 5M, SharesType = "TypeB" } }, new ClientSharesEntity() { Id = 3, Quantity = 15, Shares = new SharesEntity { Id = 1, Price = 10M, SharesType = "TypeA" } } } }); sharesTableRepository.GetById(1).Returns(new SharesEntity() { Id = 1, Price = 10M, SharesType = "TypeA" }); sharesTableRepository.GetById(2).Returns(new SharesEntity() { Id = 2, Price = 5M, SharesType = "TypeB" }); sharesTableRepository.GetById(3).Returns(new SharesEntity() { Id = 3, Price = 15M, SharesType = "TypeC" }); transactionService = new TransactionService( clientTableRepository, clientSharesTableRepository, sharesTableRepository, transactionHistoryTableRepository); }