Beispiel #1
0
        static void Main(string[] args)
        {
            var input = new CalculatorInput()
            {
                AnnualBonusPercentage                = .015m,
                GrossPerPaycheck                     = 6380.25m,
                AnnualSalaryIncreasePercent          = .03m,
                Current401kBalance                   = 60000,
                CurrentIraBalance                    = 207000,
                EmployeeContributionPercent          = .10m,
                EmployerMatchMaxPercentPerPaycheck   = .06m,
                InterestRateAnnual401k               = .06m,
                InterestRateAnnualIra                = .06m,
                PaychecksPerYear                     = 24,
                RetirementContributionPercentMonthly = .04m,
                YearsUntilRetirement                 = 22
            };

            var calculator = new Calculator(input);
            var output     = calculator.Calculate();

            foreach (var result in output.Results)
            {
                Console.WriteLine(result.ToString());
            }
            Console.ReadKey();
        }
        private void NumButton_Click(object sender, EventArgs e)
        {
            int digit = int.Parse((sender as Button).Text);

            currentValue = CalculatorInput.TryAddDigit(currentValue, digit);

            InvalidateResultTextBox();
        }
        private void InputProcessor(string stringNumbers)
        {
            var regex1 = new Regex(@"^//(.*)\n");

            var match1 = regex1.Match(stringNumbers);

            var separators = new string[] { "\n", "," };           // default

            if (match1.Success)                                    // look for custom Delimiters
            {
                stringNumbers = regex1.Replace(stringNumbers, ""); //if there are delimiters remove from the numbers

                var stringMatch1 = match1.Groups[1].Value;

                var regex2 = new Regex(@"\[(.*?)\]");   // ? is required to make it non-greedy

                var match2 = regex2.Matches(match1.Groups[1].Value);

                if (match2.Count > 0)                          //look to see if delimiters are in [] format
                {
                    var numberOfDelimiters = match2.Count;

                    separators = new string[numberOfDelimiters];

                    for (var i = 0; i < numberOfDelimiters; i++)
                    {
                        separators[i] = match2[i].Groups[1].Value;
                    }
                }
                else                                           //deals with different but single characters delimiters
                {
                    separators = new string[] { stringMatch1 };
                }
            }

            _calculatorInput = new CalculatorInput(separators, stringNumbers);

            // validate separators before splitting
            if (_calculatorInput.InvalidSeparators.Length > 0)
            {
                throw new DelimiterCannotHaveNumberOnTheEdgeException(_calculatorInput);
            }


            // if match1 is not a success then use default separators
            var processedNumbers = stringNumbers.Split(separators, StringSplitOptions.None).Select(Int32.Parse).ToArray();

            _calculatorInput.SetProcessedNumbers(processedNumbers);

            if (_calculatorInput.NegativeNumbers.Length > 0)
            {
                throw new NegativesNotAllowedException(_calculatorInput);
            }
        }
Beispiel #4
0
 public NegativesNotAllowedException(CalculatorInput calculatorInput) : base(FormatMessage(calculatorInput))
 {
 }
Beispiel #5
0
 private static string FormatMessage(CalculatorInput calculatorInput)
 {
     return("Negatives not allowed: " + string.Join(", ", calculatorInput.NegativeNumbers));
 }
        private void DotButton_Click(object sender, EventArgs e)
        {
            currentValue = CalculatorInput.TryAddDecimalSeparator(currentValue);

            InvalidateResultTextBox();
        }
Beispiel #7
0
 public DelimiterCannotHaveNumberOnTheEdgeException(CalculatorInput calculatorInput) : base(FormatMessage(calculatorInput))
 {
 }
Beispiel #8
0
 private static string FormatMessage(CalculatorInput calculatorInput)
 {
     return("Delimiter cannot have a number on the edge: " + string.Join(", ", calculatorInput.InvalidSeparators));
 }
Beispiel #9
0
 public Calculator(CalculatorInput input)
 {
     _input = input;
 }