Exemple #1
0
        private void SetCalculationManager(out ICalculatorManager manager)
        {
            _view.DisplayMessage("Select way for calculating: " +
                                 "\npress 1 - via file; " +
                                 "\npress 2 - via console " +
                                 "\npress 0 - to exit");

            while (true)
            {
                int result = 0;
                while (!int.TryParse(_view.GetUserResponse(), out result))
                {
                    _view.DisplayMessage("Wrong number, try again.");
                }

                switch (result)
                {
                case 0:
                    manager = null;
                    return;

                case 1:
                    manager = new FileCalculator(_view);
                    return;

                case 2:
                    manager = new ConsoleCalculator(_view);
                    return;

                default:
                    manager = null;
                    break;
                }
            }
        }
 public void NumberBetweenFiveIsNotValid()
 {
     ConsoleCalculator resultingValue = new ConsoleCalculator();
     bool getNumber;
     getNumber = resultingValue.Equals(6);
     Assert.IsTrue(getNumber);
 }
 public void InputValueNotNotAtest()
 {
     ConsoleCalculator resultingValue = new ConsoleCalculator();
     bool getNumber;
     getNumber = resultingValue.Equals("text");
     Assert.IsTrue(getNumber);
 }
        static void Main(string[] args)
        {
            var calculator = new ConsoleCalculator();

            while (true)
            {
                try
                {
                    Console.WriteLine("Please input the expression to be calculated:");
                    string input = Console.ReadLine();
                    Console.WriteLine();
                    Console.Write("Result: ");
                    Console.WriteLine(calculator.Calculate(input));
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine($"Expression is corrupted: {ex.Message}");
                }
                catch
                {
                    Console.WriteLine("System exception, please contact administrator");
                }
                finally
                {
                    Console.WriteLine(" -----------");
                    Console.WriteLine();
                }
            }
        }
        public void RunTaskConcoleCalculation()
        {
            Console.WriteLine("____________Task Console Calculation____________");
            ICalculator calculator = new ConsoleCalculator();

            calculator.ExecuteCalculation();
        }
Exemple #6
0
        public ApplicationHomePage(ConsoleCalculator calculator)
        {
            Console.WriteLine("Choose your calculator:");
            Console.WriteLine("1) Digit calculator\n2) Complex calculator\n3) Exit");

            base.AddNavigationListItem(
                new PickCalculatorCommand(calculator, CalculatorTypeEnum.DIGIT, "Digit calculator"),
                new PickCalculatorCommand(calculator, CalculatorTypeEnum.COMPLEX, "Complex calculator"),
                new ExitCommand()
                );
        }
Exemple #7
0
        public void GetCalculatedResults_With2Ops_AdditionAndMultiplication()
        {
            // arrange
            double expected = 11;

            //act
            ICalculator calculator = new ConsoleCalculator("5+2*3");
            double      actual     = calculator.GetCalculatedResults();

            // assert
            Assert.AreEqual <double>(expected, actual);
        }
Exemple #8
0
        public void GetCalculatedResults_With2Ops_Subtraction()
        {
            // arrange
            double expected = -18;

            //act
            ICalculator calculator = new ConsoleCalculator("-3-6-9");
            double      actual     = calculator.GetCalculatedResults();

            // assert
            Assert.AreEqual <double>(expected, actual);
        }
Exemple #9
0
        public void GetCalculatedResults_With1Op_Division()
        {
            // arrange
            double expected = 3.3333;

            //act
            ICalculator calculator = new ConsoleCalculator("10/3");
            double      actual     = calculator.GetCalculatedResults();

            // assert
            Assert.AreEqual <double>(expected, Math.Round(actual, 4));
        }
Exemple #10
0
        public void GetCalculatedResults_With1Op_AdditionWithE()
        {
            // arrange
            double expected = 8.1548;

            //act
            ICalculator calculator = new ConsoleCalculator("E*3");
            double      actual     = calculator.GetCalculatedResults();

            // assert
            Assert.AreEqual <double>(expected, Math.Round(actual, 4));
        }
Exemple #11
0
        public void GetCalculatedResults_With3Ops_MultiplicationAndSubtraction()
        {
            // arrange
            double expected = 16;

            //act
            ICalculator calculator = new ConsoleCalculator("12*2-4*2");
            double      actual     = calculator.GetCalculatedResults();

            // assert
            Assert.AreEqual <double>(expected, actual);
        }
Exemple #12
0
        public void Calculate_DifferentResults(string input, string result)
        {
            //arrange
            processorMock.Setup(x => x.GetContent(input)).Returns(new[] { input });
            string actualResult = null;

            processorMock.Setup(x => x.WriteContent(It.IsAny <string>())).Callback((string[] output) =>
            {
                actualResult = output.FirstOrDefault();
            });
            var calculator = new ConsoleCalculator(processorMock.Object);

            calculator.Calculate(input);
            Assert.AreEqual(result, actualResult);
        }
