Results 1 to 2 of 2
  1. #1
    Ejava is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default 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
    Java 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
    Java 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();
                 }
                        
    
           }
             
        }

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    4

    Default

    1. Try recalling your main method with choice = 1, or the section you want.

    your switch statements seem very illogical.

    normally I would do something like

    String Choice = "menu info 1. us 2. euro... 5, Exit"

    switch(Choice)
    case 1: CALL A METHOD FOR US CONVERSION in your main
    case 2: CALL A METHOD FOR EURO CONVERSION in your main
    case 5: system.exit();

Similar Threads

  1. Replies: 4
    Last Post: 01-17-2010, 11:13 PM
  2. Currency Converter!!!!
    By Pascal Nouma in forum AWT / Swing
    Replies: 2
    Last Post: 04-02-2009, 07:23 AM
  3. Convert Currency to Words in English
    By javanewbie in forum JCreator
    Replies: 5
    Last Post: 03-29-2009, 07:25 AM
  4. Formatting a number to currency
    By Java Tip in forum java.text
    Replies: 0
    Last Post: 04-16-2008, 10:59 PM
  5. Setting currency
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-16-2007, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •