public LendersList GetLowestInterestLenders(LendersList availableLenders, double loanRequested)
        {
            double cashReserved = 0;
            var    lenders      = new List <Lender>();

            var lendersSortedByInterest = availableLenders.Lenders.OrderBy(l => l.Rate).ToList();

            while (cashReserved < loanRequested)
            {
                var cashNeeded = loanRequested - cashReserved;
                var lender     = lendersSortedByInterest.First();

                if (lender.Founds >= cashNeeded)
                {
                    lenders.Add(new Lender(lender.Name, lender.Rate, cashNeeded));
                    cashReserved += cashNeeded;
                }
                else
                {
                    lenders.Add(lender);
                    cashReserved += lender.Founds;
                }

                lendersSortedByInterest.Remove(lender);
            }

            return(new LendersList(lenders));
        }
Exemple #2
0
        public dynamic GetDeal(LendersList lenders, int requestedLoan, int loanPeriodInMonths)
        {
            var reservedLenders = loanDistributor.GetLowestInterestLenders(lenders, requestedLoan);

            var totalMonthlyRepayment = reservedLenders.Lenders.Select(
                l => repaymentsCalculator.CalculateMonthlyRepaymentsWithCompoundInterest(l.Founds, l.Rate, loanPeriodInMonths)
                ).Sum();

            var totalRepayment = repaymentsCalculator.CalculateTotalRepayment(totalMonthlyRepayment, loanPeriodInMonths);

            var averageRate = repaymentsCalculator.AverageInterestCalculator(reservedLenders.Lenders.ToArray());

            return(new {
                Rate = averageRate,
                MonthlyRepayment = totalMonthlyRepayment,
                TotalRepayment = totalRepayment
            });
        }
Exemple #3
0
        static void Main(string[] args)
        {
            var csvFileName   = args[0].ToString();
            var requestedLoan = int.Parse(args[1]);

            var lenders = LendersList.LoadFromCsvFile(csvFileName);

            if (requestedLoan <= 0 || requestedLoan > lenders.TotalMoneyAvailable())
            {
                Console.WriteLine($"it is not possible to provide a quote at this time");
                return;
            }

            var loanCalculator = new LoanCalculator(new LoanDistributor(), new RepaymentsCalculator());
            var deal           = loanCalculator.GetDeal(lenders, requestedLoan, loanPeriodInMonths);

            Console.WriteLine($"Requested amount: £{requestedLoan}");
            Console.WriteLine($"Rate: {Math.Round(deal.Rate * 100, 1, MidpointRounding.AwayFromZero).ToString("0.0")}%");
            Console.WriteLine($"Monthly repayment: £{Math.Round(deal.MonthlyRepayment, 2, MidpointRounding.AwayFromZero).ToString("0.00")}");
            Console.WriteLine($"Total repayment: £{Math.Round(deal.TotalRepayment, 2, MidpointRounding.AwayFromZero).ToString("0.00")}");
        }