public async Task Add_Banknotes_Cout_To_Database_Object_Values() { var databaseName = nameof(Add_Banknotes_Cout_To_Database_Object_Values); var options = CashRegisterService_Utilities.GetOptions(databaseName); var banknotesDTO = new BanknotesDTO() { Fifty = 5, Twenty = 2, Ten = 3, Five = 6, Two = 4, One = 9 }; using (var actAndAssertContext = new CashRegisterContext(options)) { var sut = new CashRegisterService(actAndAssertContext); var response = await sut.Deposit(banknotesDTO); Assert.AreEqual(actAndAssertContext.Banknotes.FirstOrDefault().Fifty, 5); Assert.AreEqual(actAndAssertContext.Banknotes.FirstOrDefault().Twenty, 2); Assert.AreEqual(actAndAssertContext.Banknotes.FirstOrDefault().Ten, 3); Assert.AreEqual(actAndAssertContext.Banknotes.FirstOrDefault().Five, 6); Assert.AreEqual(actAndAssertContext.Banknotes.FirstOrDefault().Two, 4); Assert.AreEqual(actAndAssertContext.Banknotes.FirstOrDefault().One, 9); }; }
public async Task <GeneralResponseModel> Deposit(BanknotesDTO banknotesInput) { if (banknotesInput == null) { return(Validation.ValidateReponse(false, (int)HttpStatusCode.Forbidden, FailureMessage: Messages.NoCashIsProvidedToBeDeposited)); } var isRegisterInitialized = await this.context.Banknotes.AnyAsync(); if (isRegisterInitialized == false) { await this.context.Banknotes.AddAsync(new Banknotes()); await this.context.SaveChangesAsync(); } var banknotes = await this.context.Banknotes.FirstOrDefaultAsync(); banknotes.Fifty += banknotesInput.Fifty; banknotes.Twenty += banknotesInput.Twenty; banknotes.Ten += banknotesInput.Ten; banknotes.Five += banknotesInput.Five; banknotes.Two += banknotesInput.Two; banknotes.One += banknotesInput.One; await this.context.SaveChangesAsync(); return(Validation.ValidateReponse(true, (int)HttpStatusCode.OK, SuccessfulMessage: Messages.DepositPassed)); }
public async Task Return_Success_Message_When_Successful() { var databaseName = nameof(Return_Success_Message_When_Successful); var options = CashRegisterService_Utilities.GetOptions(databaseName); var banknotesDTO = new BanknotesDTO() { Fifty = 5, Twenty = 2, Ten = 3, Five = 6, Two = 4, One = 9 }; using (var actAndAssertContext = new CashRegisterContext(options)) { var sut = new CashRegisterService(actAndAssertContext); var response = await sut.Deposit(banknotesDTO); Assert.IsTrue(response.Message == Messages.DepositPassed); }; }
public async Task <ICustomActionResult> Deposit([FromBody][Required] BanknotesDTO banknotes) { var response = await this.cashRegisterService.Deposit(banknotes); return(await ExecuteAsync(response)); }
public async Task <GeneralResponseModel <BanknotesDTO> > HandleCashWithdraw(int withdrawAmount) { if (withdrawAmount < 0) { return(Validation.ValidateResponse(false, (int)HttpStatusCode.Forbidden, Model: new BanknotesDTO(), FailureMessage: Messages.NegativeAmountCannotBeWithdrawn)); } var banknotes = await this.context.Banknotes.FirstOrDefaultAsync(); if (banknotes == null) { return(Validation.ValidateResponse(false, (int)HttpStatusCode.Forbidden, Model: new BanknotesDTO(), FailureMessage: Messages.CashRegisterEmpty)); } Dictionary <int, Dictionary <int, int> > notesDic = new Dictionary <int, Dictionary <int, int> >(); AddNotesToList(0, 50, banknotes.Fifty, ref notesDic); AddNotesToList(1, 20, banknotes.Twenty, ref notesDic); AddNotesToList(2, 10, banknotes.Ten, ref notesDic); AddNotesToList(3, 5, banknotes.Five, ref notesDic); AddNotesToList(4, 2, banknotes.Two, ref notesDic); AddNotesToList(5, 1, banknotes.One, ref notesDic); var notesSum = notesDic.Values.Sum(x => x.Keys.Sum() * x.Values.Sum()); if (notesSum < withdrawAmount) { return(Validation.ValidateResponse(false, (int)HttpStatusCode.Forbidden, Model: new BanknotesDTO(), FailureMessage: Messages.CashNotEnoughToWithdraw)); } Dictionary <int, int> notesReturnedCountDic = new Dictionary <int, int>() { { 50, 0 }, { 20, 0 }, { 10, 0 }, { 5, 0 }, { 2, 0 }, { 1, 0 } }; for (int i = 0; i < Constraints.BanknotesTypeAmount; i++) { var currentNoteKey = notesDic[i].Keys.FirstOrDefault(); var currentNoteValueCount = notesDic[i].Values.FirstOrDefault(); var notesUsed = 0; if (i > notesDic.Count - 1) { break; } // if currentNoteValueCount == 0, that means we have no banknotes of this kind available in the register therefore the loop shall not continue if (currentNoteValueCount <= 0) { continue; } if (withdrawAmount >= currentNoteKey) { while (currentNoteValueCount > 0 && withdrawAmount > 0 && withdrawAmount >= currentNoteKey) { withdrawAmount = withdrawAmount - currentNoteKey; currentNoteValueCount--; notesUsed++; } notesReturnedCountDic[currentNoteKey] = notesUsed; } } if (withdrawAmount > 0) { return(Validation.ValidateResponse(false, (int)HttpStatusCode.Forbidden, Model: new BanknotesDTO(), FailureMessage: Messages.BanknotesCannotFulfilPayment)); } var banknotesDTO = new BanknotesDTO { Fifty = notesReturnedCountDic[50], Twenty = notesReturnedCountDic[20], Ten = notesReturnedCountDic[10], Five = notesReturnedCountDic[5], Two = notesReturnedCountDic[2], One = notesReturnedCountDic[1], }; banknotes.Fifty -= notesReturnedCountDic[50]; banknotes.Twenty -= notesReturnedCountDic[20]; banknotes.Ten -= notesReturnedCountDic[10]; banknotes.Five -= notesReturnedCountDic[5]; banknotes.Two -= notesReturnedCountDic[2]; banknotes.One -= notesReturnedCountDic[1]; await this.context.SaveChangesAsync(); return(Validation.ValidateResponse(true, (int)HttpStatusCode.OK, Model: banknotesDTO)); }