HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Android unit converter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
unitconverterandroid

Problem

I am developing a unit converter application for Android. The application is working correctly, but my problem is with the optimization. Is there a way to somehow optimize the code?

I have different categories - speed, weight, length and so on, and for each of them I have separate activities. Here, for example, is the code for one of them:

```
public class Speed extends Activity {

Map fromKmhMap = new HashMap<>();
Map fromKmminMap = new HashMap<>();
Map fromKmsMap = new HashMap<>();
Map fromKnotMap = new HashMap<>();
Map fromMphMap = new HashMap<>();
private RadioGroup fromRadioGroup, toRadioGroup;
private TextView from, to;
private EditText fromInput, toOutput;
private TextWatcher valueTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
if (fromInput.getText().toString().trim().length() > 0) {
checkFrom();
} else {
toOutput.setText("");
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speed);

fromKmhMap.put(R.id.toKmh, 1.0);
fromKmhMap.put(R.id.toKmmin, 0.01666666666667);
fromKmhMap.put(R.id.toKms, 0.0002777777777778 );
fromKmhMap.put(R.id.toKnot, 0.5399568034557);
fromKmhMap.put(R.id.toMph, 0.6213711922373);

fromKmminMap.put(R.id.toKmh, 60.0);
fromKmminMap.put(R.id.toKmmin, 1.0);
fromKmminMap.put(R.id.toKms, 0.01666666666667);
fromKmminMap.put(R.id.toKnot, 32.39740820734);
fromKmminMap.put(R.id.toMph, 37.28227153424);

fromKmsMap.put(R.id.toKmh, 3600.0);
fro

Solution

What you need is to use a common base unit that all your other units converts to.

For example, let's take a look at this:

fromGramMap.put(R.id.toGram, 1.0);
...
fromKilogramMap.put(R.id.toGram, 1000.0);
...
fromMicrogramMap.put(R.id.toGram, 0.000001 );
...
fromMilligramMap.put(R.id.toGram, 0.001  );


These are clearly related. 1 kilogram = 1000 gram = 1 000 000 milligram = 1 000 000 000 microgram. So instead of using multiple maps for gram, kilogram, microgram etc. Just use one base unit: Gram.

gramRatioMap.put(R.id.toGram, 1.0);
gramRatioMap.put(R.id.toKilogram, 0.001);
gramRatioMap.put(R.id.toMicrogram, 1000000.0);
gramRatioMap.put(R.id.toMilligram, 1000.0);
gramRatioMap.put(R.id.toOunce, 0.03527396194958);
gramRatioMap.put(R.id.toPound, 0.002204622621849);
gramRatioMap.put(R.id.toTroyOunce, 0.03215074656863);


Now, to convert from Kilogram to gram, you can lookup the value for R.id.toKilogram and then use 1 / (that value). In this case, \$1 / 0.001 = 1000\$

Then, once you have converted to gram, you use your gramRatioMap as normal and convert to that specific unit.

Code Snippets

fromGramMap.put(R.id.toGram, 1.0);
...
fromKilogramMap.put(R.id.toGram, 1000.0);
...
fromMicrogramMap.put(R.id.toGram, 0.000001 );
...
fromMilligramMap.put(R.id.toGram, 0.001  );
gramRatioMap.put(R.id.toGram, 1.0);
gramRatioMap.put(R.id.toKilogram, 0.001);
gramRatioMap.put(R.id.toMicrogram, 1000000.0);
gramRatioMap.put(R.id.toMilligram, 1000.0);
gramRatioMap.put(R.id.toOunce, 0.03527396194958);
gramRatioMap.put(R.id.toPound, 0.002204622621849);
gramRatioMap.put(R.id.toTroyOunce, 0.03215074656863);

Context

StackExchange Code Review Q#120714, answer score: 6

Revisions (0)

No revisions yet.