public static List <CashflowRow> CalculateCashflow(Loan loan) { List <CashflowRow> flowList = new List <CashflowRow>(); decimal monthlyPay = MonthlyPayment(loan.Amount, loan.Duration, loan.Rate); Console.WriteLine(monthlyPay); decimal RemainingBalance = loan.Amount; for (int month = 1; month <= loan.Duration; month++) { CashflowRow flowRow = new CashflowRow(); flowRow.RemainingBalance = RemainingBalance; flowRow.Month = month; flowRow.InterestPayment = InterestCalc(loan.Rate, flowRow.RemainingBalance); flowRow.PrincipalPayment = PrincipalPay(monthlyPay, flowRow.InterestPayment); flowRow.RemainingBalance = flowRow.RemainingBalance - flowRow.PrincipalPayment; flowList.Add(flowRow); RemainingBalance = flowRow.RemainingBalance; } return(flowList); }
static void Main(string[] args) { int indication = 1; List <List <CashflowRow> > fullList = new List <List <CashflowRow> >(); while (indication > 0) { Loan loan = new Loan(); Console.Write("Please enter in your loan amount: "); string amount1 = Console.ReadLine(); loan.Amount = decimal.Parse(amount1); Console.Write("Please enter in your loan duration: "); string duration1 = Console.ReadLine(); loan.Duration = Int32.Parse(duration1); Console.Write("Please enter in your interest rate: "); string r1 = Console.ReadLine(); loan.Rate = decimal.Parse(r1); List <CashflowRow> flowList = Calculator.CalculateCashflow(loan); fullList.Add(flowList); int length = flowList.Count; Console.WriteLine("Month\t\tInterest\tPrincipal\tRemaining Balance"); for (int i = 0; i < length; i++) { Console.WriteLine(flowList[i].Month + "\t\t" + Math.Round(flowList[i].InterestPayment, 2) + "\t\t" + Math.Round(flowList[i].PrincipalPayment, 2) + "\t\t" + Math.Round(flowList[i].RemainingBalance, 2)); } Console.Write("Would you want to enter anoother one? yes(1)/no(0)"); indication = int.Parse(Console.ReadLine()); } int maxMonth = fullList.Max(x => x.Count); List <CashflowRow> pool = new List <CashflowRow>(); for (var i = 0; i < maxMonth; ++i) { CashflowRow cashflowRow = new CashflowRow(); cashflowRow.Month = i + 1; foreach (var cashflow in fullList) { int cashflowMonths = cashflow.Count; cashflowRow.InterestPayment += cashflowMonths > i ? cashflow[i].InterestPayment : 0; cashflowRow.PrincipalPayment += cashflowMonths > i ? cashflow[i].PrincipalPayment : 0; cashflowRow.RemainingBalance += cashflowMonths > i ? cashflow[i].RemainingBalance : 0; } pool.Add(cashflowRow); } Console.WriteLine("Month\t\tInterest\tPrincipal\tRemaining Balance"); for (int i = 0; i < pool.Count; i++) { Console.WriteLine(pool[i].Month + "\t\t" + Math.Round(pool[i].InterestPayment, 2) + "\t\t" + Math.Round(pool[i].PrincipalPayment, 2) + "\t\t" + Math.Round(pool[i].RemainingBalance, 2)); } }
public static UnitFlow CalculateCashflow(Loan loan) { UnitFlow unitflow = new UnitFlow(); decimal monthlyPay = MonthlyPayment(loan.Amount, loan.Duration, loan.Rate); decimal RemainingBalance = loan.Amount; for (int month = 1; month <= loan.Duration; month++) { CashflowRow flowRow = new CashflowRow(); flowRow.RemainingBalance = RemainingBalance; flowRow.Month = month; flowRow.InterestPayment = InterestCalc(loan.Rate, flowRow.RemainingBalance); flowRow.PrincipalPayment = PrincipalPay(monthlyPay, flowRow.InterestPayment); flowRow.RemainingBalance = flowRow.RemainingBalance - flowRow.PrincipalPayment; unitflow.Unit.Add(flowRow); RemainingBalance = flowRow.RemainingBalance; } return(unitflow); }