Пример #1
0
        public async Task <IActionResult> Calculate([FromBody] CalculateDto calcDto)
        {
            var message = _changeService.Calc(calcDto.AmountPaid, calcDto.TotalAmountToPay);

            if (!_changeService.IsValid)
            {
                return(BadRequest(_changeService.GetErrors));
            }

            var historyEntry = new ChangeHistory(_changeService.TotalChanged());

            foreach (var(value, quantity) in _changeService.Notes)
            {
                var historyEntryItem = new ChangeHistoryItem(ECurrencyType.Note, quantity, value);
                historyEntry.AddItem(historyEntryItem);
            }

            foreach (var(value, quantity) in _changeService.Coins)
            {
                var historyEntryItem = new ChangeHistoryItem(ECurrencyType.Coin, quantity, value);
                historyEntry.AddItem(historyEntryItem);
            }

            await _context.ChangeHistories.AddAsync(historyEntry);

            await _context.SaveChangesAsync();

            return(Ok(message));
        }
Пример #2
0
        public void GivenKnownMonetaryUnitsOfBillAndPaymentShouldReturnExpectCountOfNotesAndCoins()
        {
            var expectNotes = new Dictionary <int, int>();

            expectNotes.Add(20, 1);
            expectNotes.Add(10, 1);

            var expectCoins = new Dictionary <int, int>();

            expectCoins.Add(50, 2);

            var change       = new Change();
            var stringResult = change.Calc(81, 50);


            Assert.IsTrue(expectNotes.Keys.All(x => change.Notes.ContainsKey(x)));
            Assert.IsTrue(expectNotes.All(x => change.Notes[x.Key] == x.Value));

            Assert.IsTrue(expectCoins.Keys.All(x => change.Coins.ContainsKey(x)));
            Assert.IsTrue(expectCoins.All(x => change.Coins[x.Key] == x.Value));
        }