Advertisement
corp0

Untitled

Oct 11th, 2022
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Util.Independent
  4. {
  5.     /// <summary>
  6.     /// Type that represents temperature.
  7.     /// </summary>
  8.     public class Temperature: IComparable<Temperature>
  9.     {
  10.         public override int GetHashCode()
  11.         {
  12.             return kelvin.GetHashCode();
  13.         }
  14.  
  15.         private readonly float kelvin;
  16.         public float Kelvin => kelvin;
  17.         public float Celsius => kelvin - 273.15f;
  18.         public float Fahrenheit => (kelvin - 273.15f) * 9 / 5 + 32;
  19.  
  20.         #region Constructors
  21.         private Temperature(float value)
  22.         {
  23.             kelvin = value;
  24.         }
  25.  
  26.         /// <summary>
  27.         /// Initializes a new instance of the <see cref="Temperature"/> class using a float in Kelvin.
  28.         /// </summary>
  29.         /// <param name="value"></param>
  30.         /// <returns>Temperature object</returns>
  31.         public static Temperature FromKelvin(float value)
  32.         {
  33.             return new Temperature(value);
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Initializes a new instance of the <see cref="Temperature"/> class using a float in Celsius.
  38.         /// </summary>
  39.         /// <param name="value"></param>
  40.         /// <returns>Temperature object</returns>
  41.         public static Temperature FromCelsius(float value)
  42.         {
  43.             return new Temperature(value + 273.15f);
  44.         }
  45.  
  46.         /// <summary>
  47.         /// Initializes a new instance of the <see cref="Temperature"/> class using a float in Fahrenheit.
  48.         /// </summary>
  49.         /// <param name="value"></param>
  50.         /// <returns>Temperature object</returns>
  51.         public static Temperature FromFahrenheit(float value)
  52.         {
  53.             return new Temperature((value + 459.67f) * 5 / 9);
  54.         }
  55.         #endregion
  56.  
  57.         #region operators
  58.         public static Temperature operator +(Temperature a, Temperature b)
  59.         {
  60.             return new Temperature(a.kelvin + b.kelvin);
  61.         }
  62.  
  63.         public static Temperature operator -(Temperature a, Temperature b)
  64.         {
  65.             return new Temperature(a.kelvin - b.kelvin);
  66.         }
  67.  
  68.         public static Temperature operator *(Temperature a, float b)
  69.         {
  70.             return new Temperature(a.kelvin * b);
  71.         }
  72.  
  73.         public static Temperature operator /(Temperature a, float b)
  74.         {
  75.             return new Temperature(a.kelvin / b);
  76.         }
  77.  
  78.         public static Temperature operator *(float a, Temperature b)
  79.         {
  80.             return new Temperature(a * b.kelvin);
  81.         }
  82.  
  83.         public static Temperature operator /(float a, Temperature b)
  84.         {
  85.             return new Temperature(a / b.kelvin);
  86.         }
  87.  
  88.         public static bool operator ==(Temperature a, Temperature b)
  89.         {
  90.             return a!.Equals(b);
  91.         }
  92.  
  93.         public static bool operator !=(Temperature a, Temperature b)
  94.         {
  95.             return a!.Equals(b) == false;
  96.         }
  97.  
  98.         public static bool operator <(Temperature a, Temperature b)
  99.         {
  100.             return a.kelvin < b.kelvin;
  101.         }
  102.  
  103.         public static bool operator >(Temperature a, Temperature b)
  104.         {
  105.             return a.kelvin > b.kelvin;
  106.         }
  107.  
  108.         public int CompareTo(Temperature other)
  109.         {
  110.             if (ReferenceEquals(this, other)) return 0;
  111.             if (ReferenceEquals(null, other)) return 1;
  112.             return kelvin.CompareTo(other.kelvin);
  113.         }
  114.  
  115.         private bool Equals(Temperature other)
  116.         {
  117.             return kelvin.Equals(other.kelvin);
  118.         }
  119.  
  120.         public override bool Equals(object obj)
  121.         {
  122.             if (ReferenceEquals(null, obj)) return false;
  123.             if (ReferenceEquals(this, obj)) return true;
  124.             if (obj.GetType() != GetType()) return false;
  125.             return Equals((Temperature)obj);
  126.         }
  127.  
  128.         #endregion
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement