/*
 Author:    Carlos Icaza
*/


import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Distance{
		
	public Distance(String in){
		Pattern p = Pattern.compile("(\\d+(\\.\\d*)?)(km|mi|m|nm|ft)?");
		
		Matcher m = p.matcher(in);
		
		boolean match = m.matches();
		
		if (match){
			num = Double.valueOf( m.group(1) ); // get number and turn into a float
			String measure = m.group(3);
		
		if (measure == null) measure = "m"; //meters by defaut, to avoid compareTo null 					//pointer exception when input has no measure
		if (measure.compareTo("km") == 0) num = num * 1000.000;
		if (measure.compareTo("mi") == 0) num = num * 1609.344;
		if (measure.compareTo("nm") == 0) num = num * 1852.000;
		if (measure.compareTo("ft") == 0) num = num * 0.3048;
		}
		else{
		 num = -1.00;}
	}
	
	public Double getDouble(){
		return num;
	
	}
	
	public String getString(){
		return Double.toString(num);
	}
	private Double num;
}

