static void Main(string[] args) { try { Console.WriteLine("Welcome to the C# calculator"); InputConverter inputConverter = new InputConverter(); CalculatorEngine calculatorEngine = new CalculatorEngine(); Console.WriteLine("Enter Your First Number"); double firstNum = inputConverter.convertToNumeric(Console.ReadLine()); Console.WriteLine("Enter You Second Number"); double secondNum = inputConverter.convertToNumeric(Console.ReadLine()); Console.WriteLine("Enter The Operation:\nAdd/+\nSubtract/-\nMultiply/*\nDivide//"); string operation = Console.ReadLine(); double result = calculatorEngine.calculate(operation, firstNum, secondNum); Console.WriteLine(result); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
static void Main(string[] args) { /* Sometimes it is good to write things that are not there yet. * Make placeholders for those things instead. Like writing the InputConverter line before making the class for it. * This is kinda like test-driven development, because you make the backbone first * the errors will go away if you put the right code for the placeholders. * * Single responsibility principle = cutting the code in pieces for more overview * * There will be an exception when the input is not numeric. To catch this, put all code in a try-catch statement. */ string answer; do { try { //inputconverter for converting the input from string to a numeric type InputConverter inputConverter = new InputConverter(); //engine to do the calculation CalculatorEngine calculatorEngine = new CalculatorEngine(); //to get the inputs //the inputs are double to account for floating point numbers //Console.ReadLine = parameter //ConvertInputToNumeric = method that converts the input Console.Write("Type in a first number: "); double firstNumber = inputConverter.ConvertInputToNumeric(Console.ReadLine()); Console.Write("Type in a second number: "); double secondNumber = inputConverter.ConvertInputToNumeric(Console.ReadLine()); Console.Write("Type in a operator: "); string operation = Console.ReadLine(); /* To perform the calculation and store the result of the calculation in a variable. * Calculate = method that calculates and will have 3 parameters. */ double result = calculatorEngine.Calculate(operation, secondNumber, firstNumber); Console.WriteLine(result); } catch (Exception ex) { // In real world we would want to log this message //TODO: start logging exceptions Console.WriteLine(ex.Message); } finally { Console.WriteLine("Do you want to continue: "); answer = Console.ReadLine(); } }while (answer == "yes"); }
static void Main(string[] args) { try { InputConverter ic = new InputConverter(); CalculatorEngine ce = new CalculatorEngine(); double first_number = ic.convertToDouble(Console.ReadLine()); double second_number = ic.convertToDouble(Console.ReadLine()); string opertaion = Console.ReadLine(); double result = ce.operate(first_number, second_number, opertaion); Console.WriteLine(result); } catch (Exception ex)// { //log exceptions Console.WriteLine(ex.Message); } }
static void Main(string[] args) { try { InputConverter inputConverter = new InputConverter(); CalculatorEngine calculatorEngine = new CalculatorEngine(); double firstNumber = inputConverter.ConvertInputToNumeric(Console.ReadLine()); double secondNumber = inputConverter.ConvertInputToNumeric(Console.ReadLine()); string operation = Console.ReadLine(); double result = calculatorEngine.Calculate(operation, firstNumber, secondNumber); Console.WriteLine(result); } catch (Exception ex) { // TODO : log the exceptions Console.WriteLine("Error {0}", ex.Message); } }