Calculate Partial Loan Repayment
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "CalculatePartialLoanRepayment".
Inside the public class, we will create a public static void, and declare all the input and output variables.
Then we create a try clause, and create header and content object.
Then, we will connect to the API serviceName, build the request parameters, send the request, and get the response object.
Then we will parse the Content, ServiceResponse, and ServiceRespHeader, and store the "GlobalErrorID", "ErrorText" and "ErrorDetails" in "globalErrorID", "errorText" and "errorDetails" respectively.
If globalErrorID is not '010000', we will display the ErrorText and the ErrorDetails.
Else, it means that there is no error and we will display the results accordingly.
Entire Code:
// CalculatePartialLoanRepayment.java
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class CalculatePartialLoanRepayment {
public static void main(String args[]){
// api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
boolean verbose = true;
// response parameters
String globalErrorID;
String errorText;
String errorDetails;
String BalanceAfterRepayment;
String BalanceBeforeRepayment;
String InterestRate;
String NewInterest;
String NewLoanAmount;
String NewLoanTerm;
String NewMaturityDate;
String NewMonthlyInstallment;
String NewMonthsRemaining;
String OldLoanTerm;
String OldMaturityDate;
String OldMonthlyInstallment;
String PenaltyAmount;
String PenaltyRate;
String RepaymentAmountAfterPenalty;
String SettlementAccountBalance;
String SettlementAccountID;
String Status;
String serviceName = "calculatePartialLoanRepayment";
String userID = "alan1960";
String PIN = "123456";
String OTP = "123456";
String accountID = "1160";
String repaymentAmount = "5000";
String keepMaturityDate = "true";
try {
// build header
JSONObject jo = new JSONObject();
jo.put("serviceName", serviceName);
jo.put("userID", userID);
jo.put("PIN", PIN);
jo.put("OTP", OTP);
JSONObject headerObj = new JSONObject();
headerObj.put("Header", jo);
String header = headerObj.toString();
// build content
jo = new JSONObject();
jo.put("accountID", accountID);
jo.put("repaymentAmount", repaymentAmount);
jo.put("keepMaturityDate", keepMaturityDate);
JSONObject contentObj = new JSONObject();
contentObj.put("Content", jo);
String content = contentObj.toString();
// connect to API service
HttpURLConnection urlConnection = (HttpURLConnection) new URL(apiServiceUrl).openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
// build request parameters
String parameters
= "Header="+header+"&"
+ "Content="+content;
// send request
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream()));
br.write(parameters);
br.close();
// get response
String response = "";
Scanner s = new Scanner(urlConnection.getInputStream());
while (s.hasNextLine()){
response += s.nextLine();
}
s.close();
// get response object
boolean apiError;
JSONObject responseObj = new JSONObject(response);
responseObj = new JSONObject(response);
if (verbose)
{
System.out.println(responseObj.toString(4)); // indent 4 spaces
System.out.println();
}
// parse {"Content"}
contentObj = responseObj.getJSONObject("Content");
// parse {"ServiceResponse"}
JSONObject serviceRespObj = contentObj.getJSONObject("ServiceResponse");
// parse {"ServiceRespHeader"}
JSONObject serviceRespHeaderObj = serviceRespObj.getJSONObject("ServiceRespHeader");
globalErrorID = serviceRespHeaderObj.getString("GlobalErrorID");
errorText = serviceRespHeaderObj.getString("ErrorText");
errorDetails = serviceRespHeaderObj.getString("ErrorDetails");
if (verbose){
System.out.println("GlobalErrorID="+globalErrorID);
System.out.println("ErrorText="+errorText);
System.out.println("ErrorDetails="+errorDetails);
System.out.println();
}
if(globalErrorID.equals("010041"))
{
System.out.println(errorText);
System.out.println(errorDetails);
System.out.println("OTP expired, new OTP will be sent");
}
else if(!globalErrorID.equals("010000"))
{
System.out.println(errorText);
System.out.println(errorDetails);
}
else
{
JSONObject PartialRepaymentResponseObj = serviceRespObj.getJSONObject("PartialRepaymentResponse");
BalanceAfterRepayment = PartialRepaymentResponseObj.getString("BalanceAfterRepayment");
BalanceBeforeRepayment = PartialRepaymentResponseObj.getString("BalanceBeforeRepayment");
InterestRate = PartialRepaymentResponseObj.getString("InterestRate");
NewInterest = PartialRepaymentResponseObj.getString("NewInterest");
NewLoanAmount = PartialRepaymentResponseObj.getString("NewLoanAmount");
NewLoanTerm = PartialRepaymentResponseObj.getString("NewLoanTerm");
NewMaturityDate = PartialRepaymentResponseObj.getString("NewMaturityDate");
NewMonthlyInstallment = PartialRepaymentResponseObj.getString("NewMonthlyInstallment");
NewMonthsRemaining = PartialRepaymentResponseObj.getString("NewMonthsRemaining");
OldLoanTerm = PartialRepaymentResponseObj.getString("OldLoanTerm");
OldMaturityDate = PartialRepaymentResponseObj.getString("OldMaturityDate");
OldMonthlyInstallment = PartialRepaymentResponseObj.getString("OldMonthlyInstallment");
PenaltyAmount = PartialRepaymentResponseObj.getString("PenaltyAmount");
PenaltyRate = PartialRepaymentResponseObj.getString("PenaltyRate");
RepaymentAmountAfterPenalty = PartialRepaymentResponseObj.getString("RepaymentAmountAfterPenalty");
SettlementAccountBalance = PartialRepaymentResponseObj.getString("SettlementAccountBalance");
SettlementAccountID = PartialRepaymentResponseObj.getString("SettlementAccountID");
Status = PartialRepaymentResponseObj.getString("Status");
System.out.println("BalanceAfterRepayment="+BalanceAfterRepayment);
System.out.println("BalanceBeforeRepayment="+BalanceBeforeRepayment);
System.out.println("InterestRate="+InterestRate);
System.out.println("NewInterest="+NewInterest);
System.out.println("NewLoanAmount="+NewLoanAmount);
System.out.println("NewLoanTerm="+NewLoanTerm);
System.out.println("NewMaturityDate="+NewMaturityDate);
System.out.println("NewMonthlyInstallment="+NewMonthlyInstallment);
System.out.println("NewMonthsRemaining="+NewMonthsRemaining);
System.out.println("OldLoanTerm="+OldLoanTerm);
System.out.println("OldMaturityDate="+OldMaturityDate);
System.out.println("OldMonthlyInstallment="+OldMonthlyInstallment);
System.out.println("PenaltyAmount="+PenaltyAmount);
System.out.println("PenaltyRate="+PenaltyRate);
System.out.println("RepaymentAmountAfterPenalty="+RepaymentAmountAfterPenalty);
System.out.println("SettlementAccountBalance="+SettlementAccountBalance);
System.out.println("SettlementAccountID="+SettlementAccountID);
System.out.println("Status="+Status);
}
}
catch(Exception e) {e.printStackTrace(System.out);}
}
}
Created with the Personal Edition of HelpNDoc: Easily create CHM Help documents