public void RepaymantServieThrowsRepaymantCalculationExceptionCase1()
        {
            /* 
                Given the following inputs:
                Loan amount =  290000
                Annual Interest rate = 7.5 (note this is the annual interest rate and we require a monthly repayment figure)
                Term years = 0
                Term months = 0
                This should cause an error to occur in the calculation which must be trapped and displayed to the user in a friendly manner
             */

            try
            {
                Repayment paymentTest = new Repayment() { AnnualInterestRate = 7.5, LoanAmount = 290000, TermMonths = 0, TermYears = 0 };

                var monthlyPaymant = paymentService.MonthlyPaymentAmount(paymentTest);
                var totalToBePaid = paymentService.AmountPaidBack(paymentTest);
            }
            catch (Exception e)
            {
                int? c = null;
                if (e is RepaymantCalculationException) c = (e as RepaymantCalculationException).code;
                Assert.That(c, Is.EqualTo(100));
            }
        }
 // Total amount pay back when the completion of total repaymants
 public double AmountPaidBack(Repayment payment)
 {
     if (payment.TermMonths <= 0 && payment.TermYears <= 0)
     {
         throw new RepaymantCalculationException("Minimum Repaymant term should be one month or more", ExceptionCode.REPAYMENT_TERM_EQUALS_TO_0);
     }
     var repaymentMonths = (payment.TermYears * 12) + payment.TermMonths;
     return Math.Round((MonthlyPaymentAmount(payment) * repaymentMonths), 2, MidpointRounding.AwayFromZero);
 }
        public void RepaymantServieTestCase1()
        {
            /*  Loan amount = 145000
                Annual Interest rate = 4.5 (note this is the annual interest rate and we require a monthly repayment figure)
                Term years = 23
                Term months = 7
                The resulting monthly repayment = 832.33
                Total to be repaid = 235549.39 */

            Repayment paymentTest = new Repayment() { AnnualInterestRate = 4.5, LoanAmount = 145000, TermMonths = 7, TermYears =23  };

            var monthlyPaymant = paymentService.MonthlyPaymentAmount(paymentTest);
            var totalToBePaid = paymentService.AmountPaidBack(paymentTest);

            Assert.That(monthlyPaymant, Is.EqualTo(832.33));
            Assert.That(totalToBePaid, Is.EqualTo(235549.39));
        }
        public RepaymantViewModel getRepaymentData(RepaymantViewModel model)
        {
            try
            {
                // We can use Automapper to Map View Model to Model
                var paymant = new Repayment() { LoanAmount = model.LoanAmount, AnnualInterestRate = model.AnnualInterestRate, TermYears = model.TermYears, TermMonths = model.TermMonths };

                model.MonthlyRepaymant = paymentService.MonthlyPaymentAmount(paymant);
                model.TotalToBePaid = paymentService.AmountPaidBack(paymant);

                return model;
            }
            catch (Exception e)
            {
                throw new HttpResponseException(Request.CreateResponse(
                HttpStatusCode.NotAcceptable, new ErrorResponseView(e).message));
            }
        }
 // get Payment amount
 // The formula for calculating the monthly payment amount is in WIKI
 // https://en.wikipedia.org/wiki/Mortgage_calculator
 public double MonthlyPaymentAmount(Repayment payment)
 {
     if (payment.TermMonths <= 0 && payment.TermYears <= 0)
     {
         throw new RepaymantCalculationException("Minimum Repaymant term should be one month or more", ExceptionCode.REPAYMENT_TERM_EQUALS_TO_0);
     }
     try
     {
         var repaymentMonths = (payment.TermYears * 12) + payment.TermMonths;
         double rate = payment.AnnualInterestRate / 100 / 12;
         double denaminator = Math.Pow((1 + rate), repaymentMonths) - 1;
         return Math.Round((rate + (rate / denaminator)) * payment.LoanAmount, 2, MidpointRounding.AwayFromZero);
     }
     catch (Exception e)
     {
         throw new RepaymantCalculationException(e.Message, ExceptionCode.UN_HANDLED);
     }
    
 }
        public void RepaymantServieTestCase2()
        {
            /* 
                Given the following inputs:
                Loan amount =  290000
                Annual Interest rate = 7.5 (note this is the annual interest rate and we require a monthly repayment figure)
                Term years = 15
                Term months = 6
                The resulting monthly repayment = 2641.50
                Total to be repaid = 491319.00
             */

            Repayment paymentTest = new Repayment() { AnnualInterestRate = 7.5, LoanAmount = 290000, TermMonths = 6, TermYears = 15 };

            var monthlyPaymant = paymentService.MonthlyPaymentAmount(paymentTest);
            var totalToBePaid = paymentService.AmountPaidBack(paymentTest);

            Assert.That(monthlyPaymant, Is.EqualTo(2641.50));
            Assert.That(totalToBePaid, Is.EqualTo(491319.00));
        }