-
Notifications
You must be signed in to change notification settings - Fork 0
/
Finance_Hub.java
187 lines (149 loc) · 6.52 KB
/
Finance_Hub.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.test;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.io.*;
import java.net.URL;
//-----------------------------------------------------------------------------
class Finance_Hub extends JFrame{
public JLabel poundJLabel;
public JCheckBox rupeeJCheckBox, euroJCheckBox, dollarJCheckBox, yenJCheckBox;
public JTextField poundJTextField, rupeeJTextField,euroJTextField, dollarJTextField, yenJTextField;
public JButton convertJButton;
//-----------------------------------------------------------------------------
private static Double findExchangeRateAndConvert(String from, String to, int amount) {
try {
//Yahoo Finance API
URL url = new URL("http://finance.yahoo.com/d/quotes.csv?f=l1&s="+ from + to + "=X");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line = reader.readLine();
if (line.length() > 0) {
return Double.parseDouble(line) * amount;
}
reader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
//-----------------------------------------------------------------------------
public Finance_Hub(){
creatGUI();
}
//-----------------------------------------------------------------------------
public void creatGUI(){
Container contentPane = getContentPane();
contentPane.setLayout( null );
poundJLabel = new JLabel();
poundJLabel.setText( "Amount £: " );
poundJLabel.setBounds(20,20,120,30);
contentPane.add(poundJLabel);
rupeeJCheckBox = new JCheckBox();
rupeeJCheckBox.setText( "Rupee" );
rupeeJCheckBox.setBounds(20,60,100,20);
contentPane.add(rupeeJCheckBox);
euroJCheckBox = new JCheckBox();
euroJCheckBox.setText( "Euro" );
euroJCheckBox.setBounds(20,100,100,20);
contentPane.add(euroJCheckBox);
dollarJCheckBox = new JCheckBox();
dollarJCheckBox.setText( "Dollar" );
dollarJCheckBox.setBounds(20,140,100,20);
contentPane.add(dollarJCheckBox);
yenJCheckBox = new JCheckBox();
yenJCheckBox.setText( "Yen" );
yenJCheckBox.setBounds(20,180,100,20);
contentPane.add(yenJCheckBox);
poundJTextField = new JTextField();
poundJTextField.setBounds(130,20,80,30);
poundJTextField.setHorizontalAlignment(JTextField.RIGHT);
poundJTextField.setText( "0" );
contentPane.add( poundJTextField);
poundJTextField.addKeyListener(
new KeyAdapter(){
public void keyPressed(KeyEvent e){
poundsKeyPressed(e);
}
}
);
//-------------------------------------------------------------------------------
rupeeJTextField = new JTextField();
rupeeJTextField.setBounds(130,60,80,20);
rupeeJTextField.setHorizontalAlignment(JTextField.RIGHT);
rupeeJTextField.setText( "R 0.00" );
rupeeJTextField.setEditable(false);
contentPane.add(rupeeJTextField);
euroJTextField = new JTextField();
euroJTextField.setBounds(130,100,80,20);
euroJTextField.setHorizontalAlignment(JTextField.RIGHT);
euroJTextField.setText( "€ 0.00" );
euroJTextField.setEditable(false);
contentPane.add(euroJTextField);
dollarJTextField = new JTextField();
dollarJTextField.setBounds(130,140,80,20);
dollarJTextField.setHorizontalAlignment(JTextField.RIGHT);
dollarJTextField.setText( "$ 0.00" );
dollarJTextField.setEditable(false);
contentPane.add(dollarJTextField);
yenJTextField = new JTextField();
yenJTextField.setBounds(130,180,80,20);
yenJTextField.setHorizontalAlignment(JTextField.RIGHT);
yenJTextField.setText( "¥ 0.00" );
yenJTextField.setEditable(false);
contentPane.add(yenJTextField);
convertJButton = new JButton();
convertJButton.setText( "Calculate Foreign Currency" );
convertJButton.setBounds(20,220,200,30);
contentPane.add(convertJButton);
convertJButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
convertActionPerformed(e);
}
}
);
setTitle( "Currency Converter" );
setSize(250,300);
setVisible(true);
}
//-------------------------------------------------------------------
public void convertActionPerformed(ActionEvent e){
String pound = poundJTextField.getText();
double amount = Double.parseDouble(pound);
String currency = rupeeJTextField.getText();
if(rupeeJCheckBox.isSelected()){
DecimalFormat dfr = new DecimalFormat( "R 0.00" );
double rupeeAmount = amount * findExchangeRateAndConvert("GBP", "INR", 1);
rupeeJTextField.setText(dfr.format(rupeeAmount));
}
if(euroJCheckBox.isSelected()){
DecimalFormat dfr = new DecimalFormat( "€ 0.00" );
double euroAmount = amount * findExchangeRateAndConvert("GBP", "EUR", 1);
euroJTextField.setText(dfr.format(euroAmount));
}
if(dollarJCheckBox.isSelected()){
DecimalFormat dfd = new DecimalFormat( "$ 0.00" );
double dollarAmount = amount * findExchangeRateAndConvert("GBP", "USD", 1);
dollarJTextField.setText(dfd.format(dollarAmount));
}
if(yenJCheckBox.isSelected()){
DecimalFormat dfd = new DecimalFormat( "¥ 0.00" );
double yenAmount = amount * findExchangeRateAndConvert("GBP", "JPY", 1);
yenJTextField.setText(dfd.format(yenAmount));
}
};
//---------------------------------------------------------------------------------
public void poundsKeyPressed(KeyEvent e){
rupeeJTextField.setText( "0" );
euroJTextField.setText( "0" );
dollarJTextField.setText( "0" );
yenJTextField.setText( "0" );
}
//---------------------------------------------------------------------------------
public static void main(String [] args){
Finance_Hub converter = new Finance_Hub();
converter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}