Exemple #13
0
        public void Calculate_InputBracketsBadExpression()
        {
            //arrange
            string input = @"(2+15)/(3+4*2)";

            processorMock.Setup(x => x.GetContent(input)).Returns(new[] { input });
            string actualResult = null;

            processorMock.Setup(x => x.WriteContent(It.IsAny <string>())).Callback((string[] output) =>
            {
                actualResult = output.FirstOrDefault();
            });
            var calculator = new ConsoleCalculator(processorMock.Object);

            //act
            calculator.Calculate(input);

            //assert
            Assert.AreEqual(_errorMessage, actualResult);
        }
Exemple #14
0
        public void Calculate_ValidInputDivideByZeroReturnsDivideByZeroMessage()
        {
            //arrange
            string input = @"5+5/0";

            processorMock.Setup(x => x.GetContent(input)).Returns(new[] { input });
            string actualResult = null;

            processorMock.Setup(x => x.WriteContent(It.IsAny <string>())).Callback((string[] output) =>
            {
                actualResult = output.FirstOrDefault();
            });
            var calculator = new ConsoleCalculator(processorMock.Object);

            //act
            calculator.Calculate(input);

            //assert
            Assert.AreEqual("Division by zero", actualResult);
        }
Exemple #15
0
        public void Calculate_ValidInputCorrectResult()
        {
            //arrange
            string input = @"2+15/3+4*2";

            processorMock.Setup(x => x.GetContent(input)).Returns(new[] { input });
            string actualResult = null;

            processorMock.Setup(x => x.WriteContent(It.IsAny <string>())).Callback((string[] output) =>
            {
                actualResult = output.FirstOrDefault();
            });
            var calculator = new ConsoleCalculator(processorMock.Object);

            //act
            calculator.Calculate(input);

            //assert
            Assert.AreEqual("15", actualResult);
        }
Exemple #16
0
        private static void Main(string[] args)
        {
            ConsoleCalculator consoleProgram = new ConsoleCalculator();

            consoleProgram.run();
        }
Exemple #17
0
 public ConsoleCalculatorTests()
 {
     _sut = new ConsoleCalculator();
 }
        static void Main(string[] args)
        {
            ConsolePrinter consolePrinter = new ConsolePrinter();

            //Person person = new Person("Tom", "Surname", 18);
            //Console.WriteLine(person.GetFormatPerson(2));
            //Console.WriteLine(person.GetFormatPerson(22));
            //Console.WriteLine();

            //Rectangle rectangle = new Rectangle(3, 4, 10, 5);
            //Console.WriteLine($"P = {rectangle.Perimetr()}");

            //Console.Write("Enter n:");
            //Int32.TryParse(Console.ReadLine(), out int n);
            //Console.WriteLine(Enum.GetName(typeof(Month), n));
            //Exceptions exceptions = new Exceptions();
            //try
            //{
            //    exceptions.IndexOutOfRange();
            //}
            //catch(IndexOutOfRangeException ex)
            //{
            //    consolePrinter.WriteLine(ex.Message);
            //}



            //FileHelper fileHelper = new FileHelper(consolePrinter);
            //FileLogger fileLogger = new FileLogger();
            //try
            //{
            //    string data = fileHelper.GetAllFilesByDirectory("d:\\EPAM.NET\\Structs");
            //    consolePrinter.WriteLine(data);
            //    fileLogger.Save("d:\\EPAM.NET\\Structs\\data.txt", data);
            //}
            //catch (DirectoryNotFoundException)
            //{
            //    consolePrinter.WriteLine("WRONG DIRECTORY");
            //}
            //consolePrinter.WriteLine(fileHelper.SearchFileByPartialName("dat", @"d:\"));

            //FileLogger fileLogger = new FileLogger();
            //try
            //{
            //    int a = 0;
            //    int b = 2 / a;
            //}
            //catch (DivideByZeroException ex)
            //{
            //    try
            //    {
            //        fileLogger.Error(ex, "some message");
            //    }
            //    catch(ArgumentException ex2)
            //    {
            //        Console.WriteLine(ex2.Message);
            //    }
            //};
            //try
            //{
            //    fileLogger.Debug("Debug Message");
            //    fileLogger.Info("Info message");
            //    fileLogger.Fatal("Fatal message");
            //    fileLogger.Warn("Warn message");
            //}
            //catch (ArgumentException ex)
            //{
            //    Console.WriteLine(ex.Message);
            //};
            //Car car = new Car(1,(decimal)32.213, 1000,(decimal)33.213);
            //List<Car> cars = new List<Car>()
            //{
            //    new Car(1,(decimal)32.213, 10),
            //    new Car(2,(decimal)12.213, 100),
            //    new Car(3,(decimal)42.213, 1000),
            //    new Car(4,(decimal)2.213, 1000)
            //};
            //BinarySerialize binarySerialize = new BinarySerialize("cars.dat");
            //binarySerialize.Serialize(cars);
            //binarySerialize.Deserialize();

            //XMLSerialize xMLSerialize = new XMLSerialize();
            //xMLSerialize.Serialize(cars);
            //xMLSerialize.Deserialize();

            //JSONSerialize jSONSerialize = new JSONSerialize();
            //jSONSerialize.Serialize(cars);
            //Reflection reflection = new Reflection(consolePrinter);
            //reflection.GetInfoAboutDLL("Logger.dll");

            Console.Write("Enter a: ");
            double x = Double.Parse(Console.ReadLine());

            Console.Write("Enter b: ");
            double y = Double.Parse(Console.ReadLine());

            Console.Write("Enter operation: ");
            char operation = Char.Parse(Console.ReadLine());
            ConsoleCalculator consoleCalculator = new ConsoleCalculator(consolePrinter);

            if (operation == '*')
            {
                consolePrinter.WriteLine(consoleCalculator.Calculation(x, y, (x1, y1) => x1 * y1).ToString());
            }
            else if (operation == '+')
            {
                consolePrinter.WriteLine(consoleCalculator.Calculation(x, y, (x1, y1) => consoleCalculator.Add(x1, y1)).ToString());
            }
            else if (operation == '/')
            {
                try
                {
                    consolePrinter.WriteLine(consoleCalculator.Calculation(x, y, (x1, y1) => x1 / y1).ToString());
                }
                catch (DivideByZeroException)
                {
                    throw new DivideByZeroException();
                }
            }
            else if (operation == '-')
            {
                consolePrinter.WriteLine(consoleCalculator.Calculation(x, y, (x1, y1) => x1 - y1).ToString());
            }



            //ExcelReader excelReader = new ExcelReader(consolePrinter);
            //System.Diagnostics.Stopwatch time = System.Diagnostics.Stopwatch.StartNew();
            //Console.WriteLine("Unique");
            //var smth = excelReader.GetDataTableFromExcel(1, 2);
            //foreach (var item in smth)
            //{
            //    Console.WriteLine(item);
            //}
            //time.Stop();
            //consolePrinter.WriteLine($"Spend time on funtion: {time.Elapsed.TotalMilliseconds.ToString()}");
            //excelReader.SaveIntoFile();

            //FileManager fileManager = new FileManager(consolePrinter);
            //var dublicates = fileManager.GetDublicateFilesInTwoFolders("D:\\EPAM.NET\\Structs\\TrainingWorkshop\\Training7",
            //    "D:\\EPAM.NET\\Structs\\TrainingWorkshop\\Training5");
            //Console.WriteLine("Dublicates in two folders");
            //foreach (var item in dublicates)
            //{
            //    Console.WriteLine(item);
            //}
            //var unique = fileManager.GetUniqueFilesInTwoFolders("D:\\EPAM.NET\\Structs\\TrainingWorkshop\\Training7",
            //    "D:\\EPAM.NET\\Structs\\TrainingWorkshop\\Training5");
            //Console.WriteLine("Uniques in two folders");
            //foreach (var item in unique)
            //{
            //    Console.WriteLine(item);
            //}

            //ParallelSum parallelSum = new ParallelSum(consolePrinter, 1000, 1000);
            //Console.WriteLine(parallelSum.GetParallelSum(5));


            var services = new DiServiceCollection();

            //services.RegisterSingleton<RandomGuidGenerator>();
            //services.RegisterTransient<RandomGuidGenerator>();


            //services.RegisterSingleton<ISomeService, SomeServiceOne>();
            //services.RegisterTransient<IRandomGuidProvider, RandomGuidProvider>();
            //          services.RegisterSingleton<MainApp>();
            services.RegisterSingleton <ICounter, RandomCounter>();
            //services.RegisterSingleton<CounterService>();

            var container = services.GenerateContainer();

            var serviceFirst  = container.GetService <ICounter>();
            var serviceSecond = container.GetService <ICounter>();

            Console.WriteLine(serviceFirst.Value);
            //Console.WriteLine(serviceSecond.Value);


            Console.ReadLine();
        }
Exemple #19
0
 public PickCalculatorCommand(ConsoleCalculator calculator, CalculatorTypeEnum calculatorType, string nameOfDo) : base(nameOfDo)
 {
     _calculator     = calculator;
     _calculatorType = calculatorType;
 }
 public ConsoleCalculatorTests()
 {
     _mockRepository    = new MockRepository(MockBehavior.Strict);
     _console           = _mockRepository.Create <IConsole>();
     _consoleCalculator = new ConsoleCalculator(_console.Object);
 }