import java.io.*;

//TARGET
abstract class Therm {
    //Fields
    protected double temp;
    protected String unit; 

    public double getTemp() {
        return temp;
    }

    public String getUnit() {
        return unit;
    }

    abstract void display();

}

//ADAPTEE
class TempCalc {

    //Methods
    public double getF(double temp, String unit) {
        if (unit.equals("F")) {
            return temp;
        } else if (unit.equals("C")) {
            return temp * (9/5.0) + 32;
        } else if (unit.equals("K")) {
            return (temp - 273.15) * 1.8 + 32;
        } else {
            return -460; //Abs 0 in F is -459
        }

    }

    public double getC(double temp, String unit) {
        if (unit.equals("C")) {
            return temp;
        } else if (unit.equals("F")) {
            return (temp - 32.0) * (5/9.0);
        } else if (unit.equals("K")) {
            return temp - 273.15;
        } else {
            return -274; //abs 0 in C is -273
        }

    }

    public double getK(double temp, String unit) {
        if (unit.equals("K"))  {
            if (temp < 0) 
                return -1;
            else
                return temp;

        } else if (unit.equals("C")) {
            return temp + 273.15;
        } else if (unit.equals("F")) {
            return (temp + 459.67) * (5/9.0);
        } else {
            return -1; //abs 0 is 0K
        }
    }
    
}

//ADAPTER
class SmartTherm extends Therm {
    //Fields
    private TempCalc calc;
    private double f;
    private double c;
    private double k;

    //Constructor
    public SmartTherm(double temp, String unit) {
        this.temp = temp;
        this.unit = unit.toUpperCase();
       //Adaptee
        calc = new TempCalc();
        //Adaptee request methods
        f = calc.getF(temp, unit);
        c = calc.getC(temp, unit);
        k = calc.getK(temp, unit);
    }
    
    public void display() {
        if (k < 0) {
            System.out.println("Error: Unit is wrong or a number below absolute 0 was entered");        
        } else {
            System.out.printf("It is %.2f F. %n", f);
            System.out.printf("It is %.2f C. %n", c);
            System.out.printf("It is %.2f K. %n", k);

        }   
        System.out.println();
    }

}


//CLIENT
public class TempApp {
    
    public static void main(String[] args) {
    
        SmartTherm reading1 = new SmartTherm(40.24, "C");
        System.out.println("Reading1: input is 40.24 C");
        reading1.display();

        SmartTherm reading2 = new SmartTherm(312.152, "K");
        System.out.println("Reading2: input is 312.152 K");
        reading2.display();

        SmartTherm reading3 = new SmartTherm(-5.9, "F");
        System.out.println("Reading3: input is -5.9 F");
        reading3.display();


        SmartTherm reading4 = new SmartTherm(4, "X");
        System.out.println("Reading4: input is 4 X");
        reading4.display();

        SmartTherm reading5 = new SmartTherm(-20, "K");
        System.out.println("Reading5: input is -20 K");
        reading5.display();

    }

}

