private List <ChangeData> CalculateEntities(long changeAmount) { long changeRest = changeAmount; List <ChangeData> changeDataCollection = new List <ChangeData>(); while (changeRest > 0) { ChangeData currentData = new ChangeData(); AbstractProcessor proc = ProcessorFactory.CreateProcessor(changeRest); Dictionary <int, long> monetaryObjs = proc.CalculateChange(changeRest); currentData.Name = proc.GetName(); currentData.ChangeDictionary = monetaryObjs.Where(p => p.Value > 0).ToDictionary(p => p.Key, p => p.Value); long currentProduct = monetaryObjs.Sum(p => p.Key * p.Value); changeRest = changeRest - currentProduct; changeDataCollection.Add(currentData); // Dispara o evento informando que um processador foi executado. if (this.OnProcessorExecuted != null) { string nameProcessor = proc.GetName(); this.OnProcessorExecuted(this, nameProcessor, currentProduct); } } return(changeDataCollection); }
public ComputeChangeResponse ComputeChange(ComputeChangeRequest request) { ComputeChangeResponse response = new ComputeChangeResponse(); Logger.Log(request); try { // Verifica se os dados recebidos são válidos. if (request.IsValid == false) { response.OperationReport = request.ValidationReport; return(response); } uint change = CalculateTotalChangeAmount(request.ReceivedAmount, request.ProductAmount); uint totalChangeAmount = change; List <ChangeData> changeDataList = new List <ChangeData>(); while (change > 0) { AbstractProcessor processor = ProcessorFactory.Create(change); if (processor == null) { Report report = new Report(); report.Message = "Ocorreu um erro: Não há troco disponível"; response.OperationReport.Add(report); return(response); } Dictionary <uint, uint> changeDictionary = processor.Calculate(change); ChangeData changeData = new ChangeData() { Name = processor.GetName(), ChangeDictionary = changeDictionary }; changeDataList.Add(changeData); long totalCurrentAmount = changeDictionary.Sum(p => (p.Value * p.Key)); change -= (uint)totalCurrentAmount; } response.ChangeDataList = changeDataList; response.TotalChangeAmount = totalChangeAmount; response.Success = true; } catch (Exception e) { Logger.Log(e); Report report = new Report(); report.Message = "Ocorreu um erro: não foi possível processar sua operação."; response.OperationReport.Add(report); } Logger.Log(response); return(response); }
public ReturnAmountResponse ReturnAmount(ReturnAmountRequest returnCoinsRequest) { LogService.Save("Request", returnCoinsRequest); ReturnAmountResponse returnAmountResponse = new ReturnAmountResponse(); try { // Verifica se os dados recebidos são válidos. if (returnCoinsRequest.IsValid == false) { returnAmountResponse.OperationReport = returnCoinsRequest.ValidationReport; return(returnAmountResponse); } long changeAmount = returnCoinsRequest.PaidAmount - returnCoinsRequest.ProductAmount; long currentChangeAmount = changeAmount; List <ChangeData> chData = new List <ChangeData>(); while (currentChangeAmount > 0) { AbstractProcessor processor = ProcessorFactory.Create(currentChangeAmount); if (processor == null) { returnAmountResponse.OperationReport.Add(new Report(null, "Não foi possivel processar seu troco.")); return(returnAmountResponse); } Dictionary <long, int> result = processor.CalculateChange(currentChangeAmount); long resultTotalAmount = result.Sum(t => t.Key * t.Value); chData.Add(new ChangeData() { Type = processor.GetName(), TotalAmount = resultTotalAmount, Changes = result }); currentChangeAmount -= resultTotalAmount; } returnAmountResponse.TotalAmount = changeAmount; returnAmountResponse.Result = chData; returnAmountResponse.Success = true; } catch (Exception ex) { returnAmountResponse.OperationReport.Add(new Report(null, "Ocorreu um erro interno.")); LogService.Save("Exception", ex.Message); } LogService.Save("Response", returnAmountResponse); return(returnAmountResponse); }