package carloanapp; import java.io.FileWriter; import java.io.IOException;public class CarLoan { Customer gObjCustomer = null; double gDbRequestedLoanAmount = 0; double gDbInterestRate = 0; String gStrLoanRiskLevel = null; String CalculateRiskLevel(Customer lObjCustomer, int lIntTenure, Double lDbAmountRequested){ double lDbEMI; double lDbSalary; double lDbTotalLoanAmount; String lStrRiskLevel = null; lDbSalary = lObjCustomer.getgDbAnnualIncome() / 12; lDbTotalLoanAmount = lObjCustomer.getgDbTotalExistingLoanAmount() + lDbAmountRequested; lDbEMI = lDbTotalLoanAmount / ( lIntTenure * 12 ); if(lObjCustomer.getgStrCreditLevel().equals("good")){ if(lDbEMI <= lDbSalary * 40 / 100){ lStrRiskLevel = "low"; }else if(lDbSalary * 40 / 100 < lDbEMI && lDbEMI <= lDbSalary * 80 / 100){ lStrRiskLevel = "medium"; }else{ lStrRiskLevel = "high"; } }else if(lObjCustomer.getgStrCreditLevel().equals("normal")){ if(lDbEMI <= lDbSalary * 30 / 100){ lStrRiskLevel = "low"; }else if(lDbSalary * 30 / 100 < lDbEMI && lDbEMI <= lDbSalary * 70 / 100){ lStrRiskLevel = "medium"; }else{ lStrRiskLevel = "high"; } }else if(lObjCustomer.getgStrCreditLevel().equals("bad")){ if(lDbEMI <= lDbSalary * 20 / 100){ lStrRiskLevel = "low"; }else if(lDbSalary * 20 / 100 < lDbEMI && lDbEMI <= lDbSalary * 60 / 100){ lStrRiskLevel = "medium"; }else{ lStrRiskLevel = "high"; } } return lStrRiskLevel; } double CalculateInterest(String lStrCreditLevel, String lStrRiskLevel){ double lDbInterestRate=0; if (lStrCreditLevel.equals("good") && lStrRiskLevel.equals("high")){ lDbInterestRate=11; } else if(lStrCreditLevel.equals("good") && lStrRiskLevel.equals("medium")){ lDbInterestRate=10; } else if(lStrCreditLevel.equals("good") && lStrRiskLevel.equals("low")){ lDbInterestRate=9; } else if(lStrCreditLevel.equals("normal") && lStrRiskLevel.equals("high")){ lDbInterestRate=12; } else if(lStrCreditLevel.equals("normal") && lStrRiskLevel.equals("medium")){ lDbInterestRate=11; } else if(lStrCreditLevel.equals("normal") && lStrRiskLevel.equals("low")){ lDbInterestRate=10; } else if(lStrCreditLevel.equals("bad") && lStrRiskLevel.equals("high")){ lDbInterestRate=13; } else if(lStrCreditLevel.equals("bad") && lStrRiskLevel.equals("medium")){ lDbInterestRate=12; }else if(lStrCreditLevel.equals("bad") && lStrRiskLevel.equals("low")){ lDbInterestRate=11; } return lDbInterestRate; } void updateCustomerRecord(Customer lObjCustomer,double lDbRequestedLoan){ try{ String filename= "Customers.txt"; FileWriter fw = new FileWriter(filename,true); //the true will append the new data fw.write("\r\n"+lObjCustomer.getgIntCustomerId()+","+lObjCustomer.getgStrCustomerName()+","+lObjCustomer.getgStrCreditLevel()+","+lDbRequestedLoan);//appends the string to the file fw.close(); } catch(IOException ioe) { System.err.println("IOException: " + ioe.getMessage()); } } boolean isAccepted(String lStrLoanRisk){ boolean lBooLoanAccepted=true; if(lStrLoanRisk.equals("high")){ lBooLoanAccepted=false; } return lBooLoanAccepted; } }
package carloanapp;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CarLoanApp {
public static void main(String args[]){
double lDbLoanAmountRequested = 0;
int lIntTenure = 0;
CarLoan lObjCarLoan=new CarLoan();
Scanner in = new Scanner(System.in);
System.out.println("Enter the CustomerId ");
int lIntCustomerId=Integer.parseInt(in.nextLine());
System.out.println("Enter the Annual Income");
double lDbAnnualIncome=Double.parseDouble(in.nextLine());
Customer lObjCustomer = retrieveCustomerFromFile(lIntCustomerId, lDbAnnualIncome);
if(lObjCustomer==null){
System.out.println("Customer doesnt exist ");
}
else{
System.out.println("Enter the loan amount");
lDbLoanAmountRequested = Double.parseDouble(in.nextLine());
System.out.println("Enter the tenure");
lIntTenure = Integer.parseInt(in.nextLine());
String lStrRiskLevel = lObjCarLoan.CalculateRiskLevel(lObjCustomer, lIntTenure, lDbLoanAmountRequested);
boolean lBooLoanStatusAccepted = lObjCarLoan.isAccepted(lStrRiskLevel);
if(lBooLoanStatusAccepted){
double lDbInterestRate = lObjCarLoan.CalculateInterest(lObjCustomer.getgStrCreditLevel(), lStrRiskLevel);
System.out.println("Loan Accepted\nInterest Rate : "+ lDbInterestRate);
lObjCarLoan.updateCustomerRecord(lObjCustomer, lDbLoanAmountRequested);
}else{
System.out.println("Maa chuao: ");
}
}
// System.out.println(lObjCustomer.getgDbAnnualIncome());
// System.out.println(lObjCustomer.getgDbTotalExistingLoanAmount());
// System.out.println(lObjCustomer.getgIntCustomerId());
// System.out.println(lObjCustomer.getgStrCreditLevel());
}
static Customer retrieveCustomerFromFile(int CustomerId, double AnnualIncome){
Customer lObjCustomer = new Customer();
int lIntCustomerId = 0;
String lStrCustomerName = null;
String lStrCreditLevel = null;
double lDbAnnualIncome = 0;
double lDbTotalExistingLoanAmount = 0;
boolean lBooCustomerFound = false;
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Customers.txt");
// Get the object of DataInputStream
DataInputStream inp = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(inp));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
int commaLocation = strLine.indexOf(',');
lIntCustomerId = Integer.parseInt(strLine.substring(0, commaLocation));
if(lIntCustomerId == CustomerId){
lBooCustomerFound = true;
int nextCommaLocation = strLine.indexOf(',', commaLocation + 1);
lStrCustomerName = strLine.substring(commaLocation + 1, nextCommaLocation);
commaLocation = nextCommaLocation;
nextCommaLocation = strLine.indexOf(',', commaLocation + 1);
lStrCreditLevel = strLine.substring(commaLocation + 1, nextCommaLocation);
commaLocation = nextCommaLocation;
nextCommaLocation = strLine.length();
lDbTotalExistingLoanAmount += Double.parseDouble(strLine.substring(commaLocation + 1, nextCommaLocation));
}
}
if(lBooCustomerFound){
lObjCustomer.setgDbAnnualIncome(AnnualIncome);
lObjCustomer.setgDbTotalExistingLoanAmount(lDbTotalExistingLoanAmount);
lObjCustomer.setgIntCustomerId(lIntCustomerId);
lObjCustomer.setgStrCreditLevel(lStrCreditLevel);
lObjCustomer.setgStrCustomerName(lStrCustomerName);
}else{
lObjCustomer=null;
}
inp.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return lObjCustomer;
}
}
package carloanapp;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Customer {
private int gIntCustomerId;
private String gStrCustomerName;
private String gStrCreditLevel;
private double gDbAnnualIncome;
private double gDbTotalExistingLoanAmount;
public Customer(){
}
public int getgIntCustomerId() {
return gIntCustomerId;
}
public void setgIntCustomerId(int gIntCustomerId) {
this.gIntCustomerId = gIntCustomerId;
}
public String getgStrCustomerName() {
return gStrCustomerName;
}
public void setgStrCustomerName(String gStrCustomerName) {
this.gStrCustomerName = gStrCustomerName;
}
public String getgStrCreditLevel() {
return gStrCreditLevel;
}
public void setgStrCreditLevel(String gStrCreditLevel) {
this.gStrCreditLevel = gStrCreditLevel;
}
public double getgDbAnnualIncome() {
return gDbAnnualIncome;
}
public void setgDbAnnualIncome(double gDbAnnualIncome) {
this.gDbAnnualIncome = gDbAnnualIncome;
}
public double getgDbTotalExistingLoanAmount() {
return gDbTotalExistingLoanAmount;
}
public void setgDbTotalExistingLoanAmount(double gDbTotalExistingLoanAmount) {
this.gDbTotalExistingLoanAmount = gDbTotalExistingLoanAmount;
}
}
class for carloan :
package carloanapp;
import java.io.FileWriter;
import java.io.IOException;
public class CarLoan {
Customer gObjCustomer = null;
double gDbRequestedLoanAmount = 0;
double gDbInterestRate = 0;
String gStrLoanRiskLevel = null;
String CalculateRiskLevel(Customer lObjCustomer, int lIntTenure, Double lDbAmountRequested){
double lDbEMI;
double lDbSalary;
double lDbTotalLoanAmount;
String lStrRiskLevel = null;
lDbSalary = lObjCustomer.getgDbAnnualIncome() / 12;
lDbTotalLoanAmount = lObjCustomer.getgDbTotalExistingLoanAmount() + lDbAmountRequested;
lDbEMI = lDbTotalLoanAmount / ( lIntTenure * 12 );
if(lObjCustomer.getgStrCreditLevel().equals("good")){
if(lDbEMI <= lDbSalary * 40 / 100){
lStrRiskLevel = "low";
}else if(lDbSalary * 40 / 100 < lDbEMI && lDbEMI <= lDbSalary * 80 / 100){
lStrRiskLevel = "medium";
}else{
lStrRiskLevel = "high";
}
}else if(lObjCustomer.getgStrCreditLevel().equals("normal")){
if(lDbEMI <= lDbSalary * 30 / 100){
lStrRiskLevel = "low";
}else if(lDbSalary * 30 / 100 < lDbEMI && lDbEMI <= lDbSalary * 70 / 100){
lStrRiskLevel = "medium";
}else{
lStrRiskLevel = "high";
}
}else if(lObjCustomer.getgStrCreditLevel().equals("bad")){
if(lDbEMI <= lDbSalary * 20 / 100){
lStrRiskLevel = "low";
}else if(lDbSalary * 20 / 100 < lDbEMI && lDbEMI <= lDbSalary * 60 / 100){
lStrRiskLevel = "medium";
}else{
lStrRiskLevel = "high";
}
}
return lStrRiskLevel;
}
double CalculateInterest(String lStrCreditLevel, String lStrRiskLevel){
double lDbInterestRate=0;
if (lStrCreditLevel.equals("good") && lStrRiskLevel.equals("high")){
lDbInterestRate=11;
}
else if(lStrCreditLevel.equals("good") && lStrRiskLevel.equals("medium")){
lDbInterestRate=10;
}
else if(lStrCreditLevel.equals("good") && lStrRiskLevel.equals("low")){
lDbInterestRate=9;
}
else if(lStrCreditLevel.equals("normal") && lStrRiskLevel.equals("high")){
lDbInterestRate=12;
}
else if(lStrCreditLevel.equals("normal") && lStrRiskLevel.equals("medium")){
lDbInterestRate=11;
}
else if(lStrCreditLevel.equals("normal") && lStrRiskLevel.equals("low")){
lDbInterestRate=10;
}
else if(lStrCreditLevel.equals("bad") && lStrRiskLevel.equals("high")){
lDbInterestRate=13;
}
else if(lStrCreditLevel.equals("bad") && lStrRiskLevel.equals("medium")){
lDbInterestRate=12;
}else if(lStrCreditLevel.equals("bad") && lStrRiskLevel.equals("low")){
lDbInterestRate=11;
}
return lDbInterestRate;
}
void updateCustomerRecord(Customer lObjCustomer,double lDbRequestedLoan){
try{
String filename= "Customers.txt";
FileWriter fw = new FileWriter(filename,true); //the true will append the new data
fw.write("\r\n"+lObjCustomer.getgIntCustomerId()+","+lObjCustomer.getgStrCustomerName()+","+lObjCustomer.getgStrCreditLevel()+","+lDbRequestedLoan);//appends the string to the file
fw.close();
}
catch(IOException ioe) {
System.err.println("IOException: " + ioe.getMessage());
}
}
boolean isAccepted(String lStrLoanRisk){
boolean lBooLoanAccepted=true;
if(lStrLoanRisk.equals("high")){
lBooLoanAccepted=false;
}
return lBooLoanAccepted;
}
}
class for CarLoanApp :
package carloanapp;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CarLoanApp {
public static void main(String args[]){
double lDbLoanAmountRequested = 0;
int lIntTenure = 0;
CarLoan lObjCarLoan=new CarLoan();
Scanner in = new Scanner(System.in);
System.out.println("Enter the CustomerId ");
int lIntCustomerId=Integer.parseInt(in.nextLine());
System.out.println("Enter the Annual Income");
double lDbAnnualIncome=Double.parseDouble(in.nextLine());
Customer lObjCustomer = retrieveCustomerFromFile(lIntCustomerId, lDbAnnualIncome);
if(lObjCustomer==null){
System.out.println("Customer doesnt exist ");
}
else{
System.out.println("Enter the loan amount");
lDbLoanAmountRequested = Double.parseDouble(in.nextLine());
System.out.println("Enter the tenure");
lIntTenure = Integer.parseInt(in.nextLine());
String lStrRiskLevel = lObjCarLoan.CalculateRiskLevel(lObjCustomer, lIntTenure, lDbLoanAmountRequested);
boolean lBooLoanStatusAccepted = lObjCarLoan.isAccepted(lStrRiskLevel);
if(lBooLoanStatusAccepted){
double lDbInterestRate = lObjCarLoan.CalculateInterest(lObjCustomer.getgStrCreditLevel(), lStrRiskLevel);
System.out.println("Loan Accepted\nInterest Rate : "+ lDbInterestRate);
lObjCarLoan.updateCustomerRecord(lObjCustomer, lDbLoanAmountRequested);
}else{
System.out.println("loan rejected: ");
}
}
// System.out.println(lObjCustomer.getgDbAnnualIncome());
// System.out.println(lObjCustomer.getgDbTotalExistingLoanAmount());
// System.out.println(lObjCustomer.getgIntCustomerId());
// System.out.println(lObjCustomer.getgStrCreditLevel());
}
static Customer retrieveCustomerFromFile(int CustomerId, double AnnualIncome){
Customer lObjCustomer = new Customer();
int lIntCustomerId = 0;
String lStrCustomerName = null;
String lStrCreditLevel = null;
double lDbAnnualIncome = 0;
double lDbTotalExistingLoanAmount = 0;
boolean lBooCustomerFound = false;
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Customers.txt");
// Get the object of DataInputStream
DataInputStream inp = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(inp));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
int commaLocation = strLine.indexOf(',');
lIntCustomerId = Integer.parseInt(strLine.substring(0, commaLocation));
if(lIntCustomerId == CustomerId){
lBooCustomerFound = true;
int nextCommaLocation = strLine.indexOf(',', commaLocation + 1);
lStrCustomerName = strLine.substring(commaLocation + 1, nextCommaLocation);
commaLocation = nextCommaLocation;
nextCommaLocation = strLine.indexOf(',', commaLocation + 1);
lStrCreditLevel = strLine.substring(commaLocation + 1, nextCommaLocation);
commaLocation = nextCommaLocation;
nextCommaLocation = strLine.length();
lDbTotalExistingLoanAmount += Double.parseDouble(strLine.substring(commaLocation + 1, nextCommaLocation));
}
}
if(lBooCustomerFound){
lObjCustomer.setgDbAnnualIncome(AnnualIncome);
lObjCustomer.setgDbTotalExistingLoanAmount(lDbTotalExistingLoanAmount);
lObjCustomer.setgIntCustomerId(lIntCustomerId);
lObjCustomer.setgStrCreditLevel(lStrCreditLevel);
lObjCustomer.setgStrCustomerName(lStrCustomerName);
}else{
lObjCustomer=null;
}
inp.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
return lObjCustomer;
}
}
class for Customer :
package carloanapp;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Customer {
private int gIntCustomerId;
private String gStrCustomerName;
private String gStrCreditLevel;
private double gDbAnnualIncome;
private double gDbTotalExistingLoanAmount;
public Customer(){
}
public int getgIntCustomerId() {
return gIntCustomerId;
}
public void setgIntCustomerId(int gIntCustomerId) {
this.gIntCustomerId = gIntCustomerId;
}
public String getgStrCustomerName() {
return gStrCustomerName;
}
public void setgStrCustomerName(String gStrCustomerName) {
this.gStrCustomerName = gStrCustomerName;
}
public String getgStrCreditLevel() {
return gStrCreditLevel;
}
public void setgStrCreditLevel(String gStrCreditLevel) {
this.gStrCreditLevel = gStrCreditLevel;
}
public double getgDbAnnualIncome() {
return gDbAnnualIncome;
}
public void setgDbAnnualIncome(double gDbAnnualIncome) {
this.gDbAnnualIncome = gDbAnnualIncome;
}
public double getgDbTotalExistingLoanAmount() {
return gDbTotalExistingLoanAmount;
}
public void setgDbTotalExistingLoanAmount(double gDbTotalExistingLoanAmount) {
this.gDbTotalExistingLoanAmount = gDbTotalExistingLoanAmount;
}
}
The best way to calculate loan and EMI are on the basis of the mathematical formula:
[P x R x (1+R)^N]/[(1+R)^N-1
where P stands for the loan amount or principal,
R is the interest rate per month [if the interest rate per annum is 11%, then the rate of interest will be 11/(12 x 100)],
and N is the number of monthly installments.
Source: http://www.emicalculators.in