public double GetEndingBalanceWithMonthly(CalculatorModel calc)
        {
            double P = calc.InitialAmount;

            double r        = calc.InterestRate / 100;
            double n        = calc.CompoundingTimes;
            double t        = calc.CalcPeriod;
            double PMT      = (double)calc.MonthlyDeposit;
            double depTimes = 12 / n;

            double balance = P * Math.Pow((1 + (r / n)), n * t) + (PMT * depTimes * ((Math.Pow(1 + r / n, n * t) - 1) / (r / n)));

            balance = Math.Round(balance, 2);

            return(balance);
        }
        public double GetEndingBalance(CalculatorModel calc)
        {
            double P;

            if (calc.MonthlyDeposit != null)
            {
                P = calc.InitialAmount + (double)calc.MonthlyDeposit * 12;
            }
            else
            {
                P = calc.InitialAmount;
            }
            double r = calc.InterestRate / 100;
            double n = calc.CompoundingTimes;
            double t = calc.CalcPeriod;

            double balance = P * Math.Pow((1 + (r / n)), n * t);

            balance = Math.Round(balance, 2);

            return(balance);
        }
        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            //allows for the compounding functions
            Compounding compound = new Compounding();

            //clear every time new submission is made
            YearlyAmounts = new List <CompoundingDisplayModel>();
            YearlyAmountsDataGrid.ItemsSource = null;

            bool IsValid = ValidateValues();

            //If all of the fields have been entered. If not, nothing will happen
            if (IsValid == true)
            {
                CalculatorModel CalcMod = new CalculatorModel();
                CalcMod.InitialAmount    = Convert.ToDouble(InitialAmountTextBox.Text); //can't keep it as a string
                CalcMod.InterestRate     = Convert.ToDouble(InterstRateTextBox.Text);
                CalcMod.CalcPeriod       = Convert.ToDouble(CompoundingPeriodTextBox.Text);
                CalcMod.CompoundingTimes = Convert.ToDouble(CompoundingPerYearTextBox.Text);
                if (AmountAddedPerMonTextBox != null && AmountAddedPerMonTextBox.Text != "")
                {
                    CalcMod.MonthlyDeposit = Convert.ToDouble(AmountAddedPerMonTextBox.Text);
                }


                int years = Convert.ToInt32(CalcMod.CalcPeriod);

                double TotalInterest = 0;

                double YearInterst = 0;

                double PrevYearBalance;

                PrevYearBalance = CalcMod.InitialAmount;



                for (int i = 1; i <= years; i++)
                {
                    CalcMod.CalcPeriod = i;

                    double balance;

                    if (CalcMod.MonthlyDeposit == null)
                    {
                        balance       = compound.GetEndingBalance(CalcMod);
                        TotalInterest = balance - CalcMod.InitialAmount;
                    }
                    else
                    {
                        balance       = compound.GetEndingBalanceWithMonthly(CalcMod);
                        TotalInterest = balance - (CalcMod.InitialAmount + (double)CalcMod?.MonthlyDeposit * 12 * i);
                    }



                    YearInterst = PrevYearBalance * CalcMod.InterestRate / 100;

                    PrevYearBalance = balance;

                    YearlyAmounts.Add(new CompoundingDisplayModel()
                    {
                        Year           = i,
                        Total_Balance  = balance,
                        Total_Interest = TotalInterest,
                        Year_Interest  = YearInterst
                    });
                }

                YearlyAmountsDataGrid.Visibility = Visibility.Visible;

                YearlyAmountsDataGrid.ItemsSource = YearlyAmounts;
            }
        }