// First main just deals with 2 individual thermostats
//      (in a class we don't see)
public static void main(String[] args)
{
    // Create room thermostats for 2 rooms.

    Thermostat room1 = new Thermostat();
    Thermostat room2 = new Thermostat();

    // Set up one thermostat to report its temps in Celsius
    // and one in Fahrenheit.  Then print the current temp
    // for both rooms.

    room1.useCelsius();
    room2.useFahrenheit();

    // Print basic stats for each room: current temperature,
    // minimum temperature, and maximum temperature,
    // formatted cleanly.

    double temp = room1.getCurrentTemp();
    double minTemp = room1.getMinTemp();
    double maxTemp = room1.getMaxTemp();
    System.out.println("Room 1 -- Current temp: " + temp);
    System.out.print("    Min temp: " + minTemp + "; Max temp: " + maxTemp);

    // Print the 2nd one in a different way.
    System.out.println("Room 2 -- Current temp: " + room2.getCurrentTemp());
    System.out.print("    Min temp: " + room2.getMinTemp() +
            "; Max temp: " + room2.getMaxTemp());

}


// Second main deals with a building monitor of many thermostats
//      (in a different class we don't see)
public static void main(String[] args)
{
    // Create a building monitor. Assume that the room
    // thermostats it interacts with will be created by the
    // monitor constructor.

    Thermostat room1 = new Thermostat();
    Thermostat room2 = new Thermostat();

    // Ask the building monitor to report temperatures in
    // Fahrenheit.

    room1.useCelsius();
    room2.useFahrenheit();

    // Print the average temperature of the building (the average
    // room temperature) and then the room stats for all the
    // rooms in the building.

    double temp = room1.getCurrentTemp();
    double minTemp = room1.getMinTemp();
    double maxTemp = room1.getMaxTemp();
    System.out.println("Room 1 -- Current temp: " + temp);
    System.out.print("    Min temp: " + minTemp + "; Max temp: " + maxTemp);

    // Find the currently coldest room and report its current
    // temperature.
    System.out.println("Room 2 -- Current temp: " + room2.getCurrentTemp());
    System.out.print("    Min temp: " + room2.getMinTemp() +
            "; Max temp: " + room2.getMaxTemp());

}


public class Thermostat
{
    private boolean useCelsius;
    private Sensor sensor;
    private double minTemp;
    private double maxTemp;

    public Thermostat()
    {
        this.useCelsius = true;
        this.sensor = new Sensor();

        // Initialize min and max from current temp.
        this.minTemp = this.sensor.getTemp();
        this.maxTemp = this.sensor.getTemp();
    }

    public void useCelsius()
    {
        // Having instance variable & method with same names is
        // legal, but probably confusing.  Sorry about that!
        this.useCelsius = true;
    }

    public void useFahrenheit()
    {
        this.useCelsius = false;
    }

    public double getCurrentTemp()
    {
        double celTemp = sensor.getTemp();

        // Check Celsius vs Fahrenheith using instance variable
        //    (could use method instead)
        if ( this.useCelsius == true )
            return celTemp;
        else
        {
            // Convert to Fahrenheit and return that.
            return ((celTemp * 9) / 5) + 32;
        }
    }

    // Presumably this method is called regularly, based on a timer perhaps.
    public void updateMinMax()
    {
        double celTemp = sensor.getTemp();
        if ( celTemp < this.minTemp )
            this.minTemp = celTemp;
        else if ( celTemp > this.maxTemp )
            this.maxTemp = celTemp;
    }

    public double getMinTemp()
    {
        return this.minTemp;
    }

    public double getMaxTemp()
    {
        return this.maxTemp;
    }
}


public class BuildingMonitor
{
    private ArrayList<Thermostat> allTherms;

    public BuildingMonitor()
    {
        // First create an empty list of thermostats.
        allTherms = new ArrayList<Thermostat>();

        // Presumably there is some way that the Thermostats get
        // connected to the Building Monitor, but that wasn't specified.
    }

    public void useFahrenheit()
    {
        for ( Thermostat therm : allTherms )
            therm.useFahrenheit();
    }

    // The assumption here is that the room number is the index into
    // the ArrayList.  Otherwise, we would have had to store the room
    // number with each thermostat.
    public void printStats(int roomNum)
    {
        Thermostat therm = allTherms.get(roomNum);

        System.out.println("Room " + roomNum + " -- " +
                "Current temp: " + therm.getCurrentTemp());
        System.out.println("    Min temp: " + therm.getMinTemp() +
                "; Max temp: " + therm.getMaxTemp());
    }

    public void printAllRoomStats()
    {
        for ( int i = 0; i < allTherms.size(); i++ )
            this.printStats(i);
    }

    public double averageRoomTemp()
    {
        // Sum up all the temperatures, then divide by number of values.
        double sum = 0;
        for ( int i = 0; i < allTherms.size(); i++ )
            sum += allTherms.get(i).getCurrentTemp();
        // OR
        //   for ( Thermostat therm : allTherms )
        //      sum += therm.getCurrentTemp();

        return sum / allTherms.size();
    }

    public int getCurrentColdestRoom()
    {
        // Didn't say what to do if there are no thermostats, so let's
        // return -1, which is clearly an invalid room number (index).
        if ( allTherms.isEmpty() )
            return -1;

        double minTempSoFar = allTherms.get(0).getCurrentTemp();
        int minIndexSoFar = 0;
        // use old-style for loop because we want the index
        for ( int i = 1; i < allTherms.size(); i++ )
        {
            Thermostat therm = allTherms.get(i);
            if ( therm.getCurrentTemp() < minTempSoFar )
            {
                minTempSoFar = therm.getCurrentTemp();
                minIndexSoFar = i;
            }
        }

        // By the end, the minimum "so far" is the actual minimum.
        return minIndexSoFar;
    }

}