Пример #1
0
        public void ConstructATransaction()
        {
            // arrange
            var             expectedFiveDollarBills = 3;
            var             expectedOneDollarBills  = 2;
            var             expectedAmount          = 17;
            CashTransaction testTransaction         = CashTransaction.Start();

            // act
            testTransaction
            .Add(UnitedStatesTender.FiveDollar, expectedFiveDollarBills)
            .Add(UnitedStatesTender.OneDollar, expectedOneDollarBills);

            // assert
            Assert.Equal <int>(expectedAmount, testTransaction.TotalAmount);
            Assert.Equal <int>(expectedFiveDollarBills, testTransaction.BillCount(UnitedStatesTender.FiveDollar));
            Assert.Equal <int>(expectedOneDollarBills, testTransaction.BillCount(UnitedStatesTender.OneDollar));
        }
Пример #2
0
        public void GivenEmptyInventoryWhenRestockedThenBalanceIsExpected()
        {
            // Arrange
            var sut = new AtmInventory();
            var expectedInventory = CashTransaction.Start();

            expectedInventory.Add(UnitedStatesTender.FiveDollar, 10);
            expectedInventory.Add(UnitedStatesTender.OneDollar, 10);

            // Act
            sut.ResetInventory(expectedInventory);

            // Assert
            var actual = sut.MachineBalance();

            Assert.Equal <int>(60, actual.TotalAmount);
            Assert.Equal <int>(10, actual.BillCount(UnitedStatesTender.FiveDollar));
            Assert.Equal <int>(10, actual.BillCount(UnitedStatesTender.OneDollar));
        }
Пример #3
0
        public void GivenStockedInventoryWhenRequestingInventoryByBillsThenOnlyBillsThatAreReuestedAreReturned()
        {
            // Arrange
            var sut = new AtmInventory();
            var expectedInventory = CashTransaction.Start();

            expectedInventory.Add(UnitedStatesTender.TenDollar, 1000);
            expectedInventory.Add(UnitedStatesTender.FiveDollar, 100);
            expectedInventory.Add(UnitedStatesTender.OneDollar, 10);
            sut.ResetInventory(expectedInventory);
            var billsToGet = new List <UnitedStatesTender>();

            billsToGet.Add(UnitedStatesTender.OneDollar);
            billsToGet.Add(UnitedStatesTender.TenDollar);

            // Act
            var actual = sut.InventoryByBills(billsToGet);

            // Assert
            Assert.Equal <int>(1000, actual.BillCount(UnitedStatesTender.TenDollar));
            Assert.Equal <int>(0, actual.BillCount(UnitedStatesTender.FiveDollar));
            Assert.Equal <int>(10, actual.BillCount(UnitedStatesTender.OneDollar));
        }
Пример #4
0
        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));
            }
        }