1
1
using System ;
2
+ using System . Linq ;
2
3
using TrangTestLib . DataAccess ;
3
4
4
5
namespace TrangTestLib
@@ -7,18 +8,25 @@ public class Temperature
7
8
{
8
9
private TemperatureType tempType ;
9
10
private double tempValue ;
11
+ private char [ ] validStringChars = { '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '0' , '.' , 'c' , 'C' , 'k' , 'K' , 'f' , 'F' , ' ' , '-' , '+' } ;
12
+ private char [ ] validTempTypes = { 'c' , 'C' , 'k' , 'K' , 'f' , 'F' } ;
10
13
11
14
public TemperatureType TempType { get { return tempType ; } }
12
15
public double TempValue { get { return tempValue ; } }
13
16
17
+ /// <summary>
18
+ /// string ctor
19
+ /// </summary>
20
+ /// <param name="_temp">the temperature to convert</param>
14
21
public Temperature ( string _temp )
15
22
{
16
- // split the string
17
- string [ ] values = _temp . Split ( ' ' ) ;
18
- tempValue = Convert . ToDouble ( values [ 0 ] ) ;
19
- tempType = new TemperatureType ( values [ 1 ] ) ;
23
+ ValidateInputString ( _temp ) ;
24
+ ConvertStringToTemperature ( _temp ) ;
20
25
}
21
26
27
+ /// <summary>
28
+ /// basic ctor
29
+ /// </summary>
22
30
public Temperature ( ) { }
23
31
24
32
/// <summary>
@@ -49,6 +57,83 @@ public string ConvertedTemperature(string _typeIndicator)
49
57
}
50
58
}
51
59
60
+ private bool ValidateInputString ( string _temp )
61
+ {
62
+ //convert to character array
63
+ char [ ] stringChars = _temp . ToCharArray ( ) ;
52
64
65
+ // look for the number of characters
66
+ foreach ( char c in stringChars )
67
+ {
68
+ // check that the characters are valid
69
+ if ( validStringChars . Contains ( c ) )
70
+ {
71
+ continue ;
72
+ }
73
+ else
74
+ {
75
+ throw new Exception ( "The entered value contains invalid characters." ) ;
76
+ }
77
+ }
78
+ return true ;
79
+ }
80
+ /// <summary>
81
+ /// this method uses the easy method of splitting the string via the space,
82
+ /// left side is the temperature value, right side is the temperature type
83
+ /// </summary>
84
+ /// <param name="_temp">string representation of a temperature</param>
85
+ private void ConvertStringWithSpaceToTemperature ( string _temp )
86
+ {
87
+ // split the string
88
+ string [ ] values = _temp . Split ( ' ' ) ;
89
+ tempValue = Convert . ToDouble ( values [ 0 ] ) ;
90
+ tempType = new TemperatureType ( values [ 1 ] ) ;
91
+ }
92
+
93
+ /// <summary>
94
+ /// this method takes a string representation of a temperature and figures out how to convert it to a valid temperature class object
95
+ /// </summary>
96
+ /// <param name="_temp">the string representation of a temperature</param>
97
+ private void ConvertStringToTemperature ( string _temp )
98
+ {
99
+ if ( _temp . Contains ( " " ) )
100
+ {
101
+ ConvertStringWithSpaceToTemperature ( _temp ) ;
102
+ }
103
+ else
104
+ {
105
+ try
106
+ {
107
+ // find the type of temperature
108
+ tempType = FindTemperatureType ( _temp ) ;
109
+ // remove the type from the string
110
+ int indicatorIndex = _temp . ToUpper ( ) . IndexOf ( tempType . Indicator ) ;
111
+ string inputValue = _temp . Remove ( indicatorIndex , 1 ) ;
112
+ // convert the remainder to the temperature value
113
+ tempValue = Convert . ToDouble ( inputValue ) ;
114
+ }
115
+ catch ( Exception x )
116
+ {
117
+ throw x ;
118
+ }
119
+ }
120
+ }
121
+
122
+ /// <summary>
123
+ /// this method finds the temperature type from the character in the input string.
124
+ /// </summary>
125
+ /// <param name="_temp">the input string</param>
126
+ /// <returns>a TemperatureType Object</returns>
127
+ private TemperatureType FindTemperatureType ( string _temp )
128
+ {
129
+ foreach ( char c in _temp )
130
+ {
131
+ if ( validTempTypes . Contains ( c ) )
132
+ {
133
+ return new TemperatureType ( c . ToString ( ) ) ;
134
+ }
135
+ }
136
+ throw new Exception ( "No valid temperature type was found in the input string." ) ;
137
+ }
53
138
}
54
139
}
0 commit comments