PriceGroup.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. namespace Wayne.ForecourtControl
  3. {
  4. /// <summary>
  5. /// Price group stucture. Contains an underlying int between 0 and 11.
  6. /// </summary>
  7. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes")]
  8. public struct PriceGroup
  9. {
  10. #region Fields
  11. int value;
  12. #endregion
  13. #region Construction
  14. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "System.ArgumentException.#ctor(System.String)")]
  15. private PriceGroup(int newValue, bool forceValue)
  16. {
  17. if ((newValue >= MinValue) && (newValue <= MaxValue) || forceValue)
  18. {
  19. value = newValue;
  20. }
  21. else
  22. throw new ArgumentException(
  23. "Price group must be within the bounds " +
  24. MinValue.ToString(System.Globalization.CultureInfo.InvariantCulture) +
  25. "-" +
  26. MaxValue.ToString(System.Globalization.CultureInfo.InvariantCulture
  27. ));
  28. }
  29. /// <summary>
  30. ///
  31. /// </summary>
  32. /// <param name="newValue"></param>
  33. public PriceGroup(int newValue)
  34. : this(newValue, false)
  35. {
  36. }
  37. #endregion
  38. #region Methods
  39. /// <summary>
  40. /// Equality comparer.
  41. /// </summary>
  42. /// <param name="obj"></param>
  43. /// <returns></returns>
  44. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2231:OverloadOperatorEqualsOnOverridingValueTypeEquals")]
  45. public override bool Equals(object obj)
  46. {
  47. if (obj is PriceGroup)
  48. return (int)((PriceGroup)obj) == this.value;
  49. else
  50. return false;
  51. }
  52. /// <summary>
  53. /// Hash Code generator.
  54. /// </summary>
  55. /// <returns></returns>
  56. public override int GetHashCode()
  57. {
  58. return value.GetHashCode();
  59. }
  60. #endregion
  61. #region Debug methods
  62. /// <summary>
  63. /// Presents the class as a string.
  64. /// </summary>
  65. /// <returns></returns>
  66. public string ToString(string format, IFormatProvider provider)
  67. {
  68. string extraText = " ";
  69. switch (value)
  70. {
  71. case -1: extraText = " Unknown"; break;
  72. case 0: extraText = " FullService"; break;
  73. case 1: extraText = " PayInKiosk"; break;
  74. case 2: extraText = " OptCard"; break;
  75. case 3: extraText = " OptCash"; break;
  76. }
  77. return value.ToString(format, provider) + extraText;
  78. }
  79. /// <summary>
  80. /// Presents the class as a string using the specified culture-specific format information.
  81. /// </summary>
  82. /// <returns></returns>
  83. public string ToString(IFormatProvider provider)
  84. {
  85. return ToString("", provider);
  86. }
  87. /// <summary>
  88. /// Presents the class as a string using a format string.
  89. /// </summary>
  90. /// <returns></returns>
  91. public string ToString(string format)
  92. {
  93. return ToString(format, System.Globalization.CultureInfo.InvariantCulture);
  94. }
  95. /// <summary>
  96. /// Presents the class as a string using a format string and the specified culture-specific format information.
  97. /// </summary>
  98. /// <returns></returns>
  99. public override string ToString()
  100. {
  101. return ToString("", System.Globalization.CultureInfo.InvariantCulture);
  102. }
  103. #endregion
  104. #region Conversions
  105. /// <summary>
  106. ///
  107. /// </summary>
  108. /// <param name="thePriceGroup"></param>
  109. /// <returns></returns>
  110. public static implicit operator int(PriceGroup thePriceGroup)
  111. {
  112. return thePriceGroup.value;
  113. }
  114. /// <summary>
  115. ///
  116. /// </summary>
  117. /// <param name="priceGroupId"></param>
  118. /// <returns></returns>
  119. public static implicit operator PriceGroup(int priceGroupId)
  120. {
  121. return new PriceGroup(priceGroupId);
  122. }
  123. #endregion
  124. #region Public constants
  125. /// <summary>
  126. ///
  127. /// </summary>
  128. public const int MinValue = 0;
  129. /// <summary>
  130. ///
  131. /// </summary>
  132. public const int MaxValue = 11;
  133. /// <summary>
  134. ///
  135. /// </summary>
  136. public static readonly PriceGroup FullService = 0;
  137. /// <summary>
  138. ///
  139. /// </summary>
  140. public static readonly PriceGroup PayInKiosk = 1;
  141. /// <summary>
  142. ///
  143. /// </summary>
  144. public static readonly PriceGroup OptCash = 2;
  145. /// <summary>
  146. ///
  147. /// </summary>
  148. public static readonly PriceGroup OptCard = 3;
  149. /// <summary>
  150. ///
  151. /// </summary>
  152. public static readonly PriceGroup Unknown = new PriceGroup(-1, true);
  153. #endregion
  154. }
  155. }