Sie sind auf Seite 1von 15

Micro-Project Report

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.

2.0 Aims/Benefits of the Micro-Project:


Develop the project of Currency converter in Advance java.

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.

4.0 Literature Review:


JSON:-
JSON (JavaScript Object Notation) is a lightweight, text-based, language-
independent data exchange format that is easy for humans and machines to read and
write. JSON can represent two structured types: objects and arrays. An object is an
unordered collection of zero or more name/value pairs. An array is an ordered sequence
of zero or more values. The values can be strings, numbers, Booleans, null, and these
two structured types.
Below is a simple example from Wikipedia that shows JSON representation of an object
that describes a person. The object has string values for first name and last name, a
number value for age, an object value representing the person’s address, and an array
value of phone number objects.

JSON Processing in Java:


The Java API for JSON Processing JSON.simple is a simple Java library that allow
parse, generate, transform, and query JSON.

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.

The curly bracket {} represents JSON object.

JsonObject getJsonObject(String name)


Returns the object value to which the specified name is mapped. This is a convenience method
for (JsonObject)get(name) to get the value.

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.

5.0 Actual Methodology Followed:

Call_me(String str1,String str2):-


This function return the double value of currency. First create a URL object for locate
and retrieve data of currency. Firstly create the object HttpURLConnection and then, data is
retrieved in HttpURLConnection object from URL object. The data is retrieved using
getInputStream() . The binary data collected into InputStream and then by using
BufferedReader the binary data is converted into string format and by using append method
the data is appended in StringBuffered object. JSONObject is used to convert the JSON code
into the java code using toString().

Methods used:-

1) public int getSelectedIndex()

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.

2) public String getText()

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.

3) Layout used in project:-

GroupLayout is a LayoutManager that hierarchically groups components in order to position


them in a Container. GroupLayout is intended for use by builders, but may be hand-coded as
well. Grouping is done by instances of the Group class. GroupLayout supports two types of
groups. A sequential group positions its child elements sequentially, one after another. A
parallel group aligns its child elements in one of four ways.

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;

public class CurrencyConvertor {

private JFrame frmCurrencyConvertor;


private JTextField textField;
private JTextField textField_1;

String []CountryName = {"United Arab Emeriates dirham","Argentine


peso","Australian dollar","Bulgarian lev","Brazilian real ","Bahamian
doller","Canadian doller","Swiss franc","Chilean peso ","Chinese yuan","Colombian
peso","Czech koruna","Denmark","Dominican peso","Egyptian pound","Germany
euro","Fijian doller","British pound","Guatemalan quetzal","Hong Kong
dollar","Croatian kuna","Hungarian forint","Indonesian rupiah","Israeli new
shekel","Indian rupees","Icelandic krona","Japanese yen","South Korean
won","Kazakhstani tenge","Mexican peso","Malaysian ringgit","Norwegian
krone","New Zealand Dollar","Panamanian balboa","Peruvian sol","Philippine
peso","Pakistani rupees","Polish zloty","Paraguayan guarani","Romanian
leu","Russian ruble","Saudi riyal","Swedish Krona","Singapore dollar","Thai
baht","Turkish lira","New Taiwan dollar","Ukraine hryvnia","United States
dollar","Uruguayan peso","Vietnamese domg","South African rand"};

String []CountryCode= {"AED","ARS","AUD","BGN","BRL","BSD",


"CAD","CHF","CLP","CNY","COP","CZK",
"DKK","DOP","EGP","EUR","FJD","GBP",
"GTQ","HKD","HRK","HUF","IDR","ILS",
"INR","ISK","JPY","KRW","KZT","MXN",
"MYR","NOK","NZD","PAB","PEN","PHP",
"PKR","PLN","PYG","RON","RUB","SAR",
"SEK","SGD","THB","TRY","TWD","UAH",
"USD","UYU","VND","ZAR"};

/**
* Launch the application.
*/

public static void main(String[] args) {


EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CurrencyConvertor window = new
CurrencyConvertor();
window.frmCurrencyConvertor.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

//connection

public static double call_me(String from,String to) throws Exception {


String url = "https://api.exchangerate-api.com/v4/latest/"+from;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new
InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}

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);

JLabel lblCurrencyConvertor = new JLabel("Currency Converter");


lblCurrencyConvertor.setHorizontalAlignment(SwingConstants.CENTER);
lblCurrencyConvertor.setFont(new Font("Arial Black", Font.BOLD, 28));
GridBagConstraints gbc_lblCurrencyConvertor = new GridBagConstraints();
gbc_lblCurrencyConvertor.anchor = GridBagConstraints.NORTH;
gbc_lblCurrencyConvertor.fill = GridBagConstraints.HORIZONTAL;
gbc_lblCurrencyConvertor.insets = new Insets(0, 0, 5, 0);
gbc_lblCurrencyConvertor.gridx = 0;
gbc_lblCurrencyConvertor.gridy = 0;

frmCurrencyConvertor.getContentPane().add(lblCurrencyConvertor,gbc_lblCurrency
Convertor);

JPanel panel = new JPanel();


GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 0;
gbc_panel.gridy = 1;
frmCurrencyConvertor.getContentPane().add(panel, gbc_panel);

JLabel lblNewLabel = new JLabel("Select Currency");


lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 20));
lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);

JComboBox FromConvert = new JComboBox(CountryName);

JComboBox ToConvert = new JComboBox(CountryName);

textField = new JTextField();


textField_1 = new JTextField();
textField_1.setColumns(10);
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
int convert = Integer.parseInt(textField.getText());
int selectIndex = FromConvert.getSelectedIndex();
int selectIndex1 = ToConvert.getSelectedIndex();
String code1 = CountryCode[selectIndex];
String code2 = CountryCode[selectIndex1];

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);

JLabel lblCurrencyToConvert = new JLabel("Currency To Convert In");


lblCurrencyToConvert.setHorizontalAlignment(SwingConstants.LEFT);
lblCurrencyToConvert.setFont(new Font("Times New Roman", Font.BOLD,
20));

JButton btnConvert = new JButton("Convert");


btnConvert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int convert = Integer.parseInt(textField.getText());
int selectIndex = FromConvert.getSelectedIndex();
int selectIndex1 = ToConvert.getSelectedIndex();
String code1 = CountryCode[selectIndex];
String code2 = CountryCode[selectIndex1];

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");

}
}
});

JButton btnReset = new JButton("Reset");


btnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText("");
textField_1.setText("");
ToConvert.setSelectedIndex(0);
FromConvert.setSelectedIndex(0);
}
});

JButton btnExit = new JButton("Exit");


btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);

}
});

JLabel lblNewLabel_1 = new JLabel("Enter the Amount");


lblNewLabel_1.setFont(new Font("Times New Roman", Font.BOLD, 20));

JLabel lblResult = new JLabel("Result");


lblResult.setFont(new Font("Times New Roman", Font.BOLD, 20));

GroupLayout gl_panel = new GroupLayout(panel);

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);
}
}

6.0 Actual Resources Used (Mention the actual resources used):

S. Name of Specifications Qty Remarks


No. Resource/material
1 Laptop I5 , 8GB ram 1
2 Java JDK 1.8 -
3 Java JRE1.8 -
4 Json Json1.1 -
5 Eclipse 2019 -

7.0 Outputs of the Micro-Projects:

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

Sr. No Topic Page No

1.0 Rationale 1

2.0 Aim and Benefits of micro project 2

3.0 Course Outcomes 3

4.0 Literature Review 4

5.0 Actual Methodology Followed 5

6.0 Actual Resources Used 8

7.0 Output 9

8.0 Learning Outcome of Micro Project 11

9.0 Application of Micro Project 12

10.0 Conclusion 13

Das könnte Ihnen auch gefallen