/// <summary>
        /// 
        /// </summary>
        /// <param name="changeRequest"></param>
        /// <returns></returns>
        public ChangeResponse CalculateChange(ChangeRequest changeRequest) {
            this.Log.Save(changeRequest, LogTypeEnum.Information);

            ChangeResponse changeResponse = new ChangeResponse();

            try {

                if (changeRequest.IsValid == false) {
                    changeResponse.OperationReportList = changeRequest.OperationReportList;
                    return changeResponse;
                }
                changeResponse.ChangeAmount = changeRequest.PaidAmount - changeRequest.ProductAmount;

                changeResponse.ChangeList = this.CalculateChangeList(changeResponse.ChangeAmount.Value);

            } catch (Exception exception) {
                // TODO: Log
                ILog exLog = LogFactory.Create(LoggerEnum.WindowsEventLog);
                exLog.Save(exception.Message, LogTypeEnum.Exception);

                OperationReport operationReport = new OperationReport();
                operationReport.Field = "System Error";
                operationReport.Message = exception.Message;
                changeResponse.OperationReportList = new List<OperationReport>();
                changeResponse.OperationReportList.Add(operationReport);
            }
            // TODO: Log
            this.Log.Save(changeResponse, LogTypeEnum.Information);
            return changeResponse;
        }
Esempio n. 2
0
        private void CalculateChange() {
            this.UxTxtChangeAmount.Text = string.Empty;
            ChangeRequest changeRequest = new ChangeRequest();
            long productAmount;
            long paidAmount;
            ErrorCollection = new List<string>();

            if (long.TryParse(this.UxTxtProductAmount.Text, out productAmount) == false) {
                this.ErrorCollection.Add("Valor do produto precisa ser inteiro.");
            }
            if (long.TryParse(this.UxTxtPaidAmount.Text, out paidAmount) == false) {
                this.ErrorCollection.Add("Valor pago precisa ser inteiro.");
            }
            if (paidAmount < productAmount) {
                this.ErrorCollection.Add("O valor pago precisa ser igual ou maior do que o valor do produto.");
            }
            if (this.ErrorCollection.Any()) {
                foreach (string error in this.ErrorCollection) {
                    this.UxTxtChangeAmount.Text += error + Environment.NewLine;
                }
                return;
            }

            IWhereIsMyChangeManager whereIsMyChange = new WhereIsMyChangeManager();

            changeRequest.PaidAmount = paidAmount;
            changeRequest.ProductAmount = productAmount;

            ChangeResponse changeResponse = whereIsMyChange.CalculateChange(changeRequest);

            if (changeResponse.OperationReportList != null && changeResponse.OperationReportList.Any()) {
                foreach (OperationReport operationReport in changeResponse.OperationReportList) {

                    this.UxTxtChangeAmount.Text += string.Format("{0} - {1}{2}", operationReport.Field, operationReport.Message, Environment.NewLine);
                }
            } else {
                foreach (var item in changeResponse.ChangeList) {
                    this.UxTxtChangeAmount.Text += string.Format("{0}: {1} {2}", item.Key, item.Value, Environment.NewLine);
                }
                this.UxTxtChangeAmount.Text += string.Format("Troco: {0}{1}", changeResponse.ChangeAmount, Environment.NewLine);
            }
        }