Beruflich Dokumente
Kultur Dokumente
Currency Converter
1.0 Rationale:
Different countries use different currency, and there is daily variation in these
currencies relative to one another. Those who transfer money from one country to another (one
currency to another) must be updated with the latest currency exchange rates in the market.
Such application can be used by any user, but it is mainly useful for business, shares,
and finance related areas where money transfer and currency exchange takes place on a daily
basis.
In this currency converter app, users are provided with an option to select the type of
conversion, i.e. from “this” currency to “that” currency. This simple feature allows users to
enter amount to be converted (say currency in Dollars), and display the converted amount (say
currency in Euro).
it is useful tool which gives us the value of certain amount of the one currency to be converted
in to different currency.
It is also regarded as the value of one country currency in term of another currency.
3.0 Course Outcomes Addressed (Add to the earlier list if more COs are
addressed):
1) Develop program using GUI Framework.
2) Handle events of AWT and swing component.
3) Develop java program using networking concepts.
Getting Started:
You need to download the json-simple-1.1 jar and put it in your CLASSPATH before
compiling and running the below example codes.
• For importing jar in IDE like eclipse, refer here.
• If you are using maven you may use the following maven
link https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1
JSON-Simple API:
It provides object models for JSON object and array structures. These JSON structures
are represented as object models using types JSONObject and
JSON Object:
JSON object holds key/value pair. Each key is represented as a string in JSON and value can
be of any type. The keys and values are separated by colon. Each key/value pair is separated
by comma.
HttpURLConnection:
Each HttpURLConnection instance is used to make a single request but the underlying
network connection to the HTTP server may be transparently shared by other instances.
Calling the close() methods on the InputStream or OutputStream of an HttpURLConnection
after a request may free network resources associated with this instance but has no effect on
any shared persistent connection. Calling the disconnect() method may close the underlying
socket if a persistent connection is otherwise idle at that time.
Methods used:-
Returns the first item in the list that matches the given item. The result is not always defined if
the JComboBox allows selected items that are not in the list. Returns -1 if there is no selected
item or if the user specified an item which is not in the list.
Returns the text contained in this TextComponent. If the underlying document is null, will give
a NullPointerException. Note that text is not a bound property, so no PropertyChangeEvent is
fired when it changes. To listen for changes to the text, use DocumentListener.
Each group may contain any number of elements, where an element is a Group, Component,
or gap. A gap can be thought of as an invisible component with a minimum, preferred and
maximum size. In addition GroupLayout supports a preferred gap, whose value comes from
LayoutStyle.
Code:-
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import org.json.JSONObject;
/**
* Launch the application.
*/
//connection
in.close();
JSONObject myresponse = new JSONObject(response.toString());
JSONObject myresponse_obj = new
JSONObject(myresponse.getJSONObject("rates").toString());
double d = myresponse_obj.getDouble(""+to);
return d;
}
/**
* Create the application.
*/
public CurrencyConvertor() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmCurrencyConvertor = new JFrame();
frmCurrencyConvertor.setForeground(Color.WHITE);
frmCurrencyConvertor.setTitle("Currency Convertor");
frmCurrencyConvertor.setBounds(100, 100, 723, 488);
frmCurrencyConvertor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{678, 0};
gridBagLayout.rowHeights = new int[]{40, 265, 0};
gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
frmCurrencyConvertor.getContentPane().setLayout(gridBagLayout);
frmCurrencyConvertor.getContentPane().add(lblCurrencyConvertor,gbc_lblCurrency
Convertor);
try {
double val =
CurrencyConvertor.call_me(code1,code2);
double f = val*convert;
textField_1.setText(""+f);
}
catch (Exception e) {
e.printStackTrace();
}
}
catch(NumberFormatException err)
{
textField_1.setText("Please enter valid
amount");
}
}
});
textField.setColumns(9);
try {
double val = CurrencyConvertor.call_me(code1,code2);
double f = val*convert;
textField_1.setText(""+f);
}
catch (Exception eg) {
eg.printStackTrace();
}
}
catch(NumberFormatException err)
{
textField_1.setText("Please enter valid amount");
}
}
});
}
});
gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup().addGap(67)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup().addComponent(btnConvert,
GroupLayout.PREFERRED_SIZE, 106,
GroupLayout.PREFERRED_SIZE).addGap(141)
.addComponent(btnReset,GroupLayout.PREFERRED_SIZE,103,
GroupLayout.PREFERRED_SIZE).addGap(86).addComponent(btnExit,
GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 360,
Short.MAX_VALUE).addPreferredGap(ComponentPlacement.RELATED))
.addComponent(lblNewLabel_1)
.addComponent(FromConvert, GroupLayout.PREFERRED_SIZE, 202,
GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 193,
GroupLayout.PREFERRED_SIZE).addGap(173)))
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(lblCurrencyToConvert, GroupLayout.PREFERRED_SIZE,
204, GroupLayout.PREFERRED_SIZE) .addComponent(ToConvert,
GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 157,
GroupLayout.PREFERRED_SIZE).addComponent(lblResult)).addGap(70)))
.addContainerGap())
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup().addGap(42)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel)
.addComponent(lblCurrencyToConvert))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(FromConvert, GroupLayout.PREFERRED_SIZE, 36,
GroupLayout.PREFERRED_SIZE)
.addComponent(ToConvert, GroupLayout.PREFERRED_SIZE, 36,
GroupLayout.PREFERRED_SIZE)).addGap(18)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_1).addComponent(lblResult))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 36,
GroupLayout.PREFERRED_SIZE).addComponent(textField_1,
GroupLayout.PREFERRED_SIZE, 34,
GroupLayout.PREFERRED_SIZE)).addGap(53)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(btnConvert, GroupLayout.PREFERRED_SIZE, 40,
GroupLayout.PREFERRED_SIZE).addComponent(btnReset,
GroupLayout.PREFERRED_SIZE, 38,
GroupLayout.PREFERRED_SIZE)
.addComponent(btnExit, GroupLayout.PREFERRED_SIZE, 36,
GroupLayout.PREFERRED_SIZE))
.addContainerGap(114, Short.MAX_VALUE))
);
panel.setLayout(gl_panel);
}
}
1) Start-up Page:
2) Selecting Cuurency:
3) Result:
4) Reset Application:
5) Exit Application:
8.0 Applications of this Micro-Project:
1) Banking System.
2) Share Market.
3) E-commerce website.
**************
INDEX
1.0 Rationale 1
7.0 Output 9
10.0 Conclusion 13