Difference between revisions of "Currency Converter"

From CompSciWiki
Jump to: navigation, search
(Added quotations to student quotes)
 
(6 intermediate revisions by 3 users not shown)
Line 2: Line 2:
  
 
|Problem=
 
|Problem=
This program will convert dollar amounts from Canadian to US dollars, and vice versa. There will be three input dialog boxes, implemented using JOptionPane.showInputDialog. The first input will ask the user for the currency to which they would like to convert, either CAD or USD. The next input asks for the current conversion rate to the desired currency, and the final input asks for the dollar amount to be converted. The program will then display a dialog box with the dollar amounts in both the original currency and the converted amount in the new currency.
+
This program will convert dollar amounts from Canadian to US dollars, and vice versa. There will be
 +
three input dialog boxes, implemented using JOptionPane.showInputDialog. The first input will ask  
 +
the user for the currency to which they would like to convert, either CAD or USD. The next input
 +
asks for the current conversion rate to the desired currency, and the final input asks for the  
 +
dollar amount to be converted. The program will then display a dialog box with the dollar amounts
 +
in both the original currency and the new currency.
 
<br><br>
 
<br><br>
This program will require nesting of if statements and loops within one another as well as string comparisions, and there should be some input checking to ensure that inputs are valid and that all dollar amounts are greater than or equal to zero. Use a boolean variable called done to check whether the value of the first input is null. If the user clicks cancel, the program will then exit.
+
This program will require nesting of if statements and loops within one another as well as string
 +
comparisions. There should also be some input checking to ensure that inputs are valid, and that
 +
all dollar amounts are greater than or equal to zero. Use a boolean variable called done to check
 +
whether the value of the first input is null. If the user clicks Cancel, the program will exit.
  
  
|SideSectionTitle=By Students...
+
|SideSectionTitle=...By Students
 
|SideSection=
 
|SideSection=
Figuring out how to properly nest ifs and loops and put them in the correct order is one of the most important aspects of program design. A long session of debugging can often come down to a set of nested ifs and loops that wasn't properly structured, whether it be compile errors, runtime errors, or logical errors. From programs that sort data to programs that perform complex simulations, loops and decision structures are the basic building blocks of all the programs you will write.
+
"Debugging tends to be one of the most frustrating parts of programming, and it can take a lot of
 +
practice to get your programs working correctly on the first try. Although the error messages will
 +
always tell you what went wrong, they won't always tell you where or why the error occurred. When I
 +
have difficulty debugging a program, I often make a diagram or calculate by hand exactly what my
 +
code is doing. I find this to be the fastest way to determine what the problem is when it's not
 +
obvious. From programs that sort data to programs that perform complex simulations, this has always
 +
been a helpful strategy when I get stuck."
  
 
|Solution=
 
|Solution=
 
First we declare the variables we will need to solve the problem.
 
First we declare the variables we will need to solve the problem.
<pre>
+
{{CodeBlock
 +
|Code=
 +
//declare the variables
 
String currencyTo;
 
String currencyTo;
 
String currencyFrom;
 
String currencyFrom;
Line 22: Line 38:
 
double USDtoCAD;
 
double USDtoCAD;
 
boolean done = false;
 
boolean done = false;
</pre>
+
}}
Next, we open the main while loop. The program will continue to run until the user clicks Cancel on one of the input dialog boxes. This will cause done to equal true, and the program will exit the loop.
+
Next, we open the main while loop. The program will continue to run until the user clicks Cancel on
<pre>
+
one of the input dialog boxes.
 +
<br>
 +
