/** Temperature.java models Fahrenheit, Celsius, and Kelvin temperatures.
 * @author Joel Adams, for Alice+Java.
 */

import java.util.Scanner;

public class Temperature {
	public static final double ABS_ZERO_FAHR = -459.67;
	public static final double ABS_ZERO_CELS = -273.15;
	
	public Temperature() {
		myMagnitude = ABS_ZERO_CELS;
		myScale = 'C';
	}
	
	public Temperature(double magnitude, char scale) {
		checkAndSet(magnitude, scale);
	}

	private void checkAndSet(double magnitude, char scale) {
		if ( isValid(magnitude, scale) ) {
			myMagnitude = magnitude;
			myScale = scale;
		} else {
			throw new IllegalArgumentException("checkAndSet(): "
					          + "bad arguments (" + magnitude 
					          + "," + scale + ")");
		}
	}
	
	public static boolean isValid(double magnitude, char scale) {
		switch (scale) {
		case 'f': case 'F':
			return magnitude >= ABS_ZERO_FAHR;
		case 'c': case 'C':
			return magnitude >= ABS_ZERO_CELS;
		default:
			return false;
		}
	}
	
	public char getScale() { return myScale; }
	public double getMagnitude() { return myMagnitude; }
	
//	public Temperature inFahrenheit() {
//		Temperature result = null;
//		switch (myScale) {
//		case 'F': case 'f':
//			result = new Temperature(myMagnitude, 'F');
//			break;
//		case 'C': case 'c':
//			result = new Temperature(celsToFahr(myMagnitude), 'F');
//			break;
//		default:
//			throw new IllegalArgumentException("inFahrenheit():" 
//				                                + " invalid scale '"
//				                                + myScale + "'");
//		}
//		return result;
//	}
	
	public Temperature inCelsius() {
		Temperature result = null;
		switch (myScale) {
		case 'F': case 'f':
			result = new Temperature(fahrToCels(myMagnitude), 'C');
			break;
		case 'C': case 'c':
			result = new Temperature(myMagnitude, 'C');
			break;
		default:
			throw new IllegalArgumentException("inCelsius():" 
                                             + " corrupted scale '"
                                             + myScale + "'");
		}
		return result;
	}
	
	
	public String toString() {
		return myMagnitude + " " + myScale;
	}
	
	public void read(Scanner scanner) {
		double magnitude = scanner.nextDouble();
		char   scale     = scanner.next().charAt(0);
		checkAndSet(magnitude, scale);
	}

	
	public static double fahrToCels(double fahrTemp) {
		return (fahrTemp - 32.0) / 1.8;
	}
	
//	public static double celsToFahr(double celsTemp) {
//		return celsTemp * 1.8 + 32.0;
//	}

	private double myMagnitude;
	private char   myScale;
}
