public Loan AddLoan(Loan inputLoan)
        {
            double principal = inputLoan.Principal;
            double rate      = inputLoan.Rate;
            int    term      = inputLoan.Term;

            if (rate < 1)
            {
                rate *= 100;
            }

            Loan result = context.Loans.Add(new Loan {
                Principal = inputLoan.Principal, Term = inputLoan.Term, Rate = inputLoan.Rate
            });

            double totalMonthlyPayment = (principal) * (rate / 1200) / (1 - Math.Pow((1 + rate / 1200), (term * -1)));

            CashflowRow[] cashflow = new CashflowRow[term];

            CashflowRow row = new CashflowRow
            {
                Month           = 1,
                InterestPayment = principal * rate / 1200
            };

            row.PrincipalPayment = totalMonthlyPayment - row.InterestPayment;
            row.RemainingBalance = principal - row.PrincipalPayment;
            cashflow[0]          = row;
            context.CashflowRows.Add(row);

            for (int i = 1; i <= term - 1; i++)
            {
                row = new CashflowRow
                {
                    Month           = i + 1,
                    InterestPayment = cashflow[i - 1].RemainingBalance * rate / 1200
                };
                row.PrincipalPayment = totalMonthlyPayment - row.InterestPayment;
                row.RemainingBalance = cashflow[i - 1].RemainingBalance - row.PrincipalPayment;
                cashflow[i]          = row;

                context.CashflowRows.Add(row);
            }
            context.SaveChanges();

            return(result);
        }
Example #2
0
        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);

                SaveFunction.AddLoan(loan);
                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));
            }
        }