Skip to content

Commit

Permalink
fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
acnicholls committed Aug 29, 2020
1 parent b2dd484 commit 9b50b1d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Solution/TrangTestLib/DataAccess/TemperatureType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TemperatureType
public TemperatureType(string _indicator)
{

Data.TrangTest.TemperatureTypesRow typeRow = Data.XMLOperations.ReadXML().TemperatureTypes.First(tt => tt.TempType_Indicator == _indicator);
Data.TrangTest.TemperatureTypesRow typeRow = Data.XMLOperations.ReadXML().TemperatureTypes.First(tt => tt.TempType_Indicator == _indicator.ToUpper());
id = typeRow.TempType_ID;
name = typeRow.TempType_Name;
indicator = typeRow.TempType_Indicator;
Expand Down
93 changes: 89 additions & 4 deletions Solution/TrangTestLib/Temperature.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using TrangTestLib.DataAccess;

namespace TrangTestLib
Expand All @@ -7,18 +8,25 @@ public class Temperature
{
private TemperatureType tempType;
private double tempValue;
private char[] validStringChars = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.', 'c', 'C', 'k', 'K', 'f', 'F', ' ', '-', '+' };
private char[] validTempTypes = { 'c', 'C', 'k', 'K', 'f', 'F' };

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

/// <summary>
/// string ctor
/// </summary>
/// <param name="_temp">the temperature to convert</param>
public Temperature(string _temp)
{
// split the string
string[] values = _temp.Split(' ');
tempValue = Convert.ToDouble(values[0]);
tempType = new TemperatureType(values[1]);
ValidateInputString(_temp);
ConvertStringToTemperature(_temp);
}

/// <summary>
/// basic ctor
/// </summary>
public Temperature() { }

/// <summary>
Expand Down Expand Up @@ -49,6 +57,83 @@ public string ConvertedTemperature(string _typeIndicator)
}
}

private bool ValidateInputString(string _temp)
{
//convert to character array
char[] stringChars = _temp.ToCharArray();

// look for the number of characters
foreach (char c in stringChars)
{
// check that the characters are valid
if (validStringChars.Contains(c))
{
continue;
}
else
{
throw new Exception("The entered value contains invalid characters.");
}
}
return true;
}
/// <summary>
/// this method uses the easy method of splitting the string via the space,
/// left side is the temperature value, right side is the temperature type
/// </summary>
/// <param name="_temp">string representation of a temperature</param>
private void ConvertStringWithSpaceToTemperature(string _temp)
{
// split the string
string[] values = _temp.Split(' ');
tempValue = Convert.ToDouble(values[0]);
tempType = new TemperatureType(values[1]);
}

/// <summary>
/// this method takes a string representation of a temperature and figures out how to convert it to a valid temperature class object
/// </summary>
/// <param name="_temp">the string representation of a temperature</param>
private void ConvertStringToTemperature(string _temp)
{
if(_temp.Contains(" "))
{
ConvertStringWithSpaceToTemperature(_temp);
}
else
{
try
{
// find the type of temperature
tempType = FindTemperatureType(_temp);
// remove the type from the string
int indicatorIndex = _temp.ToUpper().IndexOf(tempType.Indicator);
string inputValue = _temp.Remove(indicatorIndex, 1);
// convert the remainder to the temperature value
tempValue = Convert.ToDouble(inputValue);
}
catch(Exception x)
{
throw x;
}
}
}

/// <summary>
/// this method finds the temperature type from the character in the input string.
/// </summary>
/// <param name="_temp">the input string</param>
/// <returns>a TemperatureType Object</returns>
private TemperatureType FindTemperatureType(string _temp)
{
foreach(char c in _temp)
{
if(validTempTypes.Contains(c))
{
return new TemperatureType(c.ToString());
}
}
throw new Exception("No valid temperature type was found in the input string.");
}
}
}
1 change: 0 additions & 1 deletion Solution/TrangTestStub/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,5 @@ private void EnableInputs()
txtTestTemps.Enabled = true;
}


}
}

0 comments on commit 9b50b1d

Please sign in to comment.