public void Calculate_CalculatorNegativeNumbersDisallowed_NegativeNumberException() { string[] args = null; string userInput = ""; programArgumentsHandler.InterpretProgramArguments(Arg.Any <string[]>()) .Returns(new ProgramArguments { CalculatorType = Calculator.CalculatorTypes.Addition, AllowNegativeNumbers = false }); calculatorInputHandler.InterpretCalculatorInput(Arg.Any <string>()).Returns( new CalculatorInput { Numbers = new List <int>() { 24, -5, -3 } } ); CalculatorHandler calculatorHandler = new CalculatorHandler(calculatorInputHandler, programArgumentsHandler); CalculatorResult calculatorResult = calculatorHandler.Calculate(userInput, args); Assert.IsTrue(calculatorResult.Total == 0); Assert.IsTrue(calculatorResult.Formula.Equals("0 = 0")); Assert.IsTrue(calculatorResult.ErrorMessage.ToUpper().Contains("NEGATIVE NUMBERS ARE NOT ALLOWED: -5,-3")); }
public void Calculate_MoreThanOneNumberZeroDivision_CannotDivideByZero() { string[] args = null; string userInput = ""; calculatorInputHandler.InterpretCalculatorInput(Arg.Any <string>()).Returns( new CalculatorInput { Numbers = new List <int>() { 24, 0 } } ); programArgumentsHandler.InterpretProgramArguments(Arg.Any <string[]>()) .Returns(new ProgramArguments { CalculatorType = Calculator.CalculatorTypes.Division }); CalculatorHandler calculatorHandler = new CalculatorHandler(calculatorInputHandler, programArgumentsHandler); CalculatorResult calculatorResult = calculatorHandler.Calculate(userInput, args); Assert.IsTrue(calculatorResult.Total == 0); Assert.IsTrue(calculatorResult.Formula.Equals("0 = 0")); Assert.IsTrue(calculatorResult.ErrorMessage.ToUpper().Contains("CANNOT DIVIDE BY ZERO")); }
public ActionResult Calculate(string value, string number, string command) { CalculatorHandler calcHandler = new CalculatorHandler(); CommandHandler comHandler = new CommandHandler(); string result = value ?? "0"; if (result.Length >= 15) { result = value; number = null; command = null; } if (number != null) { result = calcHandler.Assemble(value, number); } else if (command != null) { if (comHandler.IsAssembleCommand(command)) { result = comHandler.NumberHandler(value, command); } } return(PartialView((object)result)); }
private async Task ProcessAnnualSalaryFactory(List <EmployeeEntity> entities) { var calculatorHandler = new CalculatorHandler(); foreach (var item in entities) { var type = (AnnualSalaryFactory.CalculationTypes)System.Enum .Parse(typeof(AnnualSalaryFactory.CalculationTypes), item.ContractTypeName); var factory = await calculatorHandler.ExecuteCreationAsync(type, item); item.CalculatedAnnualSalary = await factory.CalculateAnnualSalaryAsync(); } }
public void Calculate_CalculatorInputException_GenericException() { string[] args = null; string userInput = ""; calculatorInputHandler.InterpretCalculatorInput(Arg.Any <string>()).Returns(x => { throw new Exception($"This is a Test"); }); programArgumentsHandler.InterpretProgramArguments(Arg.Any <string[]>()).Returns(new ProgramArguments()); CalculatorHandler calculatorHandler = new CalculatorHandler(calculatorInputHandler, programArgumentsHandler); CalculatorResult calculatorResult = calculatorHandler.Calculate(userInput, args); Assert.IsTrue(calculatorResult.Total == 0); Assert.IsTrue(calculatorResult.Formula.Equals("0 = 0")); Assert.IsTrue(calculatorResult.ErrorMessage.ToUpper().Contains("ENCOUNTERED AN ERROR ATTEMPTING TO INTEPRET THE CALCULATOR INPUT")); }
public static void StartSocket(int nPort, Action<string> debugAction) { try { CalculatorHandler handler = new CalculatorHandler(); SendLog.Processor processor = new SendLog.Processor(handler); TServerTransport serverTransport = new TServerSocket(nPort); server = new TSimpleServer(processor, serverTransport); debugAction("开始监听端口:" + nPort); server.Serve(); } catch (Exception x) { debugAction(x.StackTrace); } debugAction("停止监听, port:" + nPort); }
public static void StartSocket(int nPort, Action <string> debugAction) { try { CalculatorHandler handler = new CalculatorHandler(); SendLog.Processor processor = new SendLog.Processor(handler); TServerTransport serverTransport = new TServerSocket(nPort); server = new TSimpleServer(processor, serverTransport); debugAction("开始监听端口:" + nPort); server.Serve(); } catch (Exception x) { debugAction(x.StackTrace); } debugAction("停止监听, port:" + nPort); }
private static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; Thread.CurrentThread.Name = "Entry"; Console.Title = "calclog - Terminal"; Console.InputEncoding = Encoding.UTF8; calculator = new CalculatorHandler(); calculator.configureKeys(); calculator.startThread(); Console.CancelKeyPress += delegate { shutdown.Set(); }; shutdown.WaitOne(); calculator.stopThread(); Environment.Exit(0); }
public void Calculate_NoNumbers_SuccessfulResult() { string[] args = null; string userInput = ""; calculatorInputHandler.InterpretCalculatorInput(Arg.Any <string>()).Returns( new CalculatorInput { Numbers = new List <int>() { } } ); programArgumentsHandler.InterpretProgramArguments(Arg.Any <string[]>()).Returns(new ProgramArguments()); CalculatorHandler calculatorHandler = new CalculatorHandler(calculatorInputHandler, programArgumentsHandler); CalculatorResult calculatorResult = calculatorHandler.Calculate(userInput, args); Assert.IsTrue(calculatorResult.Total == 0); Assert.IsTrue(calculatorResult.Formula.Equals("0 = 0")); Assert.IsTrue(calculatorResult.ErrorMessage.Length == 0); }
static void Main(string[] args) { ConsoleKeyInfo consoleKeyInfo; Console.TreatControlCAsInput = true; StringBuilder userInput = new StringBuilder(); CalculatorHandler calculatorHandler = new CalculatorHandler(new CalculatorInputHandler(), new ProgramArgumentsHandler()); Console.WriteLine("Please Enter a String for Calculation or Press CTRL + C to Exit"); do { consoleKeyInfo = Console.ReadKey(); if (consoleKeyInfo.Key == ConsoleKey.Enter) { //Move down to the next line for displaying calculation results Console.WriteLine(); //Calculate the results and display them CalculatorHandler.DisplayCalculatorResult(calculatorHandler.Calculate(userInput.ToString(), args)); //Reset the StringBuilder for possible next input userInput = new StringBuilder(); } else if (consoleKeyInfo.Key == ConsoleKey.Backspace) { userInput.Remove(userInput.Length - 1, 1); } else { userInput.Append(consoleKeyInfo.KeyChar); } } while (!(consoleKeyInfo.Modifiers.HasFlag(ConsoleModifiers.Control) && consoleKeyInfo.Key.HasFlag(ConsoleKey.C))); }