• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Build currency converter with object oriented programming using java

 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..


 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic