开发者

How to improve my currency exchange program?

开发者 https://www.devze.com 2023-01-24 11:32 出处:网络
I am writing a Java currency exchange program, in which I program 3 foreign currencies for the user to change from US dollars to foreign dollars. I want to break out of the while when user inputs \"00

I am writing a Java currency exchange program, in which I program 3 foreign currencies for the user to change from US dollars to foreign dollars. I want to break out of the while when user inputs "000"

import java.lang.StringBuffer;
import java.io.IOException;

class Project6_5
{
    public static void main(String args[])
    {
        int usa, amount, stop;
        System.out.println("This is a Currencies Exchange program");
        System.out.println("From US Dollar to Foreign Currency");        //Explaining to user
        stop = 0;
        while(true/*stop!=999*/)
        {
            try
            {
                System.out.print("What is your amount of US dollars?");
                System.out.println("                        000 to end Program");
                usa = Integer.parseInt(GCS());
                System.out.println("What currency would you prefer?");
                System.out.println("Avaliable currencies are:");
                System.out.println("1 for Japanese Yen, 2 for Taiwanese Dollar, 3 for Euro");
                amount = Integer.parseInt(GCS());
                if( amount == 1 )
                {
                    System.out.println("Exchange rate");
                    System.out.println("80.75 Yen for 1 Dollar");
                    System.out.println(" ");
                    System.out.println("Your Yen is "+ usa*80.75);
                    System.out.println("________________________________________");
                }
                if( amount == 2 )
                {
                    System.out.println("Exchange rate");
                    System.out.println("30.125 NTD for 1 Dollar");
                    System.out.println(" ");
                    System.out.println("Your NTD is "+ usa*30.125);
                    System.out.println("________________________________________");
                }
                if( amount == 3 )
                {
                    System.out.println("Exchange rate");
                    System.out.println("1.4201 Euro for 1 Dollar");
                    System.out.println(" ");
                    System.out.println("Your Euro is "+ usa*1.4201);
                    System.out.println("________________________________________");
                }
                if(usa==000)
                {
                    System.out.println("End Program");
                    return;
                }       
            }
            catch(NumberFormatException NFE)  //catch if integer's not number
            {
                System.err.println("ERROR");
                System.err.println("Type in Integer only");
            }
            catch(Exception E)  //catch Genaral Error
            {
                System.err.println("ERROR");
            }
        }
    }//end of Main
    public static String GCS()  //GetConsoleString  User-Input
    {
        int noMoreInput = -1;
        char enterKeyHit = '\n';
        int InputChar;
        StringBuffer InputBuffer = new StringBuffer(100);

        try
        {
            InputChar = System.in.read();
            while(InputC开发者_高级运维har != noMoreInput)
            {
                if((char)InputChar != enterKeyHit)
                {
                    InputBuffer.append((char) InputChar);
                }
                else
                {
                    InputBuffer.setLength(InputBuffer.length()-1);
                    break;
                }
                InputChar = System.in.read();
            }
        }//close Try(21)
        catch(IOException IOX)
        {
            System.err.println(IOX);
        }
        return InputBuffer.toString();
    }//end of GCS
}


Move this block of code:

            if(usa==000) 
            { 
                System.out.println("End Program"); 
                return; 
            }        

To just below:

            usa = Integer.parseInt(GCS());  

Because you want to check if the user has entered 000 straight after they hit return.

And I would suggest replacing return with:

            System.exit(0);

This shows your clear intention to exit the program and that this is a normal termination.


Whenever you see yourself writing the same block of code with very little differences more than once, you should probably refactor that block into a method.

From your code:

if( amount == 1 ) 
{ 
    System.out.println("Exchange rate"); 
    System.out.println("80.75 Yen for 1 Dollar"); 
    System.out.println(" "); 
    System.out.println("Your Yen is "+ usa*80.75); 
    System.out.println("________________________________________"); 
} 
if( amount == 2 ) 
{ 
    //all the same as above except for 30.125 instead of 80.75
    //and NTD instead of Yen 
} 
//...

The method should take in the values that change and then repeat your statements, outputting the variables instead of hardcoded numbers and currency types.

Note that your program does not meet your specifications currently because you parse the user's input into an Integer before checking it, so any string entered by the user that Integer.parseInt evaluates to 0 (not just "000") will cause your program to return.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号