This will cause done to equal true, and the program will exit the loop.
 +
{{CodeBlock
 +
|Code=
 
while(!done)
 
while(!done)
 
{
 
{
 
     //all of the code will go in here
 
     //all of the code will go in here
 
}
 
}
</pre>
+
}}
|SolutionCode =  
+
The first input from the user will be the currency that they want to convert to. Since we are only
<pre>
+
converting between two currencies, the other currency will obviously be the one we are converting
import java.swing.*;
+
from. Note that if currency is null, the program will not enter this section of code. If the user entered something other than "CAD" or "USD", we will keep looping until one of these two values is entered.
 +
{{CodeBlock
 +
|Code=
 +
//get the currency to which we will be converting
 +
currencyTo = JOptionPane.showInputDialog("Enter the currency you are converting to " +
 +
            "- type either CAD or USD:");
 +
 
 +
if(currencyTo != null)
 +
    {
 +
        while(!currencyTo.equals("CAD") && !currency.equals("USD"))
 +
{
 +
    currencyTo = JOptionPane.showInputDialog("Enter the currency you are converting to " +
 +
                        "- type either CAD or USD:");
 +
}
 +
}}
 +
Next we will get the current conversion rate from the user. Although we could just hard code this into the program by initializing the conversion rate variables, doing it this way makes the program more flexible.
 +
{{CodeBlock
 +
|Code=
 +
//get the current rate
 +
if(currencyTo.equals("CAD"))
 +
{
 +
    input = JOptionPane.showInputDialog("Enter the current USD to CAD conversion rate");
 +
 
 +
    if(input != null)
 +
    {
 +
        USDtoCAD = Double.parseDouble(input);
 +
    }
 +
}
 +
else  //currency is USD
 +
{
 +
    input = JOptionPane.showInputDialog("Enter the current CAD to USD conversion rate");
 +
 
 +
    if(input != null)
 +
    {
 +
        CADtoUSD = Double.parseDouble(input);
 +
    }
 +
}
 +
}}
 +
Now we will get the user to enter the amount they wish to convert, making sure that the amount they enter is not null and that it is greater than or equal to zero. Notice that we must convert input
 +
from a string to a double on each iteration, otherwise we will get an error in the second loop condition when we try to compare mismatched data types.
 +
{{CodeBlock
 +
|Code=
 +
//get an amount to convert
 +
input = JOptionPane.showInputDialog("Enter the dollar amount to be converted.");
 +
 
 +
while(input == null || amount < 0)
 +
{
 +
    input = JOptionPane.showInputDialog("Enter the dollar amount to be converted.");
 +
 
 +
    if(input != null)
 +
    {
 +
        amount = Double.parseDouble(input);
 +
    }
 +
}
 +
}}
 +
The final step is to perform the conversion and print the result.
 +
{{CodeBlock
 +
|Code=
 +
//perform the conversion
 +
if(currencyTo.equals("CAD"))
 +
{
 +
    newAmount = amount * USDtoCAD;
 +
    currencyFrom = "USD";
 +
}
 +
else
 +
{
 +
    newAmount = amount * CADtoUSD;
 +
    currencyFrom = "CAD";
 +
}
 +
 
 +
//print the result
 +
JOptionPane.showMessageDialog("$" + amount + " " + currencyFrom + " = $" + newAmount + " " +
 +
                              currencyTo);
 +
}}
 +
|SolutionCode=import java.swing.*;
  
 
public class CurrencyConverter
 
public class CurrencyConverter
 
{
 
{
public static void main(String[] args)
+
    public static void main(String[] args)
{
+
    {
String currencyTo;
+
        //declare the variables
String currencyFrom;
+
        String currencyTo;
String input;
+
String currencyFrom;
double amount = 0;
+
String input;
double newAmount = 0;
+
double amount = 0;
double CADtoUSD;
+
double newAmount = 0;
double USDtoCAD;
+
double CADtoUSD;
boolean done = false;
+
double USDtoCAD;
 +
boolean done = false;
 
 
  
while(!done)
+
while(!done)
{
+
{
//get the currency to which we will be converting
+
    //get the currency to which we will be converting
currencyTo = JOptionPane.showInputDialog("Enter the currency you are converting to - type either CAD or USD:);
+
    currencyTo = JOptionPane.showInputDialog("Enter the currency you are converting to " +
 +
                        "- type either CAD or USD:");
  
if(currencyTo != null)
+
    if(currencyTo != null)
{
+
    {
while(!currencyTo.equals("CAD") && !currency.equals("USD"))
+
        while(!currencyTo.equals("CAD") && !currency.equals("USD"))
{
+
        {
currencyTo = JOptionPane.showInputDialog("Enter a currency - type either CAD or USD:);
+
            currencyTo = JOptionPane.showInputDialog("Enter the currency you are" +
}
+
                                " converting to - type either CAD or USD:");
 +
        }
  
//get the current rate
+
        //get the current rate
if(currencyTo.equals("CAD"))
+
        if(currencyTo.equals("CAD"))
{
+
        {
input = JOptionPane.showInputDialog("Enter the current USD to CAD conversion rate");
+
            input = JOptionPane.showInputDialog("Enter the current USD to CAD conversion" +
 +
                            " rate");
  
if(input != null)
+
    if(input != null)
{
+
    {
USDtoCAD = Double.parseDouble(input);
+
        USDtoCAD = Double.parseDouble(input);
}
+
    }
}
+
        }
else   //currency has to be USD or program won't have gotten past the above while loop
+
        else //currency is USD
{
+
        {
input = JOptionPane.showInputDialog("Enter the current CAD to USD conversion rate");
+
    input = JOptionPane.showInputDialog("Enter the current CAD to USD conversion" +
 +
                            " rate");
  
if(input != null)
+
    if(input != null)
{
+
    {
CADtoUSD = Double.parseDouble(input);
+
          CADtoUSD = Double.parseDouble(input);
}
+
    }
}
+
        }
  
//get an amount to convert
+
                //get an amount to convert
input = JOptionPane.showInputDialog("Enter the dollar amount, in decimal, to be converted.");
+
        input = JOptionPane.showInputDialog("Enter the dollar amount to be converted.");
  
                                while(input == null || amount < 0)
+
                while(input == null {{!}}{{!}} amount < 0)
{
+
        {
input = JOptionPane.showInputDialog("Enter the dollar amount, in decimal, to be converted.");
+
            input = JOptionPane.showInputDialog("Enter the dollar amount to be converted.");
                                        if(input != null)
+
                    if(input != null)
                                        {
+
                    {
                                            amount = Double.parseDouble(input);
+
                        amount = Double.parseDouble(input);
                                        }
+
                    }
}
+
        }
  
//perform the conversion
+
        //perform the conversion
if(currencyTo.equals("CAD"))
+
        if(currencyTo.equals("CAD"))
{
+
        {
newAmount = amount * USDtoCAD;
+
            newAmount = amount * USDtoCAD;
currencyFrom = "USD";
+
    currencyFrom = "USD";
}
+
        }
else
+
        else
{
+
        {
newAmount = amount * CADtoUSD;
+
            newAmount = amount * CADtoUSD;
currencyFrom = "CAD";
+
    currencyFrom = "CAD";
}
+
        }
  
//print the result
+
        //print the result
JOptionPane.showMessageDialog("$" + amount + " " + currencyFrom + " = $" + newAmount + " " + currencyTo);
+
        JOptionPane.showMessageDialog("$" + amount + " " + currencyFrom + " = $" +
}
+
                                              newAmount + " " + currencyTo);
else
+
            }
{
+
    else
done = true;
+
    {
}
+
        done = true;
}
+
    }
}
+
        }
 +
    }
 
}
 
}
</pre>
 
 
}}
 
}}

