www.webdeveloper.com
Results 1 to 6 of 6

Thread: currency convertor

Hybrid View

  1. #1
    Join Date
    Nov 2004
    Posts
    6

    currency convertor

    please help me I am a newbie...

    how would i go about creating an applet or application that converts from one currency to another. for example (us dollars to euros and vice versa)

  2. #2
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    A good idea to put into mind is the use of an xml file for the currency ratios.

    But anyway...for a simple one.

    set up constants for the currency ratios:
    Code:
    private final double US_TO_CAN = 0.8;
    then you have to implement inputs, either with a keyboard class, io class, swing or awt.

    Then it's just simple algebra from there on in.

    ------

    EDIT:

    Here ya go.

    Code:
    import cs1.Keyboard;
    import java.text.NumberFormat;
    
    public class Currency
    {
    	public static void main(String[] args)
    	{
    		NumberFormat fmt = NumberFormat.getCurrencyInstance();
    		
    		final double US_TO_CAN = 1.3333333;
    		double inputUSD;
    		double output;
    		
    		//read from user input
    		System.out.print("Enter $ USD: ");
    		inputUSD = Keyboard.readDouble();
    		
    		//perform calculation conversion
    		output = inputUSD*US_TO_CAN;
    		
    		//output
    		System.out.println("Canadian Dollar Amount: " 
    		+ fmt.format(output));
    	}
    }
    Last edited by BuezaWebDev; 11-19-2004 at 01:44 AM.

  3. #3
    Join Date
    Nov 2004
    Posts
    6

    thanks

    well i guess that gets me going in the right direction, but i don't understand what you mean by cs1.keyboard and textpad doesn't recognize it either.

  4. #4
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    cs1.Keyboard; is a jar file that allows you to take in user inputs.

    You can download it here.

    http://bravura.bueza.com/cs1.jar

  5. #5
    Join Date
    Nov 2004
    Posts
    6
    ok another question

    how can i make it so that the application asks the user whether he would like to convert from us to can, or us to euros or something like that?

    some sort of menu? i guess that is what you could call it.

  6. #6
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665
    You could make a method and a public static variable like, "mode"


    Code:
    private static double conversionRate;
    
    
    private static int setMode(int numberCurrency)
    {
    	if (numberCurrency == 1)
    	{
    		conversionRate = 1.33;
    		return conversionRate;
    	}
    
    	if (numberCurrency == 2)
    	{
    		conversionRate = 0.88;
    		return conversionRate;
    	}
    }
    
    public static void main(final String[] args)
    {
    	int inputMode;
    	double inputDollars;
    
    	System.out.println("1 = US to Can");
    	System.out.println("2 = Can to US");
    
    	inputMode = Keyboard.readInt();
    
    	setMode(inputMode);
    
    	System.out.println("How much in $: ");
    	inputDollars = Keyboard.readDouble();
    
    	double output = inputDOllars*conversionRate;
    	System.out.println(output);	
    }
    Enclose it with public class NameOfClass blah blah :P

    Hopefully this leads you in the right direction. There's probably a more efficient way of doing this, and maybe someone else will submit a better version.

    Sorry for the late reply.

    Kind regards,
    Jaime Bueza

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles