Design question -- dynamic unit conversion

R

Ryan Stewart

I'm trying to come up with a good method of unit conversion for use (currently)
in a cookbook application. Of course it would be preferable to make it as
portable as possible. For now, I'm looking to be able to convert between english
units of volume (tablespoons, cups, etc). A typical scenario would be something
like:
1) user inputs recipe containing 4 tablespoons of butter
2) user requests a tripled recipe, which would be 12 tablespoons of butter

That 12 tablespoons might be better represented as 3/4 cup.

I already have a Fraction class to handle the math. My question to you is how
would you implement the actual conversion mechanism? One way that springs to
mind would be to have a UnitOfVolume interface which is implemented by classes
such as Cup, TableSpoon, etc. The interface would specify methods like:
public TeaSpoon toTeaSpoons();
public TableSpoon toTableSpoons();
public Cup toCups();
public Quart toQuarts();

That seems klunky though, since every class would have every method, including
the one to convert to itself. Another alternative might be:
public UnitOfMeasure convertTo(String unit);

or
public UnitOfMeasure convertTo(Class unitClass);

But in either case, I think you'd have to store the relationships between every
unit somewhere.

How would you do it?
 
P

Patricia Shanahan

Ryan said:
I'm trying to come up with a good method of unit conversion for use (currently)
in a cookbook application. Of course it would be preferable to make it as
portable as possible. For now, I'm looking to be able to convert between english
units of volume (tablespoons, cups, etc). A typical scenario would be something
like:
1) user inputs recipe containing 4 tablespoons of butter
2) user requests a tripled recipe, which would be 12 tablespoons of butter

These sound more typical of American, rather than English,
units. Be careful. For example, pints are different.
That 12 tablespoons might be better represented as 3/4 cup.

I already have a Fraction class to handle the math. My question to you is how
would you implement the actual conversion mechanism? One way that springs to
mind would be to have a UnitOfVolume interface which is implemented by classes
such as Cup, TableSpoon, etc. The interface would specify methods like:
public TeaSpoon toTeaSpoons();
public TableSpoon toTableSpoons();
public Cup toCups();
public Quart toQuarts();

That seems klunky though, since every class would have every method, including
the one to convert to itself. Another alternative might be:
public UnitOfMeasure convertTo(String unit);

or
public UnitOfMeasure convertTo(Class unitClass);

But in either case, I think you'd have to store the relationships between every
unit somewhere.

How would you do it?

Pick one base unit. If you extended this to handle weights
as well as volumes you would have separate base units for
each. You can do all combinations of conversions as long as
you know the size of each unit in terms of a common base.
That way you enter and store N facts instead of N*N facts.

I would have a class Unit, and an instance for each
individual unit. Class Unit would contain the conversion code.

Patricia
 
M

Mark Thornton

Ryan said:
I'm trying to come up with a good method of unit conversion for use (currently)
in a cookbook application. Of course it would be preferable to make it as
portable as possible. For now, I'm looking to be able to convert between english
units of volume (tablespoons, cups, etc). A typical scenario would be something
like:
1) user inputs recipe containing 4 tablespoons of butter
2) user requests a tripled recipe, which would be 12 tablespoons of butter

That 12 tablespoons might be better represented as 3/4 cup.

I already have a Fraction class to handle the math. My question to you is how
would you implement the actual conversion mechanism? One way that springs to
mind would be to have a UnitOfVolume interface which is implemented by classes
such as Cup, TableSpoon, etc. The interface would specify methods like:
public TeaSpoon toTeaSpoons();
public TableSpoon toTableSpoons();
public Cup toCups();
public Quart toQuarts();

That seems klunky though, since every class would have every method, including
the one to convert to itself. Another alternative might be:
public UnitOfMeasure convertTo(String unit);

or
public UnitOfMeasure convertTo(Class unitClass);

But in either case, I think you'd have to store the relationships between every
unit somewhere.

How would you do it?

Search for prototype implementations of JSR-108 and see how they are
approaching the issues of units.

http://sourceforge.net/projects/jsr-108/

Mark Thornton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top