aspose file tools*
The moose likes Java in General and the fly likes Build currency converter with object oriented programming using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Build currency converter with object oriented programming using java" Watch "Build currency converter with object oriented programming using java" New topic
Author

Build currency converter with object oriented programming using java

Sheila Robusta
Greenhorn

Joined: Feb 16, 2013
Posts: 1
Design a currency Converter class whose object will handle conversion of three currencies (Dollar, Ringgit, Pound). A single object of the new currency Converter class you design here will handle all these three currencies. Instead of having specific conversion methods such as toDollar, toPound and so forth, the new currency Converter class supports one generic conversion method call exchange. The method has three arguments: fromCurrency, toCurrency and amount. The first two arguments are String and give the names of currencies. The third argument is the amount that need to be converted. For example, to convert RM250 to dollar, we write:

dollar = converter.exchange(‘ringgit”, “dollar”, 250.0);


To set the exchange rate for a currency, we use the setRate method. This method takes three arguments. The first two arguments are the currency names, and the third argument is the rate. For example, if the exchange rate for dollar is 3.4 Dollar to RM1, then we write:

converter.setRate(“dollar”, “ringgit”, 3.4);

In addition, the class also have the following method:

updateRate (“fromCurrency”, “toCurrency”, newRate) - The first two arguments are the currency names, and the third argument is the new exchange rate.


Finally, test your program by creating its object in the main() method.


So far this is my code..but how to prompt user to input the name of currency, name of currency to convert, amount and the rate..


Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

Before going any deeper into your code.
Is there a good reason why you use double for monetary values?
You should almost never use this. (I just wrote almost because there might be very special cases where you might get away with it.)
Either use BigDecimal or use Long or Integer if you know that the value is not that big.


Please correct my English.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Welcome to the Ranch, Sheila.

Sheila Robusta wrote:So far this is my code..but how to prompt user to input the name of currency, name of currency to convert, amount and the rate..


If you had to do that, you would use a Scanner object in your test code, which would most likely be part of the main() method of the class which uses the one you posted there. But do you have to do that? I didn't see anything like that in what you posted, all I saw was

Finally, test your program by creating its object in the main() method.


(By the way I put "code" tags around the code you posted. Makes it a lot easier to read, don't you think? You can do that yourself, just look for the "code" button when you're posting.)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Manuel Petermann wrote:Before going any deeper into your code.
Is there a good reason why you use double for monetary values?
You should almost never use this. (I just wrote almost because there might be very special cases where you might get away with it.)
Either use BigDecimal or use Long or Integer if you know that the value is not that big.

Hmmm. Not sure I agree with you completely (although I understand the reason for your warning).

The fact is that a currency rate is not a currency value, and it may be perfectly reasonable to use a double for the rate.

@Sheila: What Manuel was saying is quite correct when you're converting currency amounts, because your answer must be exact (or rounded correctly); but the internal rate is probably OK as a double. You should be aware though that doubles are only good for ≈15 digits of precision, so if you're dealing with very large amounts you could run into rounding errors. For more information, read this.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 175

If you use double for the rate you got the same problem as 0.1 is not exact in double precission and that would be a reasonable currency exchange rate.
Even if you use BigDecimal for the amount you would get rounding errors if your rates are represented in double.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Manuel Petermann wrote:If you use double for the rate you got the same problem as 0.1 is not exact in double precission and that would be a reasonable currency exchange rate.

Only if it's precision is not sufficient. 0.1 will be accurate to 15SD, so if that's good enough, your answer will be correct. It's also worth noting that there is a MathContext that is equivalent to double precision that can be used with BigDecimal.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Build currency converter with object oriented programming using java