Exemplo n.º 1
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("What do you want to do?");
                Console.WriteLine("(1) Look Around");
                Console.WriteLine("(2) Buy Animal");
                Console.WriteLine("(3) Pet Dog");
                Console.WriteLine("(4) Steal Animal");
                var    operation = Console.ReadLine();
                double numberOne;
                double numberTwo;
                ICalculationStrategy strat;

                Console.WriteLine("Enter the first number:");
                numberOne = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter the second number:");
                numberTwo = Convert.ToDouble(Console.ReadLine());
                bool reset = false;

                switch (operation)
                {
                case "1":
                    strat = new AdditionStrategy();
                    break;

                case "2":
                    strat = new SubtractionStrategy();
                    break;

                case "3":
                    strat = new MultiplicationStrategy();
                    break;

                case "4":
                    strat = new DivisionStrategy();
                    break;

                default:
                    Console.WriteLine("Please select a valid operation");
                    reset = true;
                    strat = new AdditionStrategy();
                    break;
                }

                if (reset)
                {
                    continue;
                }

                var calc = new Calculation(strat);
                calc.CalculationInterface(numberOne, numberTwo);

                Console.WriteLine("Do you wish to continue? 'y'/'n'");
                if (Console.ReadLine().ToLower() != "y")
                {
                    break;
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("What operation would you like to perform? ('+', '-', '*', '/')");
                var    operation = Console.ReadLine();
                double numberOne;
                double numberTwo;
                ICalculationStrategy strat;

                Console.WriteLine("Enter the first number:");
                numberOne = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter the second number:");
                numberTwo = Convert.ToDouble(Console.ReadLine());
                bool reset = false;

                switch (operation)
                {
                case "+":
                    strat = new AdditionStrategy();
                    break;

                case "-":
                    strat = new SubtractionStrategy();
                    break;

                case "*":
                    strat = new MultiplicationStrategy();
                    break;

                case "/":
                    strat = new DivisionStrategy();
                    break;

                default:
                    Console.WriteLine("Please select a valid operation");
                    reset = true;
                    strat = new AdditionStrategy();
                    break;
                }

                if (reset)
                {
                    continue;
                }

                var calc = new Calculation(strat);
                calc.CalculationInterface(numberOne, numberTwo);

                Console.WriteLine("Do you wish to continue? 'y'/'n'");
                if (Console.ReadLine().ToLower() != "y")
                {
                    break;
                }
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            List <string> mathOperatorList = new List <string>()
            {
                "+", "-", "/", "*"
            };

            while (true)
            {
                //Output of all available math types the program can run
                Console.WriteLine("What kind of math would you like to do?");
                foreach (string mathOperator in mathOperatorList)
                {
                    Console.Write($"\"{mathOperator}\" ");
                }

                //User inputs type of math that is to be used, if its not valid, a reset will be done
                var typeOfMath = Console.ReadLine().ToString();
                if (!mathOperatorList.Contains(typeOfMath))
                {
                    Console.WriteLine("\nNot a valid input, please try again!\n");
                    continue;
                }

                //User inputs numbers that is to be calculated
FaultyInput:
                string numberInputOne;
                string numberInputTwo;
                Console.WriteLine("Input the first number");
                numberInputOne = Console.ReadLine();
                Console.WriteLine("Input the second number");
                numberInputTwo = Console.ReadLine();
                if (!Regex.IsMatch(numberInputOne, "^(-?)(0|([1-9][0-9]*))(\\.[0-9]+)?$") || !Regex.IsMatch(numberInputTwo, "^(-?)(0|([1-9][0-9]*))(\\.[0-9]+)?$"))
                {
                    Console.WriteLine($"One or both of you inputs were not valid please try again\nInput one: {numberInputOne}\nInput two: {numberInputTwo}");
                    goto FaultyInput;
                }
                double firstNum = Convert.ToDouble(numberInputOne);
                Console.WriteLine("Input the second number");
                double secondNum = Convert.ToDouble(numberInputTwo);

                ICalculationStrategy mathStrategy = new DefaultStrategy();

                switch (typeOfMath)
                {
                case "+":
                    mathStrategy = new AdditionStrategy();
                    break;

                case "-":
                    mathStrategy = new SubtractionStrategy();
                    break;

                case "/":
                    mathStrategy = new DivisionStrategy();
                    break;

                case "*":
                    mathStrategy = new MultiplicationStrategy();
                    break;

                default:
                    break;
                }

                //Inputs are calculated
                var calculate = new Calculation(mathStrategy);

                Console.WriteLine($"The result is: {calculate.CalculationInterface(firstNum, secondNum)}");
            }
        }