PDA

View Full Version : Java Currency Converter



Jax5
November 16th, 2010, 05:16 AM
Hello everyone,

I'm working on a class exercise to create a GUI Currency Converter. It is supposed to read from a supplied rates.txt file and then convert different currencies as selected from a drop-down box.
Here's an example of how it is supposed to work: EXAMPLE (http://www.umsl.edu/~subramaniana/javadocs/CurrencyConverter/launch.jnlp)
I've got the GUI designed and code to read from rates.txt file, but I've hit a little bit of a brick wall now trying to figure how to get the conversion going on.
This is the code I've got so far:


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* CurrencyConverterGUI.java
*
* Created on Nov 8, 2010, 6:30:36 PM
*/
package currencyconverter;
import java.util.*;
import java.io.*;
import java.text.NumberFormat;
import javax.swing.*;

/**
*
* @author Jay
*/
public class CurrencyConverterGUI extends javax.swing.JFrame {
String currentPattern;
private HashMap<String, Double> HM;
private ArrayList<String> KeyList;
/** Creates new form CurrencyConverterGUI */
public CurrencyConverterGUI() {
initComponents();
String[] patternExamples = {
"EUR",
"USD"
};
HM = new HashMap<String, Double>();
KeyList = new ArrayList<String>();
populateHashMap();
currentPattern = patternExamples[0];
CurrencyBox1 = new JComboBox(patternExamples);
CurrencyBox1.setEditable(false);
//CurrencyBox1.addActionListener(this);
}

private void populateHashMap() {
Scanner SC=null;
try{

SC = new Scanner(new FileReader(new File("rates.txt")));
}catch(IOException e){
System.err.println("File Error when reading rates.txt file");
//return;
System.exit(1);
}
while (SC.hasNextLine()){
String[] S = SC.nextLine().split(" ");
HM.put(S[0], Double.valueOf(S[1]));

}
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
InternalFrame = new javax.swing.JInternalFrame();
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
CurrencyBox1 = new javax.swing.JComboBox();
CurrencyBox2 = new javax.swing.JComboBox();
CurrencyText1 = new javax.swing.JTextField();
CurrencyText2 = new javax.swing.JTextField();
ResetButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
InternalFrame.setMaximizable(true);
InternalFrame.setResizable(true);
InternalFrame.setTitle("Java Currency Converter");
InternalFrame.setVisible(true);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel2.setForeground(new java.awt.Color(204, 204, 255));
CurrencyBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "EUR", "INR", "USD", "GBP" }));
CurrencyBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CurrencyBox1ActionPerformed(evt);
}
});
CurrencyBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "EUR", "INR", "USD", "GBP" }));
CurrencyBox2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CurrencyBox2ActionPerformed(evt);
}
});
CurrencyText1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CurrencyText1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, jPanel2Layout.createSequentialGroup()
.addGap(98, 98, 98)
.addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(CurrencyText1, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(CurrencyBox1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(106, 106, 106)
.addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
.addComponent(CurrencyBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(CurrencyText2, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
.addGap(97, 97, 97))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(66, 66, 66)
.addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(CurrencyBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(CurrencyBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(44, 44, 44)
.addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(CurrencyText1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(CurrencyText2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(105, Short.MAX_VALUE))
);
ResetButton.setText("Reset");
ResetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ResetButtonActionPerformed(evt);
}
});
ResetButton.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
ResetButtonKeyReleased(evt);
}
});
javax.swing.GroupLayout InternalFrameLayout = new javax.swing.GroupLayout(InternalFrame.getContentPa ne());
InternalFrame.getContentPane().setLayout(InternalF rameLayout);
InternalFrameLayout.setHorizontalGroup(
InternalFrameLayout.createParallelGroup(javax.swin g.GroupLayout.Alignment.LEADING)
.addGroup(InternalFrameLayout.createSequentialGrou p()
.addGroup(InternalFrameLayout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(InternalFrameLayout.createSequentialGrou p()
.addGap(40, 40, 40)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(InternalFrameLayout.createSequentialGrou p()
.addGap(258, 258, 258)
.addComponent(ResetButton)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
);
InternalFrameLayout.setVerticalGroup(
InternalFrameLayout.createParallelGroup(javax.swin g.GroupLayout.Alignment.LEADING)
.addGroup(InternalFrameLayout.createSequentialGrou p()
.addGroup(InternalFrameLayout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(InternalFrameLayout.createSequentialGrou p()
.addGap(57, 57, 57)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(InternalFrameLayout.createSequentialGrou p()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(26, 26, 26)
.addComponent(ResetButton)
.addContainerGap(39, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addComponent(InternalFrame)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addComponent(InternalFrame)
);
pack();
}// </editor-fold>
private void CurrencyBox1ActionPerformed(java.awt.event.ActionE vent evt) {
// TODO add your handling code here:
CurrencyText1 = new JFormattedTextField(NumberFormat.getInstance());
JComboBox cb = (JComboBox)evt.getSource();
CurrencyConverter currconv = new CurrencyConverter();
String from = (String)cb.getSelectedItem();
String to = (String)this.CurrencyBox1.getSelectedItem();
Double amount = Double.parseDouble(this.CurrencyText1.getText());
CurrencyText1.setText(String.valueOf(currconv.conv ert(from, to, amount)));

}
private void ResetButtonKeyReleased(java.awt.event.KeyEvent evt) {

}
private void CurrencyText1ActionPerformed(java.awt.event.Action Event evt) {
// TODO add your handling code here:
}
private void CurrencyBox2ActionPerformed(java.awt.event.ActionE vent evt) {
JComboBox cb = (JComboBox)evt.getSource();
CurrencyConverter currconv = new CurrencyConverter();
String from = (String)cb.getSelectedItem();
String to = (String)this.CurrencyBox2.getSelectedItem();
Double amount = Double.parseDouble(this.CurrencyText1.getText());
CurrencyText2.setText(String.valueOf(currconv.conv ert(from, to, amount))); // TODO add your handling code here:
}
private void ResetButtonActionPerformed(java.awt.event.ActionEv ent evt) {
CurrencyText1.setText("");
CurrencyText2.setText(""); // TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CurrencyConverterGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox CurrencyBox1;
private javax.swing.JComboBox CurrencyBox2;
private javax.swing.JTextField CurrencyText1;
private javax.swing.JTextField CurrencyText2;
private javax.swing.JInternalFrame InternalFrame;
private javax.swing.JButton ResetButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
// End of variables declaration
}


Any help, tips, suggestions would be much appreciated.

Some Penguin
November 16th, 2010, 10:46 AM
I'm not entirely sure why you would have

(1) the GUI store the HashMap of values read from rates.txt in a private field,
and
(2) farm the conversion to a separate class.

Seems that the conversion code should itself read and store the table, and the GUI should just make calls to it.

lykeion
November 16th, 2010, 01:48 PM
Looking at your code you seem to have used some sort of UI designer. That makes the code very unstructured. You can study the code of this similar example (http://download.oracle.com/javase/tutorial/uiswing/examples/zipfiles/components-ConverterProject.zip) to get a feel on how to write structured code.

But before creating the GUI you should concentrate on getting the core functionality to work. In this case it's simple arithmetics so it shouldn't be so hard:

// vars currencies and rates (later maybe get these from external file)
int USD = 0;
int EUR = 1;
double[] rates = new double[] { 135.962, 100.000 };

// vars conversion (later get these from user input)
int fromCurr = USD;
int toCurr = EUR;
double fromAmount = 75;

// actual conversion
double toAmount = fromAmount / rates[fromCurr] * rates[toCurr];

Jax5
November 16th, 2010, 09:02 PM
I'm not entirely sure why you would have

(1) the GUI store the HashMap of values read from rates.txt in a private field,
and
(2) farm the conversion to a separate class.

Seems that the conversion code should itself read and store the table, and the GUI should just make calls to it.

To be quite honest I'm very new to Java and I'm sure there's alot more that can be done to make the code more efficient.


Looking at your code you seem to have used some sort of UI designer. That makes the code very unstructured. You can study the code of this similar example (http://download.oracle.com/javase/tutorial/uiswing/examples/zipfiles/components-ConverterProject.zip) to get a feel on how to write structured code.

But before creating the GUI you should concentrate on getting the core functionality to work. In this case it's simple arithmetics so it shouldn't be so hard:

// vars currencies and rates (later maybe get these from external file)
int USD = 0;
int EUR = 1;
double[] rates = new double[] { 135.962, 100.000 };

// vars conversion (later get these from user input)
int fromCurr = USD;
int toCurr = EUR;
double fromAmount = 75;

// actual conversion
double toAmount = fromAmount / rates[fromCurr] * rates[toCurr];

I should've mentioned in OP that I used NetBeans for creating the GUI and doing the rest of the programming.
I'll check out the link you provided and see what I can figure.


Thanks both of you for help and insight.