Currency Coverter Project
Hi all, thanks for taking the time out to view this post :). I have been working on the below code for a few weeks now and I'm just about complete with it just short of the comments for the main class, it consists of 2 classes. The MoneyConverter class holds the methods and the main class is for bringing up the menu so that the user can make their selections. The issues that I am currently having with it are that Iam trying to get the input box for the currency values to display an error and then immediately return to that input box rather than returning to the main menu. I've discussed this with my friends and they suggest that a break be added after the exception handler but I'm not sure how to implement it.
The second issue is that after you make a selection, the program is saving the choice and is not allowing any other input boxes to show up even if a new selection is made. Thanks again if you take the time to view my code, still fairly new but I want to master it :).
MoneyConverter class
Code:
package Interface;
/**
*
* @author Edrick
*/
public class MoneyConverter {
/** holds the currency in Dollars, Euro , Pounds and Yen */
private float us;
private float euro;
private float pounds;
private float yen;
/** constructor that initializes the currency values */
public MoneyConverter(){
us = 0;
euro = 0;
pounds = 0;
yen = 0;
}
/** sets the us value to what is passed */
public void setUS(float u){
us = u;
}
/** returns the value held in us */
public float getUS(){
return us;
}
/** Sets the euro to what is passed */
public void setEURO(float e){
euro = e;
}
/** Returns the value held in euro */
public float getEURO(){
return euro;
}
/** sets the pounds value to what is passed */
public void setPOUNDS(float p){
pounds = p;
}
/** returns the value held in pounds */
public float getPOUNDS(){
return pounds;
}
/** Sets the yen to what is passed */
public void setYEN(float y){
yen = y;
}
/** Returns the value held in yen */
public float getYEN(){
return yen;
}
/** convert us to euro and return the value */
public static float UStoEuro(float e){
float u = e / 1.34f;
return u;
}
/** convert us to pounds and return the value */
public static float UStoPounds(float p){
float u = p / 1.54f;
return u;
}
/** convert us to yen and return the value */
public static float UStoYen(float y){
float u = y * 93.13f;
return u;
}
/** convert euro to us and return the value */
public static float EurotoUS(float u){
float e = u * 1.34f;
return e;
}
/** convert euro to pounds and return the value */
public static float EurotoPounds(float p){
float e = p / 1.15f;
return e;
}
/** convert euro to yen and return the value */
public static float EurotoYen(float y){
float e = y * 125.77f;
return e;
}
/** convert pounds to us and return the value */
public static float PoundstoUS(float u){
float p = u * 1.54f;
return p;
}
/** convert pounds to euro and return the value */
public static float PoundstoEuro(float e){
float p = e * 1.15f;
return p;
}
/** convert pounds to yen and return the value */
public static float PoundstoYen(float y){
float p = y * 144.49f;
return p;
}
/** convert yen to us and return the value */
public static float YentoUS(float u){
float y = u / 93.97f;
return y;
}
/** convert yen to euro and return the value */
public static float YentoEuro(float e){
float y = e / 125.77f;
return y;
}
/** convert yen to pounds and return the value */
public static float YentoPounds(float p){
float y = p / 144.49f;
return y;
}
}
Main Class
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Interface;
import javax.swing.JOptionPane;
/**
*
* @author Edrick
*/
public class Main {
private static int menuChoice(){
int choice = 0;
String menu = " 1. US$\n 2. Euro€\n 3. GBP£\n 4. JP¥\n 5. Exit";
try {
choice = Integer.parseInt(JOptionPane.showInputDialog(null, menu, "Pocket Currency Converter V1.0" , JOptionPane.QUESTION_MESSAGE));
} catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Invalid Input");
}finally {
return choice;
}
}
private static int usChoice(){
int choice1 = 0;
String menu1 = "1.US$ to Euro€\n 2. US$ to GBP£\n 3. US$ to JP¥\n 4. Exit";
choice1 = Integer.parseInt(JOptionPane.showInputDialog(null, menu1, "US$ Conversions", JOptionPane.QUESTION_MESSAGE));
return choice1;
}
private static int euroChoice(){
int choice2 = 0;
String menu2 = "1. Euro€ to US$\n 2. Euro€ to GBP£\n 3. Euro€ to JP¥\n 4. Exit";
choice2 = Integer.parseInt(JOptionPane.showInputDialog(null, menu2, "Euro€ Conversions", JOptionPane.QUESTION_MESSAGE));
return choice2;
}
private static int poundsChoice(){
int choice3 = 0;
String menu3 = "1. GBP£ to US$\n 2. GBP£ to Euro€\n 3. GBP£ to JP¥\n 4. Exit";
choice3 = Integer.parseInt(JOptionPane.showInputDialog(null, menu3, "GBP£ Conversions", JOptionPane.QUESTION_MESSAGE));
return choice3;
}
private static int yenChoice(){
int choice4 = 0;
String menu4 = "1. JP¥ to US$\n 2. JP¥ to Euro€\n 3. JP¥ to GBP£\n 4. Exit";
choice4 = Integer.parseInt(JOptionPane.showInputDialog(null, menu4, "JP¥ Conversions", JOptionPane.QUESTION_MESSAGE));
return choice4;
}
public static void main(String[] args) {
int choice = menuChoice();
int choice1 = 0;
int choice2 = 0;
int choice3 = 0;
int choice4 = 0;
float dollars = 0.0f;
float pounds = 0.0f;
float euros = 0.0f;
float yen = 0.0f;
MoneyConverter MC = new MoneyConverter();
while(choice!=5){
switch(choice){
case 0:
break;
case 1:
choice1 = usChoice();
while(choice1!=4){
switch(choice1){
case 0:
break;
case 1:
try{
dollars = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of US$ :", "Enter Money", JOptionPane.QUESTION_MESSAGE));
euros = MoneyConverter.UStoEuro(dollars);
JOptionPane.showMessageDialog(null, "The amount in Euro is: " + euros , "€", JOptionPane.INFORMATION_MESSAGE);
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Invalid Input" );
}finally{
}
break;
case 2:
dollars = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of US$ :", "Enter Money", JOptionPane.QUESTION_MESSAGE));
pounds = MoneyConverter.UStoPounds(dollars);
JOptionPane.showMessageDialog(null, "The amount in Pounds is: " + pounds , "£", JOptionPane.INFORMATION_MESSAGE);
break;
case 3:
dollars = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of US$ :", "Enter Money", JOptionPane.QUESTION_MESSAGE));
yen = MoneyConverter.UStoYen(dollars);
JOptionPane.showMessageDialog(null, "The amount in Yen is: " + yen , "¥", JOptionPane.INFORMATION_MESSAGE);
break;
case 4:
break;
default:
//error message
JOptionPane.showMessageDialog(null, "Please make a selection", "Invalid Choice", JOptionPane.INFORMATION_MESSAGE);
break;
}
choice = menuChoice();
}
break;
case 2:
choice2 = euroChoice();
while(choice2!=4){
switch(choice2){
case 0:
break;
case 1:
euros = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of Euro€ : ", "Enter Money" , JOptionPane.QUESTION_MESSAGE));
dollars = MoneyConverter.EurotoUS(euros);
JOptionPane.showMessageDialog(null, "The amount in US is: " + dollars , "¥", JOptionPane.INFORMATION_MESSAGE);
break;
case 2:
euros = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of Euro€ :", "Enter Money", JOptionPane.QUESTION_MESSAGE));
pounds = MoneyConverter.EurotoPounds(euros);
JOptionPane.showMessageDialog(null, "The amount in Pounds is: " + pounds , "£", JOptionPane.INFORMATION_MESSAGE);
break;
case 3:
euros = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of Euro€ : ", "Enter Money" , JOptionPane.QUESTION_MESSAGE));
yen = MoneyConverter.EurotoYen(euros);
JOptionPane.showMessageDialog(null, "The amount in Yen is: " + yen , "$", JOptionPane.INFORMATION_MESSAGE);
break;
case 4:
break;
default:
//error message
JOptionPane.showMessageDialog(null, "Please make a selection", "Invalid Choice", JOptionPane.INFORMATION_MESSAGE);
break;
}
// ends loop
choice = menuChoice();
}
break;
case 3:
choice3 = poundsChoice();
while(choice3!=4){
switch(choice3){
case 0:
break;
case 1:
pounds = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of GBP£ : ", "Enter Money" , JOptionPane.QUESTION_MESSAGE));
dollars = MoneyConverter.PoundstoUS(pounds);
JOptionPane.showMessageDialog(null, "The amount in US is: " + dollars , "$", JOptionPane.INFORMATION_MESSAGE);
break;
case 2:
pounds = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of GBP£ : ", "Enter Money", JOptionPane.QUESTION_MESSAGE));
euros = MoneyConverter.PoundstoEuro(pounds);
JOptionPane.showMessageDialog(null, "The amount in Euro is: " + euros , "€", JOptionPane.INFORMATION_MESSAGE);
break;
case 3:
pounds = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of GBP£ : ", "Enter Money" , JOptionPane.QUESTION_MESSAGE));
yen = MoneyConverter.PoundstoYen(pounds);
JOptionPane.showMessageDialog(null, "The amount in Yen is: " + yen , "¥", JOptionPane.INFORMATION_MESSAGE);
break;
case 4:
break;
default:
//error message
JOptionPane.showMessageDialog(null, "Please make a selection", "Invalid Choice", JOptionPane.INFORMATION_MESSAGE);
break;
}
// ends loop
choice = menuChoice();
}
break;
case 4:
choice4 = yenChoice();
while(choice4!=4){
switch(choice4){
case 0:
break;
case 1:
yen = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of Yen¥ : ", "Enter Money" , JOptionPane.QUESTION_MESSAGE));
dollars = MoneyConverter.YentoUS(yen);
JOptionPane.showMessageDialog(null, "The amount in US is: " + dollars , "$", JOptionPane.INFORMATION_MESSAGE);
break;
case 2:
yen = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of Yen¥ : ", "Enter Money" , JOptionPane.QUESTION_MESSAGE));
euros = MoneyConverter.YentoEuro(yen);
JOptionPane.showMessageDialog(null, "The amount in Euro is: " + euros , "€", JOptionPane.INFORMATION_MESSAGE);
break;
case 3:
yen = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter Amount of Yen¥ : ", "Enter Money" , JOptionPane.QUESTION_MESSAGE));
pounds = MoneyConverter.YentoPounds(yen);
JOptionPane.showMessageDialog(null, "The amount in GBP is: " + pounds , "¥", JOptionPane.INFORMATION_MESSAGE);
break;
case 4:
break;
default:
//error message
JOptionPane.showMessageDialog(null, "Please make a selection", "Invalid Choice", JOptionPane.INFORMATION_MESSAGE);
break;
}
//
choice = menuChoice();
}
break;
case 5:
break;
default:
//error message
JOptionPane.showMessageDialog(null, "Please make a selection", "Invalid Choice", JOptionPane.INFORMATION_MESSAGE);
break;
}
//ends loop
choice = menuChoice();
}
}
}