Two input or more than two input parameters txtweb

Txtweb:

Txtweb is a sms based platform to get any details via sms.
Eg: kilometre chennai bangalore – gives the distance and approximate time to reach from chennai to bangalore.

How to get the input from users (from mobile via sms) ?

The parameter name should be “txtweb-message”, if you are using java to handle the inputs then you have to get the input like below,
   

[java]String input = request.getParameter("txtweb-message");[/java]

and if you have given more than two inputs, get the input in the same way as like above and do the splitting according to your requirements.

Eg:  kilometre chennai bangalore

[java]
String input = request.getParameter("txtweb-message"); // input = chennai bangalore
String[] splitSpace = input.split(" "); //splitSpace[0] = chennai, splitSpace[1] = bangalore
String source = splitSpace[0];
String destination = splitSpace[1];[/java]

The below program is written to find the emi and the below example txtweb program uses more than two input parameters,

FindEMI:

[java]

package com.ngdeveloper.com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.*;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

@SuppressWarnings("serial")
public class FindEMIServlet extends HttpServlet {

//private static final String APPKEY_NAME = "txtweb-appkey";
private static final String APPKEY_NAME = "txtweb-appkey";
private static final String APPKEY_CONTENT = "555ed93b-6903-4de4-86c6-87ec38fcc472";

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
processRequest(req, resp);
}

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
processRequest(req, resp);
}

public void processRequest(HttpServletRequest request,
HttpServletResponse response) {
String txtWebResponse = null;
String txtWebMessage = request.getParameter("txtweb-message");

if (txtWebMessage == null || txtWebMessage.isEmpty()) {
txtWebResponse = getWelcomeMessage();
sendResponse(response, txtWebResponse);
return;
}

String[] parameters = txtWebMessage.split(" ");

//System.out.println("cool"+parameters.length);

if (parameters.length != 3) {
//System.out.println("i am here");
txtWebResponse = getWelcomeMessage();
sendResponse(response, txtWebResponse);
return;
}

txtWebResponse = "";
// String searchWord = URLEncoder.encode(parameters[0], "UTF-8");
// String sourceUrlString="http://en.wiktionary.org/wiki/" + searchWord;
String amount = parameters[0];
String interest = parameters[1];
String months = parameters[2];
//System.out.println(amount + interest+months);
try {
double loanAmount = Double.parseDouble(amount);
double rateOfInterest = Double.parseDouble(interest);
int numberOfMonths = Integer.parseInt(months);

// System.out.println("inside try"+loanAmount+rateOfInterest+numberOfMonths);

double temp = 1200; //100*numberofmonths(12))
double interestPerMonth = rateOfInterest/temp;
//System.out.println(interestPerMonth);

double onePlusInterestPerMonth = 1 + interestPerMonth;
//System.out.println(onePlusInterestPerMonth);

double powerOfOnePlusInterestPerMonth = Math.pow(onePlusInterestPerMonth,numberOfMonths);
//System.out.println(powerOfOnePlusInterestPerMonth);

double powerofOnePlusInterestPerMonthMinusOne = powerOfOnePlusInterestPerMonth-1;
//System.out.println(powerofOnePlusInterestPerMonthMinusOne);

double divides = powerOfOnePlusInterestPerMonth/powerofOnePlusInterestPerMonthMinusOne;

double principleMultiplyInterestPerMonth = loanAmount * interestPerMonth;
//System.out.println(principleMultiplyInterestPerMonth);

double totalEmi = principleMultiplyInterestPerMonth*divides;
// System.out.println("EMI per month (Exact) : " + totalEmi);

double finalValue = Math.round( totalEmi * 100.0 ) / 100.0;

//System.out.println("EMI per month (Rounded) : " + finalValue);
txtWebResponse = "Principal Amount:"+loanAmount+",\nInterest Rate:"+rateOfInterest+",\nNumber of Months:"+numberOfMonths+",\nRounded EMI Amount:" +Double.toString(finalValue) +",\nExact EMI Amount "+totalEmi +".\n App Designed by www.Javadomain.in";
// System.out.println("output"+txtWebResponse);

} catch (Exception e) {

txtWebResponse = "Sorry, Try again with different values";
//System.out.println(final_val);
//e.printStackTrace();
}

if (!txtWebResponse.isEmpty()) {
sendResponse(response, txtWebResponse);
return;
}

txtWebResponse = getNothingFoundMessage(txtWebMessage);
sendResponse(response, txtWebResponse);
return;
}

private void sendResponse(HttpServletResponse response, String smsResponse) {
try {
PrintWriter out = response.getWriter();
String smsResponse1 = "<html><head><title>Hello txtWeb user!</title>"
+ "<meta http-equiv=’Content-Type’ content=’text/html; charset=UTF-8′ />"
+ "<meta name=’" + APPKEY_NAME + "’ content=’" + APPKEY_CONTENT + "’ />"
+ "</head><body>" + smsResponse + "</body></html>";
out.println(smsResponse1);
} catch (IOException e) {

}
}

private String getWelcomeMessage() {
return "<html><body>Welcome to EMICalculation \n\n"
+ "Reply with @emicalculation Principal_amount Rate_of_interest number_of_months to find the EMI per month. Eg: @emicalculation 200000 13.5 84.\n\n"
+ "(Powered by www.ngdeveloper.com)</body></html>";
}

private String getNothingFoundMessage(String txtWebMessage) {
return "<html><body>Sorry we are not able to find EMI for your request: "
+ txtWebMessage + "\n\n"
+ "Reply with another values.\n\n"
+ "</body></html>";
}

}

[/java]

Input parameter example:

@findemi 200000 13.5 84 – gives the monthly emi amount to pay.

Leave a Reply