forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrencyconvert.java
73 lines (58 loc) · 2.48 KB
/
currencyconvert.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import com.google.gson.Gson;
@WebServlet("/CurrencyConverter")
public class CurrencyConverter extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String query = "";
String amount = "";
String curTo = "";
String curFrom = "";
String res = "";
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
/* Read request parameters */
amount = request.getParameter("amount");
curTo = request.getParameter("to");
curFrom = request.getParameter("from");
/* Open a connection to Google and read the result */
try {
query = "http://www.google.com/ig/calculator?hl=en&q=" + amount + curFrom + "=?" + curTo;
URL url = new URL(query);
try (InputStreamReader stream = new InputStreamReader(url.openStream());
BufferedReader in = new BufferedReader(stream)) {
String str = "";
String temp = "";
while ((temp = in.readLine()) != null) {
str = str + temp;
}
/* Parse the result which is in JSON format */
Gson gson = new Gson();
Recv st = gson.fromJson(str, Recv.class);
String rhs = st.getRhs();
rhs = rhs.replace("�", "");
/* Check if there are additional words (millions, billions, etc.) and print them */
StringTokenizer strto = new StringTokenizer(rhs);
String nextToken = strto.nextToken();
out.write(nextToken);
nextToken = strto.nextToken();
if (nextToken.equals("million") || nextToken.equals("billion") || nextToken.equals("trillion")) {
out.println(" " + nextToken);
}
}
} catch (NumberFormatException e) {
out.println("The given amount is not a valid number");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}