Ad

You are developing a microservice that processes loan requests and performs a variety of calculations. In this particular scenario, you will receive a batch of loan requests which must be processed to obtain the monthly interest rate, monthly payment amount and the risk factor. These 3 values must be stored in a read efficient in-memory structure in order to serve as a cache for other systems to query.

The formula to calculate the monthly payment amount is as follows:

(LOAN_AMOUNT * MONTHLY_RATE) / (1 - (1 + MONTHLY_RATE ^ ((-1) * TERM_MONTHS))

The risk factor is a value from 0 to 1, where 0 means no risk and 1 is the highest possible risk. The bank follows these guidelines to estimate a borrower's credit quality (higher percentages mean better credit capabilities):

  • No delinquent debt (+35%)
  • Debt to Income ratio (Monthly Debt / Monthly Income) < 43% (+30%)
  • Credit history (Good +25%)
  • Type of credit used (Mortgage +10%)

Input:

JSON Array of loan submissions

[
  {
    loanId: 1,
    amount: 100000,
    termYears: 10,
    termMonths: 0,
    anualInterest: 6,
    type: "mortgage",
    borrower: {
      name: "Jhon Smith",
      anualIncome: "150000",
      delinquentDebt: false,
      amountOwed: 0
    }
  },
    {
    loanId: 2,
    amount: 200000,
    termYears: 15,
    termMonths: 0,
    anualInterest: 6.5,
    type: "student",
    borrower: {
      name: "James Gosling",
      anualIncome: "150000",
      delinquentDebt: false,
      amountOwed: 0
    }
  }
]

Output:

In-memory collections of instances of LoanAnalysis POJO containing:

  • loanId,
  • monthlyInterestRate
  • monthlyPayment,
  • riskFactor
public class Loan {
  private int amount;
  private int termYears;
  private int termMonths;
  private double anualInterest;
  
  public Loan(int amount, int termYears, int termMonths, double anualInterest){
    this.amount = amount;
    this.termYears = termYears;
    this.termMonths = termMonths;
    this.anualInterest = anualInterest;
  }
  
  public double getMonthlyPayment(){
    return this.amount*this.getMonthlyRate()/
    (1-Math.pow(1+this.getMonthlyRate(), -this.getTermInMonths()));
  }
  
  public double getMonthlyRate(){
    return this.anualInterest/100/12;
  }
  
  public int getTermInMonths(){
    return this.termYears*12+this.termMonths;
  }
  
  public double riskFactor(int anualIncome){
    return 0;
  }
}