sameetandpotatoes
sameetandpotatoes

Reputation: 1220

How to create an EFFICIENT currency/unit converter?

I just have a general question on how to create efficient programs. For example, I made a unit converter program for Blackberry (The Ultimate Unit Converter if you want to check it out :) )that converts between 5-7 different units. To code this, I did the brute-force way by simply creating methods for all possible units. So, for example, I created methods for all of the following: centimeters to inches, centimeters to meters, inches to centimeters, inches to meters, meters to centimeters, meters to inches, ETC.

My question is: Is there a more efficient way of coding this? Because it becomes a pain when I have to add in a unit, because then I have to create another set of methods.

Thank you! (Sorry if I sound stupid.)

Upvotes: 3

Views: 1752

Answers (5)

matsev
matsev

Reputation: 33797

Don't reinvent the wheel. The JSR 275 has been targeted to solve this problem, and the JScience project implements that JSR:

Amount<Length> lengthInCm = Amount.valueOf(5, SI.CENTIMETER);
Amount<Length> lengthInInches = lengthInCm.to(NonSI.INCH);    // conversion

long inches = lenghtInCm.longValue(NonSI.INCH);  // unboxing and conversion
long meters = lenghtInCm.longValue(SI.METER);    // unboxing and conversion

Upvotes: 0

Joachim Isaksson
Joachim Isaksson

Reputation: 181097

The easiest way is to just decide a "base unit", let's for length say meters.

If you make methods for each unit to convert to and from meters instead of all-to-all, you can convert x=>y by converting x=>meters=>y.

Of course that would also allow you to just store a conversion factor for each unit to meters, and not need to make more than two functions; to_meters and from_meters.

Upvotes: 0

StrangeQuirk
StrangeQuirk

Reputation: 96

Simple: just choose one "standard" unit (lets say SI units) and convert to it and from it. For example, make a "inches to meters", "meters to inches", "centimeters to meters", "meters to centimeters", etc. To convert from inches to centimeters, simply call "inches to meters" and then "meters to centimeters".

To add a new unit, you only need to add 2 methods: "NewUnit to meters" and "meters to NewUnit".

Of course, its up to you how to organize your methods. You could have one method that takes units as arguments, for instance.

Upvotes: 4

Irfy
Irfy

Reputation: 9607

Yes there is.

  • For every type of measurement (e.g. distance, time, energy, money, etc.), define a base unit. If SI applies, I suggest that. Otherwise, choose whatever you want.
  • For all supported units, make two methods (written in OO style, can be anything though)
    1. base to_base_unit()
    2. from_base_unit(base)
  • For every conversion of units, do a two-step conversion. E.g. to convert a to b: b.from_base_unit(a.to_base_unit())

Upvotes: 1

Voo
Voo

Reputation: 30245

Use SI units (well you can use any one, but since most units are defined wrt SI units that's the most sensible imo) as the basis and conversion factors to those for all others.

That way you only have to store one value per unit and can convert from unit A to B in at most two steps. That's O(N) instead of O(N^2) the naive approach has. Also it allows to do this easily using enums and just one function. Adding more units just means adding the unit to the enum and the (one!) correct conversion factor

Upvotes: 3

Related Questions