Skip to content

Commit 9b50b1d

Browse files
committed
fixes #4
1 parent b2dd484 commit 9b50b1d

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

Solution/TrangTestLib/DataAccess/TemperatureType.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class TemperatureType
1515
public TemperatureType(string _indicator)
1616
{
1717

18-
Data.TrangTest.TemperatureTypesRow typeRow = Data.XMLOperations.ReadXML().TemperatureTypes.First(tt => tt.TempType_Indicator == _indicator);
18+
Data.TrangTest.TemperatureTypesRow typeRow = Data.XMLOperations.ReadXML().TemperatureTypes.First(tt => tt.TempType_Indicator == _indicator.ToUpper());
1919
id = typeRow.TempType_ID;
2020
name = typeRow.TempType_Name;
2121
indicator = typeRow.TempType_Indicator;

Solution/TrangTestLib/Temperature.cs

+89-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using TrangTestLib.DataAccess;
34

45
namespace TrangTestLib
@@ -7,18 +8,25 @@ public class Temperature
78
{
89
private TemperatureType tempType;
910
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' };
1013

1114
public TemperatureType TempType { get { return tempType; } }
1215
public double TempValue { get { return tempValue; } }
1316

17+
/// <summary>
18+
/// string ctor
19+
/// </summary>
20+
/// <param name="_temp">the temperature to convert</param>
1421
public Temperature(string _temp)
1522
{
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);
2025
}
2126

27+
/// <summary>
28+
/// basic ctor
29+
/// </summary>
2230
public Temperature() { }
2331

2432
/// <summary>
@@ -49,6 +57,83 @@ public string ConvertedTemperature(string _typeIndicator)
4957
}
5058
}
5159

60+
private bool ValidateInputString(string _temp)
61+
{
62+
//convert to character array
63+
char[] stringChars = _temp.ToCharArray();
5264

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+
}
53138
}
54139
}

Solution/TrangTestStub/Form1.cs

-1
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,5 @@ private void EnableInputs()
514514
txtTestTemps.Enabled = true;
515515
}
516516

517-
518517
}
519518
}

0 commit comments

Comments
 (0)