 |
|
|
NOKExchangeRateCalculator.java
|
/*
* Author: Havard Rast Blok
* Web : www.rememberjava.com
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
/**
* Reads a web page and extracts exchange rate information. This information is
* used in a simple calculator.
*
* It is designed for the "Aftenposten valutakurser" web page:
* http://www.aftenposten.no/nyheter/okonomi/ However, it should be fairly easy
* to adjust to other's.
*
* IMPORTANT! Aftenposten is a registered tradmark, and this program is for
* educational purposes only.
*/
public class NOKExchangeRateCalculator extends JFrame implements ItemListener,
KeyListener {
private final String startRates = "Reelle vekslingskurser fra DnBNOR";
private final String startInfo = "</td>";
private final String endRates = "</tbody></table>";
private final int currencies = 45;
// GUI
private Container c;
private JLabel localLabel;
private JTextField localText, foreignText;
private JComboBox currency;
private float rate;
private String currencyNames[] = new String[currencies];;
private float rates[] = new float[currencies];
public NOKExchangeRateCalculator(URL theURL) throws IOException {
super("Exchange Rate Calculator");
// load exchange rates
loadRates(theURL);
// set up GUI
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 150);
c = getContentPane();
c.setLayout(new GridLayout(2, 2));
localLabel = new JLabel("Local currency");
localText = new JTextField();
localText.addKeyListener(this);
foreignText = new JTextField();
foreignText.addKeyListener(this);
currency = new JComboBox(currencyNames);
currency.addItemListener(this);
c.add(localLabel);
c.add(localText);
c.add(currency);
c.add(foreignText);
// update current rate by faking selection
itemStateChanged(null);
pack();
setVisible(true);
}
/**
* Update the local currency text field based upon the value in the foreign
* text field.
*/
private void updateLocal() {
float value;
try {
value = Float.parseFloat(foreignText.getText());
localText.setText("" + (value * rate));
} catch (NumberFormatException e) {
System.out.println("ExchangeRateCalculator.updateLocal - error parsing: "+ e);
}
}
/**
* Update the foreign currency text field based upon the value in the local
* text field.
*/
private void updateForegin() {
float value;
try {
value = Float.parseFloat(localText.getText());
foreignText.setText("" + (value * rate));
} catch (NumberFormatException e) {
System.out
.println("ExchangeRateCalculator.updateForegin - error parsing: "
+ e);
}
}
public void keyTyped(KeyEvent e) {
// System.out.println("keyTyped");
}
public void keyPressed(KeyEvent e) {
// System.out.println("keyPressed");
}
/**
* Detect a change in the text field and change the calulated value.
*/
public void keyReleased(KeyEvent e) {
// System.out.println("keyReleased");
Object source = e.getSource();
if (source == localText) {
updateForegin();
} else if (source == foreignText) {
updateLocal();
}
}
/**
* Sets the current rate based upon the selected country from the list.
*/
public void itemStateChanged(ItemEvent e) {
int index = currency.getSelectedIndex();
rate = rates[index];
localText.setText("0.0");
foreignText.setText("0.0");
}
/**
* Load the exchange rates from web (or a local file) (from the week 48-2001
* program)
*
* @param theURL
* web or local URL for the file
*/
public void loadRates(URL theURL) throws IOException {
// open web page
BufferedReader bufReader = new BufferedReader(new InputStreamReader(
theURL.openStream()));
boolean foundRates = false;
int colCounter = 0;
int rowCounter = 0;
// read line by line
String line;
while ((line = bufReader.readLine()) != null) {
// check if the exchange rate table is found
if (!foundRates) {
// if not, check if it starts in this line
foundRates = (line.indexOf(startRates) != -1);
}
// check if this line contains interesing information
else if (line.indexOf(startInfo) != -1) {
String numbers = line.replaceAll("[^0-9,]", "");
String uppercase = line.replaceAll("[^A-Z]", "");
// if it does, check if it is of the first two coloumns
if (colCounter < 2) {
// extract the information
// (no number conversion is done here)
if (uppercase != null && uppercase.length() >= 3) {
currencyNames[rowCounter] = uppercase;
colCounter++;
} else if (numbers != null && numbers.length() >= 4) {
//replace Norwegian decimal character (,) with English (.)
String engNumber = numbers.replace(',', '.');
rates[rowCounter] = Float.parseFloat(engNumber);
colCounter++;
}
} else {
// end of row (both on web page and in array)
rowCounter++;
colCounter = 0;
}
}
// check if exchange rate table is finished
else if (line.indexOf(endRates) != -1) {
break;
}
}
// close the file when finished
bufReader.close();
}
public static void main(String args[]) {
try {
new NOKExchangeRateCalculator(new URL(
"file:///media/USBDISK/todo/currency.page.htm"));
// Open web page
// new ExchangeRateCalculator(new URL(args[0]));
// Open from local file
// File f = new File( args[0] );
// new ExchangeRateCalculator( f.toURL() );
} catch (MalformedURLException e) {
System.err.println("ExchangeRateCalculator.main - wrong url: " + e);
} catch (IOException e){
System.err.println("Error reading or parsing url: "+e);
}
}
}
|
|
|
 |