public CalculateChangeResponse GetChange(CalculateChangeRequest request) { CalculateChangeResponse response = new CalculateChangeResponse(); try { LogManager.WriteLog(new LogData() { Category = "Request", ObjectToLog = request, LogType = "Informacao", MethodName = "GetChange" }); if (request.IsValid == false) { response.OperationReportList = request.OperationReportList; return response; } response.ChangeAmountInCents = request.PaidAmount - request.ProductAmount; response.MonetaryDataCollection = CalculateMonetaryChange((long)response.ChangeAmountInCents); if (response.MonetaryDataCollection == null) { response.OperationReportList.Add(new OperationReport() { Message = "Não foi possível retornar o troco." }); return response; } response.Success = true; } catch (Exception ex) { OperationReport operationReport = new OperationReport(); operationReport.Message = ("Não foi possível processar a sua requisição. Por favor, tente novamente mais tarde."); response.OperationReportList.Add(operationReport); LogManager.WriteLog(new LogData() { Category = "EXCEPTION", ObjectToLog = ex.ToString(), LogType = "ERROR", MethodName = "GetChange" }); } finally { LogManager.WriteLog(new LogData() { Category = "Response", ObjectToLog = response, LogType = "Informacao", MethodName = "GetChange" }); } return response; }
private void DoTheJob() { this.UxTxtChangeAmountInCents.Clear(); //Verifica se os campos 'Valor do Produto' e 'Valor Pago' foram preenchidos if (string.IsNullOrEmpty(this.UxTxtProductAmountInCents.Text) == true || string.IsNullOrEmpty(this.UxTxtPaidAmountInCents.Text) == true ) { this.UxTxtChangeAmountInCents.Text = "Campos informados não podem ser nulos!"; return; } try { long paidAmountInCents = Convert.ToInt64(this.UxTxtPaidAmountInCents.Text); long productAmontInCents = Convert.ToInt64(this.UxTxtProductAmountInCents.Text); //Executa a função de calcular o troco CalculateChangeRequest changeRequest = new CalculateChangeRequest(); changeRequest.ProductAmount = productAmontInCents; changeRequest.PaidAmount = paidAmountInCents; CalculateChangeResponse changeResponse = trocoSertoManager.GetChange(changeRequest); if (changeResponse.Success == true && changeResponse.ChangeAmountInCents != null) { foreach (MonetaryData item in changeResponse.MonetaryDataCollection) { foreach (KeyValuePair<int,long> changeValue in item.MonetaryValues) { this.UxTxtChangeAmountInCents.Text += string.Format("{0}:{1} Count:{2} \r\n", item.MonetaryName, changeValue.Key.ToString(), changeValue.Value.ToString()); } } this.UxTxtChangeAmountInCents.Text += "Change amount:" + changeResponse.ChangeAmountInCents.ToString(); } else { foreach (OperationReport operationReport in changeResponse.OperationReportList) { this.UxTxtChangeAmountInCents.Text += operationReport.PropertyName + ": "+ operationReport.Message + Environment.NewLine; } } } catch (Exception ex) { this.UxTxtChangeAmountInCents.Text = ex.Message; } }