public void CheckNumbers() { double interest = 0; double rate; double.TryParse(txtRate.Text, out rate); double balance; double.TryParse(txtBalance.Text, out balance); if (balance > 0 && rate > 0) { interest = Debt.CalculateInterest(rate, balance, DateTime.Now.Year, DateTime.Now.Month); } double payment; double.TryParse(txtPayment.Text, out payment); //txtPayment.Text = Math.Round(interest, 2).ToString(); if (payment < interest) { btnSave.Enabled = false; lblToLow.Visible = true; lblToLow.Text = "Payment must be greater than " + Math.Round(interest, 2).ToString() + " to cover interest"; } else { btnSave.Enabled = true; lblToLow.Visible = false; } }
private void calculate_Click(object sender, EventArgs e) { frmDebt d = new frmDebt(); d.ShowDialog(); if (d.Value == DialogResult.OK) { Debt debt = new Debt(d.DebtName, d.Balance, d.IntrestRate, d.Payment, d.FirstPaymentDate); debtList.Debts.Add(debt); DebtList.Save(debtList, path); RefreshData(); } /* * double rate = Convert.ToDouble(txtRate.Text); * double payment = Convert.ToDouble(txtPayment.Text); * double balance = Convert.ToDouble(txtBalance.Text); * string name = txtName.Text; * if (name.Trim() == "") * { * name = "Debt " + (debtList.Debts.Count + 1).ToString(); * } * * Debt debt = new Debt(name, balance, rate, payment); * debtList.Debts.Add(debt); * * DebtList.Save(debtList, path); * * RefreshData(); */ }
private void btnDebtDown_Click(object sender, EventArgs e) { int index = -1; List <int> toSelect = new List <int>(); foreach (DataGridViewRow r in gvDebts.Rows) { index++; if (r.Selected && index < gvDebts.Rows.Count - 1) { Debt dummy = debtList.Debts[index]; debtList.Debts[index] = debtList.Debts[index + 1]; debtList.Debts[index + 1] = dummy; toSelect.Add(index + 1); DebtList.Save(debtList, path); } } RefreshData(); foreach (int s in toSelect) { gvDebts.Rows[s].Selected = true; } }
public DebtDisplay(Debt d) { Name = d.Name; Balance = d.Balance.ToString("C2"); IntrestRate = d.IntrestRate.ToString() + "%"; Payment = d.Payment.ToString("C2"); OriginalBalance = d.OriginalBalance.ToString("C2"); FirstPaymentDate = d.FirstPaymentDate.ToShortDateString(); Enabled = d.Enabled; }
public frmDebt(Debt debt) { InitializeComponent(); ddlTerms.SelectedIndex = 0; DebtName = debt.Name; Balance = debt.Balance; IntrestRate = debt.IntrestRate; Payment = debt.Payment; FirstPaymentDate = debt.FirstPaymentDate; }
private void gvDebts_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { frmDebt d = new frmDebt(debtList.Debts[e.RowIndex]); d.ShowDialog(); if (d.Value == DialogResult.OK) { Debt debt = new Debt(d.DebtName, d.Balance, d.IntrestRate, d.Payment, d.FirstPaymentDate); debtList.Debts[e.RowIndex] = debt; DebtList.Save(debtList, path); RefreshData(); } }
public List <DebtPayment> ProcessPlan(bool Snowball, SnowballApproaches approach, double initialSnowballPayment, DateTime?initialSnowballStart = null) { DateTime startProcess = DateTime.Now; DateTime cur = DateTime.Now; List <DebtPayment> DebtPayments = new List <DebtPayment>(); if (initialSnowballStart == null) { initialSnowballStart = DateTime.Now; } double TotalIntrest = 0; foreach (Debt d in Debts) { d.Balance = d.OriginalBalance; if (d.FirstPaymentDate < cur) { cur = d.FirstPaymentDate; } } while (GetTotalBalance() > 0) { double curExtra = cur >= initialSnowballStart && Snowball ? initialSnowballPayment : 0; if (Snowball) { foreach (ExtraPayment ep in ExtraPayments.Where(x => x.Enabled == true)) { curExtra = curExtra + ep.IsItTimeToApply(cur.Month, cur.Year); } } List <Debt> debtOrder = new List <Debt>(); switch (approach) { case SnowballApproaches.MyOrder: debtOrder = Debts.ToList(); break; case SnowballApproaches.LowestBalanceFirst: debtOrder = Debts.OrderBy(x => x.OriginalBalance).ToList(); break; case SnowballApproaches.HighestInterestFirst: debtOrder = Debts.OrderByDescending(x => x.IntrestRate).ToList(); break; } foreach (Debt d in debtOrder) { if (d.Balance > 0 && d.Enabled && cur >= d.FirstPaymentDate) { double extra = 0; if (d.Balance >= curExtra) { extra = curExtra; curExtra = 0; } else { extra = d.Balance; curExtra = curExtra - d.Balance; } PaymentResult pr = Debt.CalculateBalanceWithPayment(d.IntrestRate, d.Payment + extra, d.Balance, cur.Year, cur.Month); d.Balance = pr.NewBalance; TotalIntrest += pr.Interest; DebtPayment dp = new DebtPayment(); dp.Intrest = Math.Round(pr.Interest, 2); dp.Balance = Math.Round(pr.NewBalance, 2); dp.Name = d.Name; dp.Payment = Math.Round(d.Payment + extra, 2); dp.ExtraPaid = extra; dp.Principal = Math.Round(pr.Principal, 2); dp.TotalBalance = Math.Round(GetTotalBalance(), 2); dp.TotalInterestPaid = Math.Round(TotalIntrest, 2); dp.Date = cur.Month.ToString() + "/" + cur.Year.ToString(); DebtPayments.Add(dp); if (Snowball) { if (d.Balance == 0) { Debt snowballTo = new Debt(); snowballTo.Balance = 9999999999; foreach (Debt dm in Debts) { if (dm.Enabled && dm.Balance < snowballTo.Balance && dm.Balance > 0) { snowballTo = dm; } } initialSnowballPayment += d.Payment; //snowballTo.Payment += d.Payment; } } } } cur = cur.AddMonths(1); if (cur.Year > 2050) { break; } } AccumulatorCollection acPrincipal = new AccumulatorCollection(); AccumulatorCollection acInterest = new AccumulatorCollection(); for (int x = 0; x < DebtPayments.Count; x++) { acInterest.AddTo(DebtPayments[x].Date, DebtPayments[x].Intrest); acPrincipal.AddTo(DebtPayments[x].Date, DebtPayments[x].Principal); } for (int x = 0; x < DebtPayments.Count; x++) { DebtPayments[x].TotalMonthlyInterest = acInterest.GetMonthAccumulation(DebtPayments[x].Date); DebtPayments[x].TotalMonthlyPrincipal = acPrincipal.GetMonthAccumulation(DebtPayments[x].Date); } TimeSpan finishedProcess = DateTime.Now - startProcess; Debug.WriteLine($"Process Took: {finishedProcess.TotalSeconds} seconds"); return(DebtPayments); }