Reputation: 1289
I am trying to do a simple convert units application. I have 2 spinners, which one of them containing an array that says the different types of units.
And when I press the Convert button based on what is selected in both spinners should do something.
Example: Spinner 1 - Selected POUNDS US / Spinner2 - Selected KiloGrams Them when I press the convert button it converts it and display in another textbox.
My problem is I will have to check every single time what is selected in each spinner... and that would be a lot of if, else if statements and I was hoping if I could do it using a switch with a Integer Array. Here is my code to understand it a little more.
private void mass() {
num = (Double.parseDouble(input.getText().toString()));
int position1 = sInput.getSelectedItemPosition();
int position2 = sResult.getSelectedItemPosition();
int[] position = new int[] {position1, position2};
if(position[0] == 1 && position[1] == 6) {
setValue((Double.toString(round(num / 2.2))));
} else if(position1 == 6 && position2 == 1) {
setValue((Double.toString(round(num * 2.2))));
}
}
The array position holds the values selected in the spinner. Is there a way of doing a switch statement using something like position[0,0], position[0,1], etc., or an even better approach than that?
Upvotes: 0
Views: 532
Reputation: 5095
As an example, for converting mass to and from different units, I would go through an intermediate unit.
eg. lets say I have the following
final int TYPE_KG = 0;
final int TYPE_POUNDS = 1;
// these two values come from your spinners
int sourceUnitType; // eg 0=kg, 1=pounds, ...
int destUnitType; // same types as the source unit type
Then you can convert the source number into your intermediate units, like kilograms, using a function
double convertMassToKg(int sourceUnitType, double source)
{
switch (sourceUnitType)
{
case TYPE_KG:
return source;
case TYPE_POUND:
return source * 0.453592;
}
}
then convert the intermediate value into the destination units
double convertKgToMass(int destinationUnitType, double kilos)
{
switch (destinationUnitType)
{
case TYPE_KG:
return kilos;
case TYPE_POUND:
return kilos / 0.453592;
}
}
That's just my take on the problem anyway. What values can your spinners take? Because it doesn't make sense to let somebody try to convert kilometers to kilograms for example, so depending upon how you have broken up your program (i.e. is this form just for converting mass?) then perhaps you can approach it a little differently.
You could let the user select the source type in the first spinner, and then update the second spinner with only those types which make sense, eg if the first spinner is selected to "kilometers" then the second could have the options "miles", "centimeters", "yards", etc.
Upvotes: 1
Reputation: 54702
Just an idea, There might be better idea than that
initially create a 2d array a which dimension is
a[ number of first spinner's item][number of second spinner's item]
initialized the array with the conversion factor. For example
if kg to pouds conversion factor is 2.2. In your spinner 1 kg's index is 1 and in spinner 2 kg's index is 3 then
a[1][3] = 2.2
now when the convert button is pressed do the following
result = value*a[spiner1 index][spinner 2 index]
Visible Advantages:
Visible Disadvantages
Upvotes: 1