static void Main(string[] args) { Calculator calculator = new Calculator(); ArgumentsParser parser = new ArgumentsParser(); while (true) // Loop indefinitely { Console.WriteLine("Enter a number:"); string strNumber1 = Console.ReadLine(); Console.WriteLine("Enter another number:"); string strNumber2 = Console.ReadLine(); Console.WriteLine("Enter an operator:"); string strOperator = Console.ReadLine(); //Parse inputs try { CalculationArgument arguments = parser.Parse(strNumber1, strNumber2, strOperator); calculator.Process(arguments); Console.WriteLine("Result: {0} (press enter to continue or 'exit' to quit)", calculator.Result); // Log calculation using Trace for now, why not use Log4net? Trace.WriteLine(String.Format("Calcul {0} {1} {2} ={3} time:{4}", arguments.FirstArgument, arguments.Operator, arguments.SecondArgument, calculator.Result, DateTime.Now.ToString())); } catch (ArgumentException ex) { Console.WriteLine(String.Format("Input error: {0}", ex.Message)); Trace.WriteLine(String.Format(String.Format("User Input Error: {0}", ex.Message))); } catch (Exception ex) { Console.WriteLine(String.Format("Error:{0}", ex.Message)); Trace.WriteLine(String.Format("Other error: {0}", ex.Message)); } if (Console.ReadLine() == "exit") { break; } } }
public void Process(CalculationArgument parsedInput) { Result = parsedInput.FirstArgument; switch (parsedInput.Operator) { case "+": Result += parsedInput.SecondArgument; break; case "-": Result -= parsedInput.SecondArgument; break; case "*": Result *= parsedInput.SecondArgument; break; default: //Throw execption or console message throw new ArgumentException("Wrong operator"); } }