• 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

Data structures in java.

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say I've 3 currencies A, B and C.
I'm storing them in a table using 3 columns currency_1, currency_2, and conversion rate.
For example;

C_1->C_2 Rate
A->B 0.45
B->C 1.25
C->A 1.60

Like this I may have N number of currencies in the DB.
I just find out the number of combinations such as A->B->C and again C->A, in the database. (A->B->C->A)
So while doing this in java programming laguage which data-structure can be used? and how this can be resolved.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wish the columns were named a little more explicitly. Does this mean that 1 unit of A is equivalent to .45 of B? Or does it mean that 1 unit of B is equivalent to .45 of A?

If it were my problem to solve, I'd create a CurrencyConversion class whose attributes are the database fields.
 
Mahesh Bamane
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, it 1 unit of A is equal 0.45 unit of B, Well I'm interested to see your class. Because I'm not getting enough hint to nail down this problem.
 
Marshal
Posts: 28193
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
You didn't say what the keys to this database table were, and that's an important question if you're trying to mirror the table in Java.

If I were implementing that table, its keys would be currency-1 and currency-2 (which I would give better names to, so you could tell which way the conversion was being done). And therefore you would need some collection which had keys (there's a big clue right there), and those keys would be a pair of Currency objects.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mahesh Bamane wrote:Thanks for your reply, it 1 unit of A is equal 0.45 unit of B, Well I'm interested to see your class. Because I'm not getting enough hint to nail down this problem.


Rather than deprive you of the enjoyment of implementing the class, I'll just describe it. It would have two final fields of type Currency: sourceCurrency and destinationCurrency. (And I would rename those database columns source_currency and destination_currency. The name "currency_2" doesn't tell anyone what its relationship is to the entity.) It would have a final field of type double: exchangeRate. It might also have a method named convert that takes an amount in sourceCurrency and returns the equivalent amount in destinationCurrency.
 
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

Mahesh Bamane wrote:Say I've 3 currencies A, B and C.
I'm storing them in a table using 3 columns currency_1, currency_2, and conversion rate.
For example;

C_1->C_2 Rate
A->B 0.45
B->C 1.25
C->A 1.60

Like this I may have N number of currencies in the DB.


As Dennis said, think about creating a class of your own for that in Java (Currency?).

I just find out the number of combinations such as A->B->C and again C->A, in the database. (A->B->C->A)
So while doing this in java programming laguage which data-structure can be used? and how this can be resolved.


Well, to answer your 2nd question first: either a Map ( java.util.Map ) or a Set ( java.util.Set ), but you need a class first.

And, while there's no particular problem with your idea of holding all combinations, you do have a couple of issues:
1. How do you eliminate duplicates? Personally, I'd think that $→Yen is the same as 1/(Yen→$), wouldn't you agree?
2. The sheer volume of combinations. Say you have 20 currencies: how many combinations can you have?

There is, however, a simple way of holding your currencies that doesn't involve combinations at all. Can you think what it is? (Hint: it involves a 2-step conversion).

HIH

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic