Object-Oriented Programming/Validation/C Sharp

main.cs edit

/// <summary>
/// This program demonstrates use of the Temperature class.
/// </summary>

using System;

/// <summary>
/// Provides the main program code.
/// </summary>
class Program {
    /// <summary>
    /// Runs the main program.
    /// </summary>
    public static void Main (string[] args) {
        var temperature = new Temperature(celsius: 37);
        Console.WriteLine(temperature.Fahrenheit);
        
        temperature = new Temperature(fahrenheit: 98.6);
        Console.WriteLine(temperature.Celsius);
        
        temperature = new Temperature();
        temperature.Celsius = 37;
        Console.WriteLine(temperature.Fahrenheit);
        
        temperature = new Temperature();
        temperature.Fahrenheit = 98.6;
        Console.WriteLine(temperature.Celsius);
        
        temperature = new Temperature();
        Console.WriteLine(temperature.ToCelsius(98.6));
        Console.WriteLine(temperature.ToFahrenheit(37)); 

        try 
        {
            temperature = new Temperature(celsius: 1, fahrenheit: 2);
        }
        catch(Exception exception)
        {
            Console.WriteLine(exception.Message);
        }

        try 
        {
            temperature = new Temperature(celsius: -500);
        }
        catch(Exception exception)
        {
            Console.WriteLine(exception.Message);
        }

        try 
        {
            temperature = new Temperature(fahrenheit: -600);
        }
        catch(Exception exception)
        {
            Console.WriteLine(exception.Message);
        }

        try 
        {
            Console.WriteLine(temperature.ToCelsius(-700));
        }
        catch(Exception exception)
        {
            Console.WriteLine(exception.Message);
        }

        try 
        {
            Console.WriteLine(temperature.ToFahrenheit(-800));
        }
        catch(Exception exception)
        {
            Console.WriteLine(exception.Message);
        }
    }
}

temperature.cs edit

/// <summary>
/// Temperature converter. Provides temperature conversion functions. Supports Fahrenheit and Celsius temperatures.
/// </summary>
/// <remarks>
/// Examples:
///    var temperature = new Temperature(celsius: 37);
///    Console.WriteLine(temperature.Fahrenheit);
///    
///    temperature = new Temperature(fahrenheit: 98.6);
///    Console.WriteLine(temperature.Celsius);
///    
///    temperature = new Temperature();
///    temperature.Celsius = 37;
///    Console.WriteLine(temperature.Fahrenheit);
///    
///    temperature = new Temperature();
///    temperature.Fahrenheit = 98.6;
///    Console.WriteLine(temperature.Celsius);
///    
///    temperature = new Temperature();
///    Console.WriteLine(temperature.ToCelsius(98.6));
///    Console.WriteLine(temperature.ToFahrenheit(37)); 
///
/// References:
///   https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments
///   https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types
///   https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
/// </remarks>

public class Temperature
{
    private double _celsius;
    private double _fahrenheit;

    /// <summary>
    /// Creates a Temperature object.
    /// Throws ArgumentException if both celsius and fahrenheit are initialized.
    /// </summary>
    public Temperature(double? celsius=null, double? fahrenheit=null)
    {
        if (celsius != null && fahrenheit != null)
        {
            throw new System.ArgumentException("Only initialize celsius or fahrenheit, not both.");
        }

        if (celsius != null)
        {
            Celsius = (double)celsius;
        }

        if (fahrenheit != null)
        {
            Fahrenheit = (double)fahrenheit;
        }
    }

    /// <summary>
    /// Returns absolute zero Celsius.
    /// </summary>
    public double AbsoluteZeroCelsius
    {
        get
        {
            return -273.15;
        }
    }
    
    /// <summary>
    /// Returns absolute zero Fahrenheit.
    /// </summary>
    public double AbsoluteZeroFahrenheit
    {
        get
        {
            return -459.67;
        }
    }

    /// <summary>
    /// Gets and sets Celsius temperature.
    /// </summary>
    public double Celsius
    {
        get
        {
            return _celsius;
        }

        set
        {
            _celsius = ValidateCelsius(value);
            _fahrenheit = ToFahrenheit(value);
        }
    }

    /// <summary>
    /// Gets and sets Fahrenheit temperature.
    /// </summary>
    public double Fahrenheit
    {
        get
        {
            return _fahrenheit;
        }

        set
        {
            _fahrenheit = ValidateFahrenheit(value);
            _celsius = ToCelsius(value);
        }
    }

    /// <summary>
    /// Converts Fahrenheit temperature to Celsius.
    /// </summary>
    public double ToCelsius(double fahrenheit)
    {
        fahrenheit = ValidateFahrenheit(fahrenheit);
        return (fahrenheit - 32) * 5 / 9;
    }

    /// <summary>
    /// Converts Celsius temperature to Fahrenheit.
    /// </summary>
    public double ToFahrenheit(double celsius)
    {
        celsius = ValidateCelsius(celsius);
        return celsius * 9 / 5 + 32;
    }

    /// <summary>
    /// Validates Celsius temperature.
    /// Throws error if Celsius temperature is below absolute zero.
    /// </summary>
    private double ValidateCelsius(double celsius)
    {
        if (celsius < AbsoluteZeroCelsius) {
            throw new System.ArgumentException("celsius must not be below absolute zero (" +
                AbsoluteZeroCelsius + "). Received " + celsius);
        }

        return celsius;
    }

    /// <summary>
    /// Validates Fahrenheit temperature.
    /// Throws error if Fahrenheit temperature is below absolute zero.
    /// </summary>
    private double ValidateFahrenheit(double fahrenheit)
    {
        if (fahrenheit < AbsoluteZeroFahrenheit) {
            throw new System.ArgumentException("fahrenheit must not be below absolute zero (" +
                AbsoluteZeroFahrenheit + "). Received " + fahrenheit);
        }

        return fahrenheit;
    }
}

Try It edit

Copy and paste the code above into one of the following free online development environments or use your own C Sharp compiler / interpreter / IDE.

See Also edit