public AtmMachine(IWithdrawalStrategy withdrawalStrategy, IAtmInventory atmInventory) { if (withdrawalStrategy == null) { throw new ArgumentNullException(nameof(withdrawalStrategy)); } if (atmInventory == null) { throw new ArgumentNullException(nameof(atmInventory)); } _withdrawalStrategy = withdrawalStrategy; _atmInventory = atmInventory; }
public void ConstructWithNullInventoryShouldThrow() { // Arrange var testWithdrawalStrategy = new Moq.Mock <IWithdrawalStrategy>(); IAtmInventory testInventory = null; // Act var actualException = Assert.Throws <ArgumentNullException>(() => { #pragma warning disable RECS0026 // Possible unassigned object created by 'new' new ATMMachine.Entities.AtmMachine(testWithdrawalStrategy.Object, testInventory); #pragma warning restore RECS0026 // Possible unassigned object created by 'new' }); // Assert Assert.Contains("Value cannot be null", actualException.Message); }
public IWithdrawalResult Withdraw(int amount, IAtmInventory inventory) { var withdrawTransaction = CashTransaction.Start(); var descendingUnitedStatesTenders = UnitedStatesTender.GetAllDefinedTenders().OrderByDescending(tender => tender.Value); var transactionAmount = amount; foreach (var tender in descendingUnitedStatesTenders) { var numberOfBills = transactionAmount / tender.Value; if (numberOfBills > 0) { withdrawTransaction.Add(tender, numberOfBills); transactionAmount = transactionAmount - tender.GetValue(numberOfBills); } if (transactionAmount == 0) { break; } if (transactionAmount < 0) { throw new ApplicationException($"transaction amount has gone below zero!"); } } // Does inventory support transaction var isPossible = inventory.Withdraw(withdrawTransaction); if (isPossible) { return(WithdrawalResult.CreateSuccessResult(withdrawTransaction)); } else { return(WithdrawalResult.CreateFailureResult("insufficient funds", withdrawTransaction)); } }