Latest revision as of 15:23, 8 December 2011

Back to the Program-A-Day homepage

Problem

This program will convert dollar amounts from Canadian to US dollars, and vice versa. There will be three input dialog boxes, implemented using JOptionPane.showInputDialog. The first input will ask the user for the currency to which they would like to convert, either CAD or USD. The next input asks for the current conversion rate to the desired currency, and the final input asks for the dollar amount to be converted. The program will then display a dialog box with the dollar amounts in both the original currency and the new currency.

This program will require nesting of if statements and loops within one another as well as string comparisions. There should also be some input checking to ensure that inputs are valid, and that all dollar amounts are greater than or equal to zero. Use a boolean variable called done to check whether the value of the first input is null. If the user clicks Cancel, the program will exit.

 

...By Students

"Debugging tends to be one of the most frustrating parts of programming, and it can take a lot of practice to get your programs working correctly on the first try. Although the error messages will always tell you what went wrong, they won't always tell you where or why the error occurred. When I have difficulty debugging a program, I often make a diagram or calculate by hand exactly what my code is doing. I find this to be the fastest way to determine what the problem is when it's not obvious. From programs that sort data to programs that perform complex simulations, this has always been a helpful strategy when I get stuck."

Solution

First we declare the variables we will need to solve the problem.

 //declare the variables
String currencyTo;
String currencyFrom;
String input;
double amount = 0;
double newAmount = 0;
double CADtoUSD;
double USDtoCAD;
boolean done = false; 

Next, we open the main while loop. The program will continue to run until the user clicks Cancel on one of the input dialog boxes.
This will cause done to equal true, and the program will exit the loop.

 while(!done)
{
    //all of the code will go in here
} 

The first input from the user will be the currency that they want to convert to. Since we are only converting between two currencies, the other currency will obviously be the one we are converting from. Note that if currency is null, the program will not enter this section of code. If the user entered something other than "CAD" or "USD", we will keep looping until one of these two values is entered.

 //get the currency to which we will be converting
currencyTo = JOptionPane.showInputDialog("Enter the currency you are converting to " +
             "- type either CAD or USD:");

if(currencyTo != null)
    {
        while(!currencyTo.equals("CAD") && !currency.equals("USD"))
	{
	    currencyTo = JOptionPane.showInputDialog("Enter the currency you are converting to " +
                         "- type either CAD or USD:");	
	} 

Next we will get the current conversion rate from the user. Although we could just hard code this into the program by initializing the conversion rate variables, doing it this way makes the program more flexible.

 //get the current rate
if(currencyTo.equals("CAD"))
{
    input = JOptionPane.showInputDialog("Enter the current USD to CAD conversion rate");

    if(input != null)
    {
        USDtoCAD = Double.parseDouble(input);
    }
}
else   //currency is USD
{
    input = JOptionPane.showInputDialog("Enter the current CAD to USD conversion rate");

    if(input != null)
    {
        CADtoUSD = Double.parseDouble(input);
    }
} 

Now we will get the user to enter the amount they wish to convert, making sure that the amount they enter is not null and that it is greater than or equal to zero. Notice that we must convert input from a string to a double on each iteration, otherwise we will get an error in the second loop condition when we try to compare mismatched data types.

 //get an amount to convert
input = JOptionPane.showInputDialog("Enter the dollar amount to be converted.");

while(input == null 

The final step is to perform the conversion and print the result.

 //perform the conversion
if(currencyTo.equals("CAD"))
{
    newAmount = amount * USDtoCAD;
    currencyFrom = "USD";
}
else
{
    newAmount = amount * CADtoUSD;
    currencyFrom = "CAD";
}

//print the result
JOptionPane.showMessageDialog("$" + amount + " " + currencyFrom + " = $" + newAmount + " " +
                              currencyTo); 

Code

Solution Code

Back to the Program-A-Day homepage