4
\$\begingroup\$

This is my program for a sauna. It is a Swedish sauna meant to only understand Celsius, but the user can only understand Fahrenheit, so when the user inputs Fahrenheit, it is converted to Celcius. Afterwards, the Celsius is rounded to an even value when presented to the user, to get rid of all the decimals from the conversion.

class Program
{
    static void tempCheck(double temperature)
    {
        if (temperature < 0)
        {
            Console.WriteLine("The sauna has now become a freezer, \nCelcius: {0}", Math.Round(temperature));
            Console.WriteLine();
        }
        else if (temperature < 73)
        {
            Console.WriteLine("This temperature is too low for sauna use, \nCelcius: {0}", Math.Round(temperature));
            Console.WriteLine();
        }
        else if (temperature > 100)
        {
            Console.WriteLine("You will be boiled alive if you use the sauna, \nCelcius: {0}", Math.Round(temperature));
            Console.WriteLine();
        }
        else if (temperature > 77)
        {
            Console.WriteLine("This heat will scald you, please lower the temperarture, \nCelcius: {0}", Math.Round(temperature));
            Console.WriteLine();
        }
        else if (temperature == 75)
        {
            Console.WriteLine("Perfect heat for sauna!");
            Console.WriteLine();
        }
        else  
            Console.WriteLine("This temperature is adequate for sauna use! \nCelcius: {0}", Math.Round(temperature));

    }

    public static double FahrToCels(int fahr)
    {
        double fahrenheit = (5.0 / 9.0) * (fahr - 32); 
        return fahrenheit;
    }

    static void Main(string[] args)
    {

        double temperature = 0;
        Console.WriteLine("Enter a desired temperature between 73 och 77 celcius");



        do
        {
            try
            {
                int fahrenheit = int.Parse(Console.ReadLine()); 
                Console.WriteLine(temperature);
                double celcius = FahrToCels(fahrenheit); 
                Console.WriteLine(celcius);
                temperature = temperature + celcius;
                tempCheck(temperature);

            }
            catch (FormatException)
            { 
                Console.WriteLine("Please only enter whole numbers, no decimals");
            }
            catch
            {
                Console.WriteLine("Error");
            }

        } while (temperature < 73 || temperature > 77);
    }
}
}
\$\endgroup\$
4
  • \$\begingroup\$ since zero is a valid temperature input value, I don't think it is a good trigger for a randomization (though i hope no sauna is zero degrees). \$\endgroup\$
    – mcgyver5
    Jun 26, 2017 at 16:59
  • \$\begingroup\$ Okay, thanks. Will take note of that for the future, but what else should I use and how do I use it? \$\endgroup\$
    – J.Dhaik
    Jun 26, 2017 at 17:03
  • \$\begingroup\$ You ask them to enter in Celsius but still convert to Celsius. You add entries to temperature temperature = temperature + celcius; If temperature conversion fails you still try to use the variable temperature. \$\endgroup\$
    – paparazzo
    Jun 26, 2017 at 18:37
  • \$\begingroup\$ Ditto what @Paparazzi said. Make a Celcius class so the code is handling "celsius temperatures" not mere integer values. So not static void tempCheck(double temperature), but static void tempCheck(Celsius temperature) \$\endgroup\$
    – radarbob
    Jun 27, 2017 at 18:30

1 Answer 1

2
\$\begingroup\$

Review

Several issues have also been addressed in the comments below your question. A quick re-iteration:

  • You state the user only knows Fahrenheit, yet you ask them to input in Celcius: Enter a desired temperature between 73 och 77 celcius.
  • For each bad attempt, you add the new input to the previous input temperature = temperature + celcius;. This doesn't make any sense at all.
  • You should be able to read from code what the code is about. tempCheck(double temperature) doesn't tell us the unit of the input. Prefer celcius or a custom class Celcius.

tempCheck(double temperature)

This method has two very different responsibilities: (1) detect status by provided temperature (2) render status information to user. These should be two different methods to allow for reusability of the status check.

The status could be an enum:

enum TemperatureStatus
{
    Normal,   // 73°C - 77°C (but not 75°C)
    Perfect,  // 75°C
    Freezing, // < 0°C

    // and so on ..
}

Have tempCheck return the status:

static TemperatureStatus tempCheck(double celcius)
{
    if (celcius < 0) return TemperatureStatus.Freezing;

    // and so on ..

    return TemperatureStatus.Normal;
}

Rendering the status message can then go to its own method:

static string RenderStatus(TemperatureStatus status, double celcius)
{
    celcius = Math.Round(celcius);
    switch (status)
    {
        case TemperatureStatus.Freezing:
            return "The sauna has now become a freezer, \nCelcius: {celcius}";
        // and so on ..  
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.