Skip to content

Commit af67104

Browse files
authored
Currency Converter
1 parent 39488a9 commit af67104

File tree

1 file changed

+132
-241
lines changed

1 file changed

+132
-241
lines changed

Currency Converter.java

+132-241
Original file line numberDiff line numberDiff line change
@@ -1,241 +1,132 @@
1-
/*
2-
3-
* To change this template, choose Tools | Templates
4-
5-
* and open the template in the editor.
6-
7-
*/
8-
9-
package com.exchange;
10-
11-
import java.io.*;
12-
13-
import java.net.*;
14-
15-
import java.util.*;
16-
17-
import javax.servlet.*;
18-
19-
import javax.servlet.http.*;
20-
21-
import java.io.InputStream;
22-
23-
import java.net.*;
24-
25-
import com.google.gson.*;
26-
27-
/**
28-
29-
*
30-
31-
* @author pakallis
32-
33-
*/
34-
35-
classRecv
36-
37-
{
38-
39-
private String lhs;
40-
41-
private String rhs;
42-
43-
private String error;
44-
45-
private String icc;
46-
47-
public Recv(
48-
49-
{
50-
51-
}
52-
53-
public String getLhs()
54-
55-
{
56-
57-
return lhs;
58-
59-
}
60-
61-
public String getRhs()
62-
63-
{
64-
65-
return rhs;
66-
67-
}
68-
69-
}
70-
71-
public classConvertextendsHttpServlet {
72-
73-
/**
74-
75-
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
76-
77-
* @param request servlet request
78-
79-
* @param response servlet response
80-
81-
* @throws ServletException if a servlet-specific error occurs
82-
83-
* @throws IOException if an I/O error occurs
84-
85-
*/
86-
87-
protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
88-
89-
throws ServletException, IOException {
90-
91-
String query = "";
92-
93-
String amount = "";
94-
95-
String curTo = "";
96-
97-
String curFrom = "";
98-
99-
String submit = "";
100-
101-
String res = "";
102-
103-
HttpSession session;
104-
105-
resp.setContentType("text/html;charset=UTF-8");
106-
107-
PrintWriter out = resp.getWriter();
108-
109-
/*Read request parameters*/
110-
111-
amount = req.getParameter("amount");
112-
113-
curTo = req.getParameter("to");
114-
115-
curFrom = req.getParameter("from");
116-
117-
/*Open a connection to google and read the result*/
118-
119-
try {
120-
121-
query = "http://www.google.com/ig/calculator?hl=en&q=" + amount + curFrom + "=?" + curTo;
122-
123-
URL url = new URL(query);
124-
125-
InputStreamReader stream = new InputStreamReader(url.openStream());
126-
127-
BufferedReader in = new BufferedReader(stream);
128-
129-
String str = "";
130-
131-
String temp = "";
132-
133-
while ((temp = in.readLine()) != null) {
134-
135-
str = str + temp;
136-
137-
}
138-
139-
/*Parse the result which is in json format*/
140-
141-
Gson gson = new Gson();
142-
143-
Recv st = gson.fromJson(str, Recv.class);
144-
145-
String rhs = st.getRhs();
146-
147-
rhs = rhs.replaceAll("�", "");
148-
149-
/*we do the check in order to print the additional word(millions,billions etc)*/
150-
151-
StringTokenizer strto = new StringTokenizer(rhs);
152-
153-
String nextToken;
154-
155-
out.write(strto.nextToken());
156-
157-
nextToken = strto.nextToken();
158-
159-
if( nextToken.equals("million") || nextToken.equals("billion") || nextToken.equals("trillion"))
160-
161-
{
162-
163-
out.println(" "+nextToken);
164-
165-
}
166-
167-
} catch (NumberFormatException e) {
168-
169-
out.println("The given amount is not a valid number");
170-
171-
}
172-
173-
}
174-
175-
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
176-
177-
/**
178-
179-
* Handles the HTTP <code>GET</code> method.
180-
181-
* @param request servlet request
182-
183-
* @param response servlet response
184-
185-
* @throws ServletException if a servlet-specific error occurs
186-
187-
* @throws IOException if an I/O error occurs
188-
189-
*/
190-
191-
@Override
192-
193-
protected void doGet(HttpServletRequest request, HttpServletResponse response)
194-
195-
throws ServletException, IOException {
196-
197-
processRequest(request, response);
198-
199-
}
200-
201-
/**
202-
203-
* Handles the HTTP <code>POST</code> method.
204-
205-
* @param request servlet request
206-
207-
* @param response servlet response
208-
209-
* @throws ServletException if a servlet-specific error occurs
210-
211-
* @throws IOException if an I/O error occurs
212-
213-
*/
214-
215-
@Override
216-
217-
protected void doPost(HttpServletRequest request, HttpServletResponse response)
218-
219-
throws ServletException, IOException {
220-
221-
processRequest(request, response);
222-
223-
}
224-
225-
/**
226-
227-
* Returns a short description of the servlet.
228-
229-
* @return a String containing servlet description
230-
231-
*/
232-
233-
@Override
234-
235-
public String getServletInfo() {
236-
237-
return "Short description";
238-
239-
}// </editor-fold>
240-
241-
}
1+
// Java program to convert from
2+
// rupee to the dollar and vice-versa
3+
// using Java Swing
4+
5+
import javax.swing.*;
6+
import java.awt.*;
7+
import java.awt.event.*;
8+
public class GFG {
9+
10+
// Function to convert from rupee
11+
// to the dollar and vice-versa
12+
// using Java Swing
13+
public static void converter()
14+
{
15+
16+
// Creating a new frame using JFrame
17+
JFrame f = new JFrame("CONVERTER");
18+
19+
// Creating two labels
20+
JLabel l1, l2;
21+
22+
// Creating two text fields.
23+
// One for rupee and one for
24+
// the dollar
25+
JTextField t1, t2;
26+
27+
// Creating three buttons
28+
JButton b1, b2, b3;
29+
30+
// Naming the labels and setting
31+
// the bounds for the labels
32+
l1 = new JLabel("Rupees:");
33+
l1.setBounds(20, 40, 60, 30);
34+
l2 = new JLabel("Dollars:");
35+
l2.setBounds(170, 40, 60, 30);
36+
37+
// Initializing the text fields with
38+
// 0 by default and setting the
39+
// bounds for the text fields
40+
t1 = new JTextField("0");
41+
t1.setBounds(80, 40, 50, 30);
42+
t2 = new JTextField("0");
43+
t2.setBounds(240, 40, 50, 30);
44+
45+
// Creating a button for INR,
46+
// one button for the dollar
47+
// and one button to close
48+
// and setting the bounds
49+
b1 = new JButton("INR");
50+
b1.setBounds(50, 80, 60, 15);
51+
b2 = new JButton("Dollar");
52+
b2.setBounds(190, 80, 60, 15);
53+
b3 = new JButton("close");
54+
b3.setBounds(150, 150, 60, 30);
55+
56+
// Adding action listener
57+
b1.addActionListener(new ActionListener() {
58+
public void actionPerformed(ActionEvent e)
59+
{
60+
// Converting to double
61+
double d
62+
= Double.parseDouble(t1.getText());
63+
64+
// Converting rupees to dollars
65+
double d1 = (d / 65.25);
66+
67+
// Getting the string value of the
68+
// calculated value
69+
String str1 = String.valueOf(d1);
70+
71+
// Placing it in the text box
72+
t2.setText(str1);
73+
}
74+
});
75+
76+
// Adding action listener
77+
b2.addActionListener(new ActionListener() {
78+
public void actionPerformed(ActionEvent e)
79+
{
80+
// Converting to double
81+
double d2
82+
= Double.parseDouble(t2.getText());
83+
84+
// converting Dollars to rupees
85+
double d3 = (d2 * 65.25);
86+
87+
// Getting the string value of the
88+
// calculated value
89+
String str2 = String.valueOf(d3);
90+
91+
// Placing it in the text box
92+
t1.setText(str2);
93+
}
94+
});
95+
96+
// Action listener to close the form
97+
b3.addActionListener(new ActionListener() {
98+
public void actionPerformed(ActionEvent e)
99+
{
100+
f.dispose();
101+
}
102+
});
103+
104+
// Default method for closing the frame
105+
f.addWindowListener(new WindowAdapter() {
106+
public void windowClosing(WindowEvent e)
107+
{
108+
System.exit(0);
109+
}
110+
});
111+
112+
// Adding the created objects
113+
// to the form
114+
f.add(l1);
115+
f.add(t1);
116+
f.add(l2);
117+
f.add(t2);
118+
f.add(b1);
119+
f.add(b2);
120+
f.add(b3);
121+
122+
f.setLayout(null);
123+
f.setSize(400, 300);
124+
f.setVisible(true);
125+
}
126+
127+
// Driver code
128+
public static void main(String args[])
129+
{
130+
converter();
131+
}
132+
}

0 commit comments

Comments
 (0)