|
1 | 1 | using System;
|
2 | 2 | using System.Globalization;
|
3 | 3 |
|
4 |
| -namespace ca1036 |
| 4 | +//<snippet1> |
| 5 | +// Valid ratings are between A and C. |
| 6 | +// A is the highest rating; it is greater than any other valid rating. |
| 7 | +// C is the lowest rating; it is less than any other valid rating. |
| 8 | + |
| 9 | +public class RatingInformation : IComparable, IComparable<RatingInformation> |
5 | 10 | {
|
6 |
| - //<snippet1> |
7 |
| - // Valid ratings are between A and C. |
8 |
| - // A is the highest rating; it is greater than any other valid rating. |
9 |
| - // C is the lowest rating; it is less than any other valid rating. |
| 11 | + public string Rating { get; private set; } |
10 | 12 |
|
11 |
| - public class RatingInformation : IComparable, IComparable<RatingInformation> |
| 13 | + public RatingInformation(string rating) |
12 | 14 | {
|
13 |
| - public string Rating |
14 |
| - { |
15 |
| - get; |
16 |
| - private set; |
17 |
| - } |
| 15 | + ArgumentNullException.ThrowIfNull(rating); |
18 | 16 |
|
19 |
| - public RatingInformation(string rating) |
| 17 | + string v = rating.ToUpper(CultureInfo.InvariantCulture); |
| 18 | + if (v.Length != 1 || |
| 19 | + string.Compare(v, "C", StringComparison.Ordinal) > 0 || |
| 20 | + string.Compare(v, "A", StringComparison.Ordinal) < 0) |
20 | 21 | {
|
21 |
| - if (rating == null) |
22 |
| - { |
23 |
| - throw new ArgumentNullException("rating"); |
24 |
| - } |
25 |
| - |
26 |
| - string v = rating.ToUpper(CultureInfo.InvariantCulture); |
27 |
| - if (v.Length != 1 || string.Compare(v, "C", StringComparison.Ordinal) > 0 || string.Compare(v, "A", StringComparison.Ordinal) < 0) |
28 |
| - { |
29 |
| - throw new ArgumentException("Invalid rating value was specified.", "rating"); |
30 |
| - } |
31 |
| - |
32 |
| - Rating = v; |
| 22 | + throw new ArgumentException("Invalid rating value was specified.", nameof(rating)); |
33 | 23 | }
|
34 | 24 |
|
35 |
| - public int CompareTo(object? obj) |
36 |
| - { |
37 |
| - if (obj == null) |
38 |
| - { |
39 |
| - return 1; |
40 |
| - } |
41 |
| - |
42 |
| - if (obj is RatingInformation other) |
43 |
| - { |
44 |
| - return CompareTo(other); |
45 |
| - } |
46 |
| - |
47 |
| - throw new ArgumentException("A RatingInformation object is required for comparison.", "obj"); |
48 |
| - } |
| 25 | + Rating = v; |
| 26 | + } |
49 | 27 |
|
50 |
| - public int CompareTo(RatingInformation? other) |
| 28 | + public int CompareTo(object? obj) |
| 29 | + { |
| 30 | + if (obj == null) |
51 | 31 | {
|
52 |
| - if (other is null) |
53 |
| - { |
54 |
| - return 1; |
55 |
| - } |
56 |
| - |
57 |
| - // Ratings compare opposite to normal string order, |
58 |
| - // so reverse the value returned by String.CompareTo. |
59 |
| - return -string.Compare(this.Rating, other.Rating, StringComparison.OrdinalIgnoreCase); |
| 32 | + return 1; |
60 | 33 | }
|
61 | 34 |
|
62 |
| - public static int Compare(RatingInformation left, RatingInformation right) |
| 35 | + if (obj is RatingInformation other) |
63 | 36 | {
|
64 |
| - if (object.ReferenceEquals(left, right)) |
65 |
| - { |
66 |
| - return 0; |
67 |
| - } |
68 |
| - if (left is null) |
69 |
| - { |
70 |
| - return -1; |
71 |
| - } |
72 |
| - return left.CompareTo(right); |
| 37 | + return CompareTo(other); |
73 | 38 | }
|
74 | 39 |
|
75 |
| - // Omitting Equals violates rule: OverrideMethodsOnComparableTypes. |
76 |
| - public override bool Equals(object? obj) |
77 |
| - { |
78 |
| - if (obj is RatingInformation other) |
79 |
| - { |
80 |
| - return this.CompareTo(other) == 0; |
81 |
| - } |
82 |
| - |
83 |
| - return false; |
84 |
| - } |
| 40 | + throw new ArgumentException("A RatingInformation object is required for comparison.", nameof(obj)); |
| 41 | + } |
85 | 42 |
|
86 |
| - // Omitting getHashCode violates rule: OverrideGetHashCodeOnOverridingEquals. |
87 |
| - public override int GetHashCode() |
| 43 | + public int CompareTo(RatingInformation? other) |
| 44 | + { |
| 45 | + if (other is null) |
88 | 46 | {
|
89 |
| - char[] c = this.Rating.ToCharArray(); |
90 |
| - return (int)c[0]; |
| 47 | + return 1; |
91 | 48 | }
|
92 | 49 |
|
93 |
| - // Omitting any of the following operator overloads |
94 |
| - // violates rule: OverrideMethodsOnComparableTypes. |
95 |
| - public static bool operator ==(RatingInformation left, RatingInformation right) |
| 50 | + // Ratings compare opposite to normal string order, |
| 51 | + // so reverse the value returned by String.CompareTo. |
| 52 | + return -string.Compare(Rating, other.Rating, StringComparison.OrdinalIgnoreCase); |
| 53 | + } |
| 54 | + |
| 55 | + public static int Compare(RatingInformation left, RatingInformation right) |
| 56 | + { |
| 57 | + if (object.ReferenceEquals(left, right)) |
96 | 58 | {
|
97 |
| - if (left is null) |
98 |
| - { |
99 |
| - return right is null; |
100 |
| - } |
101 |
| - return left.Equals(right); |
| 59 | + return 0; |
102 | 60 | }
|
103 |
| - public static bool operator !=(RatingInformation left, RatingInformation right) |
| 61 | + if (left is null) |
104 | 62 | {
|
105 |
| - return !(left == right); |
| 63 | + return -1; |
106 | 64 | }
|
107 |
| - public static bool operator <(RatingInformation left, RatingInformation right) |
| 65 | + return left.CompareTo(right); |
| 66 | + } |
| 67 | + |
| 68 | + // Omitting Equals violates rule: OverrideMethodsOnComparableTypes. |
| 69 | + public override bool Equals(object? obj) |
| 70 | + { |
| 71 | + if (obj is RatingInformation other) |
108 | 72 | {
|
109 |
| - return (Compare(left, right) < 0); |
| 73 | + return CompareTo(other) == 0; |
110 | 74 | }
|
111 |
| - public static bool operator >(RatingInformation left, RatingInformation right) |
| 75 | + |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + // Omitting getHashCode violates rule: OverrideGetHashCodeOnOverridingEquals. |
| 80 | + public override int GetHashCode() |
| 81 | + { |
| 82 | + char[] c = Rating.ToCharArray(); |
| 83 | + return (int)c[0]; |
| 84 | + } |
| 85 | + |
| 86 | + // Omitting any of the following operator overloads |
| 87 | + // violates rule: OverrideMethodsOnComparableTypes. |
| 88 | + public static bool operator ==(RatingInformation left, RatingInformation right) |
| 89 | + { |
| 90 | + if (left is null) |
112 | 91 | {
|
113 |
| - return (Compare(left, right) > 0); |
| 92 | + return right is null; |
114 | 93 | }
|
| 94 | + return left.Equals(right); |
115 | 95 | }
|
116 |
| - //</snippet1> |
| 96 | + public static bool operator !=(RatingInformation left, RatingInformation right) |
| 97 | + { |
| 98 | + return !(left == right); |
| 99 | + } |
| 100 | + public static bool operator <(RatingInformation left, RatingInformation right) |
| 101 | + { |
| 102 | + return (Compare(left, right) < 0); |
| 103 | + } |
| 104 | + public static bool operator >(RatingInformation left, RatingInformation right) |
| 105 | + { |
| 106 | + return (Compare(left, right) > 0); |
| 107 | + } |
| 108 | +} |
| 109 | +//</snippet1> |
117 | 110 |
|
118 |
| - //<snippet2> |
119 |
| - public class Test |
| 111 | +//<snippet2> |
| 112 | +public class TestCompare |
| 113 | +{ |
| 114 | + public static void Main1036(params string[] args) |
120 | 115 | {
|
121 |
| - public static void Main1036(string[] args) |
| 116 | + if (args.Length < 2) |
122 | 117 | {
|
123 |
| - if (args.Length < 2) |
124 |
| - { |
125 |
| - Console.WriteLine("usage - TestRatings string 1 string2"); |
126 |
| - return; |
127 |
| - } |
128 |
| - RatingInformation r1 = new RatingInformation(args[0]); |
129 |
| - RatingInformation r2 = new RatingInformation(args[1]); |
130 |
| - string answer; |
131 |
| - |
132 |
| - if (r1.CompareTo(r2) > 0) |
133 |
| - answer = "greater than"; |
134 |
| - else if (r1.CompareTo(r2) < 0) |
135 |
| - answer = "less than"; |
136 |
| - else |
137 |
| - answer = "equal to"; |
138 |
| - |
139 |
| - Console.WriteLine("{0} is {1} {2}", r1.Rating, answer, r2.Rating); |
| 118 | + return; |
140 | 119 | }
|
| 120 | + RatingInformation r1 = new(args[0]); |
| 121 | + RatingInformation r2 = new(args[1]); |
| 122 | + string answer; |
| 123 | + |
| 124 | + if (r1.CompareTo(r2) > 0) |
| 125 | + answer = "greater than"; |
| 126 | + else if (r1.CompareTo(r2) < 0) |
| 127 | + answer = "less than"; |
| 128 | + else |
| 129 | + answer = "equal to"; |
| 130 | + |
| 131 | + Console.WriteLine("{0} is {1} {2}", r1.Rating, answer, r2.Rating); |
141 | 132 | }
|
142 |
| - //</snippet2> |
143 | 133 | }
|
| 134 | +//</snippet2> |
0 commit comments