コード例 #1
0
        private void UxBtnCalculate_Click(object sender, EventArgs e)
        {
            ulong          productAmount = ulong.Parse(UxTxtProductAmount.Text);
            DilmaProcessor dp            = new DilmaProcessor();

            ulong paidAmount = ulong.Parse(UxTxtPaidAmount.Text);

            ChangeMachine.Core.ChangeCalculator changeCalculator = new Core.ChangeCalculator();

            changeCalculator.OnProcessorCompleted += changeCalculator_OnProcessorCompleted;

            CalculateRequest request = new CalculateRequest();

            request.ProductAmount = productAmount;
            request.PaidAmount    = paidAmount;

            CalculateResponse response = changeCalculator.Calculate(request);

            if (response.Success == false)
            {
                StringBuilder errorMessage = new StringBuilder();

                foreach (ErrorReport error in response.ErrorReportCollection)
                {
                    errorMessage.AppendFormat("{0}: {1}", error.FieldName, error.Message).AppendLine();
                }

                MessageBox.Show(errorMessage.ToString(), "Deu Ruim, Brother!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                StringBuilder result = new StringBuilder();
                foreach (ChangeData changeData in response.Change)
                {
                    foreach (KeyValuePair <uint, ulong> changeItem in changeData.ChangeCollection)
                    {
                        result.AppendFormat("{0} {1} de {2}", changeItem.Value, changeData.MoneyDescription, changeItem.Key).AppendLine();
                    }
                }
                UxTxtChangeResult.Text = result.ToString();
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: turmas/ChangeMachine
        static void Main(string[] args)
        {
            Console.WriteLine("Entre com o valor do produto: ");
            ulong productAmount = ulong.Parse(Console.ReadLine());

            Console.WriteLine("Entre com o valor pago: ");
            ulong paidAmount = ulong.Parse(Console.ReadLine());

            ChangeMachine.Core.ChangeCalculator changeCalculator = new Core.ChangeCalculator();

            CalculateRequest request = new CalculateRequest();

            request.ProductAmount = productAmount;
            request.PaidAmount    = paidAmount;

            CalculateResponse response = changeCalculator.Calculate(request);

            if (response.Success == false)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Ocorreram um ou mais erros:");
                foreach (ErrorReport error in response.ErrorReportCollection)
                {
                    Console.WriteLine("{0}: {1}", error.FieldName, error.Message);
                }
            }
            else
            {
                Console.WriteLine("Troco:");
                foreach (ChangeData changeData in response.Change)
                {
                    foreach (KeyValuePair <uint, ulong> changeItem in changeData.ChangeCollection)
                    {
                        Console.WriteLine("{0} {1} de {2}", changeItem.Value, changeData.MoneyDescription, changeItem.Key);
                    }
                }
            }

            Console.ReadKey();
        }