Last active
June 7, 2016 19:42
-
-
Save DLMousey/1a419537d009771bda0514751caf5b42 to your computer and use it in GitHub Desktop.
Converter.java class file from the Stackskills complete Android Marshmallow series since Mohitd didn't provide it!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CHANGE THIS TO YOUR PACKAGE NAME! | |
package com.tmlov.unitconverter; | |
public class Converter { | |
public enum Unit { | |
INCH, | |
CENTIMETER, | |
FOOT, | |
YARD, | |
METER, | |
MILE, | |
KILOMETER; | |
public static Unit fromString(String text) { | |
if (text != null) { | |
for (Unit unit : Unit.values()) { | |
if (text.equalsIgnoreCase(unit.toString())) { | |
return unit; | |
} | |
} | |
} | |
throw new IllegalArgumentException("Cannot find a value for " + text); | |
} | |
} | |
private final double multiplier; | |
public Converter(Unit from, Unit to) { | |
double constant = 1; | |
switch (from) { | |
case INCH: | |
if (to == Unit.CENTIMETER) { | |
constant = 2.54; | |
} else if (to == Unit.FOOT) { | |
constant = 0.0833333; | |
} else if (to == Unit.YARD) { | |
constant = 0.0277778; | |
} else if (to == Unit.METER) { | |
constant = 0.254; | |
} else if (to == Unit.MILE) { | |
constant = 1.5783e-5; | |
} else if (to == Unit.KILOMETER) { | |
constant = 2.54e-5; | |
} | |
break; | |
case CENTIMETER: | |
if (to == Unit.INCH) { | |
constant = 0.393701; | |
} else if (to == Unit.FOOT) { | |
constant = 0.0328084; | |
} else if (to == Unit.YARD) { | |
constant = 0.0109361; | |
} else if (to == Unit.METER) { | |
constant = 0.01; | |
} else if (to == Unit.MILE) { | |
constant = 6.2137e-6; | |
} else if (to == Unit.KILOMETER) { | |
constant = 1e-5; | |
} | |
break; | |
case FOOT: | |
if (to == Unit.INCH) { | |
constant = 12; | |
} else if (to == Unit.CENTIMETER) { | |
constant = 30.48; | |
} else if (to == Unit.YARD) { | |
constant = 0.333333; | |
} else if (to == Unit.METER) { | |
constant = 0.3048; | |
} else if (to == Unit.MILE) { | |
constant = 0.000189394; | |
} else if (to == Unit.KILOMETER) { | |
constant = 0.0003048; | |
} | |
break; | |
case YARD: | |
if (to == Unit.INCH) { | |
constant = 36; | |
} else if (to == Unit.CENTIMETER) { | |
constant = 91.44; | |
} else if (to == Unit.FOOT) { | |
constant = 3; | |
} else if (to == Unit.METER) { | |
constant = 3; | |
} else if (to == Unit.MILE) { | |
constant = 0.000568182; | |
} else if (to == Unit.KILOMETER) { | |
constant = 0.0009144; | |
} | |
break; | |
case METER: | |
if (to == Unit.INCH) { | |
constant = 39.3701; | |
} else if (to == Unit.CENTIMETER) { | |
constant = 100; | |
} else if (to == Unit.FOOT) { | |
constant = 3.28084; | |
} else if (to == Unit.YARD) { | |
constant = 1.09361; | |
} else if (to == Unit.MILE) { | |
constant = 0.000621371; | |
} else if (to == Unit.KILOMETER) { | |
constant = 0.001; | |
} | |
break; | |
case MILE: | |
if (to == Unit.INCH) { | |
constant = 63360; | |
} else if (to == Unit.CENTIMETER) { | |
constant = 160934; | |
} else if (to == Unit.FOOT) { | |
constant = 5280; | |
} else if (to == Unit.YARD) { | |
constant = 1760; | |
} else if (to == Unit.METER) { | |
constant = 1609.34; | |
} else if (to == Unit.KILOMETER) { | |
constant = 1.60934; | |
} | |
break; | |
case KILOMETER: | |
if (to == Unit.INCH) { | |
constant = 39370.1; | |
} else if (to == Unit.CENTIMETER) { | |
constant = 100000; | |
} else if (to == Unit.FOOT) { | |
constant = 3280.84; | |
} else if (to == Unit.YARD) { | |
constant = 1093.61; | |
} else if (to == Unit.METER) { | |
constant = 1000; | |
} else if (to == Unit.MILE) { | |
constant = 0.612371; | |
} | |
break; | |
} | |
multiplier = constant; | |
} | |
public double convert(double input) { return input * multiplier; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment