public String calculate(CalculatorBean calculatorBean, String mnemonic) { if ("=".Equals(mnemonic)) { // jeśli operacja i bufor jest dostępny to wartość przepisywana jest do drugiej liczby if (calculatorBean.Operation != null && calculatorBean.Buffer != null) { calculatorBean.SecondNumber = Double.Parse(calculatorBean.Buffer.ToString()); log.InfoFormat("Second number accepted: {0}", calculatorBean.SecondNumber); } // operacja zawsze czyści bufor calculatorBean.Buffer = null; // jeżeli dostępna jest operacja do wykonania, ale drugiej liczby nie ma, powtarzana jest pierwsza z liczb if (calculatorBean.Operation != null && calculatorBean.SecondNumber == null) { log.InfoFormat("First number copied to be second number : {0}", calculatorBean.FirstNumber); calculatorBean.SecondNumber = calculatorBean.FirstNumber; } // jeżeli dwie liczby znajdują się w pamięci to wykonujemy na nich operację if (calculatorBean.FirstNumber != null && calculatorBean.SecondNumber != null) { Double result = calculatorBean.Operation(calculatorBean.FirstNumber.GetValueOrDefault(0), calculatorBean.SecondNumber.GetValueOrDefault(0)); log.InfoFormat("Calucation result: {0}", result); if (Double.IsInfinity(result) || Double.IsNaN(result)) { CalculationException calculationException = new CalculationException(); log.Warn("Calcuation error!", calculationException); throw calculationException; } log.InfoFormat("Result copied to first number: {0}", result); calculatorBean.FirstNumber = result; return CalculatorUtils.formatNumber(result); } } return null; }
public String calculate(CalculatorBean calculatorBean, String mnemonic) { if (MATH_OPERATIONS.ContainsKey(mnemonic)) { Double? newValue = null; if (calculatorBean.Buffer != null) { newValue = Double.Parse(calculatorBean.Buffer.ToString()); if (calculatorBean.FirstNumber != null) { log.InfoFormat("Accepted the second number to calculate: {0}", newValue); calculatorBean.SecondNumber = newValue; } else { log.InfoFormat("Accepted the first number to calculate: {0}", newValue); calculatorBean.FirstNumber = newValue; } calculatorBean.Buffer = null; } if (calculatorBean.Operation != null && calculatorBean.SecondNumber != null) { newValue = calculatorBean.Operation(calculatorBean.FirstNumber.GetValueOrDefault(0), calculatorBean.SecondNumber.GetValueOrDefault(0)); log.InfoFormat("Calucation result: {0}", newValue); if (Double.IsInfinity(newValue.GetValueOrDefault(0)) || Double.IsNaN(newValue.GetValueOrDefault(0))) { CalculationException calculationException = new CalculationException(); log.Warn("Calcuation error!", calculationException); throw calculationException; } calculatorBean.FirstNumber = newValue; calculatorBean.SecondNumber = null; } if (MATH_OPERATIONS.ContainsKey(mnemonic)) { log.InfoFormat("Accepted the operation to calculate: {0}", mnemonic); calculatorBean.Operation = MATH_OPERATIONS[mnemonic]; } if (newValue != null) { return CalculatorUtils.formatNumber(newValue.GetValueOrDefault(0)); } } return null; }