Smart rounding was ajusted

This commit is contained in:
Evgeny Redikultsev
2024-06-02 16:56:44 +05:00
parent 99d5aa3608
commit 31d668b996
58 changed files with 716 additions and 274 deletions

View File

@@ -1,116 +0,0 @@
using StructureHelperCommon.Infrastructures.Enums;
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Services.Units;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Documents;
namespace StructureHelperCommon.Services.Units
{
public static class CommonOperation
{
private static IEnumerable<IUnit> units = UnitsFactory.GetUnitCollection();
public static double ConvertToDoubleChangeComma(string s)
{
double result;
if (!double.TryParse(s, NumberStyles.Any, CultureInfo.CurrentCulture, out result) &&
!double.TryParse(s, NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out result) &&
!double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
{
throw new StructureHelperException($"{ErrorStrings.IncorrectValue}: {s}");
}
return result;
}
public static IStringDoublePair DivideIntoStringDoublePair(string s)
{
s = s.Replace(" ", "");
//string digitPattern = @"^[-]?[+]?\d+(\.?)|(\,?)\d*";
string digitPattern = @"^[-]?[+]?\d*\.?\,?\d*";
string textPattern = @"[0-9]|\.|\,";
string caracterPattern = "[a-z]+$";
string target = "";
Regex regexText = new Regex(textPattern);
string textString = regexText.Replace(s, target);
var textMatch = Regex.Match(textString, caracterPattern, RegexOptions.IgnoreCase);
if (textMatch.Success) {textString = textMatch.Value.ToLower();}
var match = Regex.Match(s, digitPattern);
if (match.Success)
{
string digitString = match.Value;
double digit = ConvertToDoubleChangeComma(digitString);
return new StringDoublePair() { Digit = digit, Text = textString };
}
throw new StructureHelperException(ErrorStrings.DataIsInCorrect);
}
public static IUnit GetUnit(UnitTypes unitType, string unitName = null)
{
if (unitName is null)
{
var boolResult = DefaultUnitNames.TryGetValue(unitType, out unitName);
if (boolResult == false)
{
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + $": unit type{unitType} is unknown");
}
}
return units.Where(u => u.UnitType == unitType & u.Name == unitName).Single();
}
public static Dictionary<UnitTypes, string> DefaultUnitNames => new()
{
{ UnitTypes.Length, "m"},
{ UnitTypes.Area, "m2"},
{ UnitTypes.Force, "kN" },
{ UnitTypes.Moment, "kNm"},
{ UnitTypes.Stress, "MPa"},
{ UnitTypes.Curvature, "1/m"},
};
public static string Convert(IUnit unit, string unitName, object value)
{
double val;
if (value != null) { val = (double)value; }
else { throw new Exception($"{unitName} value is null"); }
val *= unit.Multiplyer;
string strValue = $"{val} {unit.Name}";
return strValue;
}
public static double ConvertBack(UnitTypes unitType, IUnit unit, object value)
{
double val;
double multy;
double coefficient = unit.Multiplyer;
var strVal = value as string;
IStringDoublePair pair = DivideIntoStringDoublePair(strVal);
try
{
multy = GetMultiplyer(unitType, pair.Text);
}
catch (Exception ex)
{
multy = coefficient;
}
val = pair.Digit / multy;
return val;
}
public static double GetMultiplyer(UnitTypes unitType, string unitName)
{
try
{
return units.Where(u => u.UnitType == unitType & u.Name == unitName).Single().Multiplyer;
}
catch (Exception ex)
{
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + ex);
}
}
}
}

View File

@@ -0,0 +1,85 @@
using StructureHelperCommon.Infrastructures.Enums;
using StructureHelperCommon.Infrastructures.Exceptions;
using StructureHelperCommon.Models.Parameters;
using StructureHelperCommon.Services.Units;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Documents;
namespace StructureHelperCommon.Services.Units
{
public class ConvertUnitLogic : IConvertUnitLogic
{
private static IEnumerable<IUnit> units = UnitsFactory.GetUnitCollection();
private static IProcessValuePairLogic<double> pairLogic = new ProcessDoublePairLogic();
public IMathRoundLogic MathRoundLogic { get; set; } = new DirectRoundLogic();
public ValuePair<double> Convert(IUnit unit, string unitName, object value)
{
double val;
if (value != null)
{
try
{
val = (double)value;
}
catch (Exception ex)
{
throw new StructureHelperException($"{ErrorStrings.IncorrectValue}");
}
}
else
{
throw new StructureHelperException($"{ErrorStrings.ParameterIsNull}: {unitName}");
}
val *= unit.Multiplyer;
var pair = new ValuePair<double>
{
Text = unit.Name,
Value = val
};
return pair;
}
public double ConvertBack(UnitTypes unitType, IUnit unit, object value)
{
double val;
double multy;
double factor = unit.Multiplyer;
var strVal = value as string;
var pair = pairLogic.GetValuePairByString(strVal);
try
{
multy = GetMultiplyer(unitType, pair.Text);
}
catch (Exception ex)
{
multy = factor;
}
val = pair.Value / multy;
return val;
}
private double GetMultiplyer(UnitTypes unitType, string unitName)
{
try
{
return units
.Where(u =>
u.UnitType == unitType
&
u.Name == unitName)
.Single()
.Multiplyer;
}
catch (Exception ex)
{
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + ex);
}
}
}
}

View File

@@ -0,0 +1,50 @@
using StructureHelperCommon.Infrastructures.Enums;
using StructureHelperCommon.Infrastructures.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services.Units
{
public class GetUnitLogic : IGetUnitLogic
{
private static IEnumerable<IUnit> units = UnitsFactory.GetUnitCollection();
private Dictionary<UnitTypes, string> defaultUnitNames;
public GetUnitLogic()
{
defaultUnitNames = new()
{
{ UnitTypes.Length, "m"},
{ UnitTypes.Area, "m2"},
{ UnitTypes.Force, "kN" },
{ UnitTypes.Moment, "kNm"},
{ UnitTypes.Stress, "MPa"},
{ UnitTypes.Curvature, "1/m"},
};
}
public IUnit GetUnit(UnitTypes unitType, string unitName = null)
{
if (unitName is null)
{
var boolResult = defaultUnitNames.TryGetValue(unitType, out unitName);
if (boolResult == false)
{
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + $": unit type{unitType} is unknown");
}
}
return units
.Where(u =>
u.UnitType == unitType
&
u.Name == unitName)
.Single();
}
}
}

View File

@@ -0,0 +1,13 @@
using StructureHelperCommon.Infrastructures.Enums;
using StructureHelperCommon.Models.Parameters;
using System.Collections.Generic;
namespace StructureHelperCommon.Services.Units
{
public interface IConvertUnitLogic
{
IMathRoundLogic MathRoundLogic { get; set; }
ValuePair<double> Convert(IUnit unit, string unitName, object value);
double ConvertBack(UnitTypes unitType, IUnit unit, object value);
}
}

View File

@@ -0,0 +1,9 @@
using StructureHelperCommon.Infrastructures.Enums;
namespace StructureHelperCommon.Services.Units
{
public interface IGetUnitLogic
{
IUnit GetUnit(UnitTypes unitType, string unitName = null);
}
}

View File

@@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services.Units
{
public interface IStringDoublePair
{
double Digit { get; }
string Text { get; }
}
}

View File

@@ -0,0 +1,25 @@
using StructureHelperCommon.Infrastructures.Exceptions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services.Units
{
public static class ProcessString
{
public static double ConvertCommaToCultureSettings(string s)
{
double result;
if (!double.TryParse(s, NumberStyles.Any, CultureInfo.CurrentCulture, out result) &&
!double.TryParse(s, NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out result) &&
!double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
{
throw new StructureHelperException($"{ErrorStrings.IncorrectValue}: {s}");
}
return result;
}
}
}

View File

@@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StructureHelperCommon.Services.Units
{
internal class StringDoublePair : IStringDoublePair
{
public double Digit { get; set; }
public string Text { get; set; }
}
}

View File

@@ -5,6 +5,10 @@ namespace StructureHelperCommon.Services.Units
{
public static class UnitsFactory
{
/// <summary>
/// Returns collection of unit
/// </summary>
/// <returns></returns>
public static List<IUnit> GetUnitCollection()
{
List<IUnit> units = new List<IUnit>();
@@ -28,8 +32,8 @@ namespace StructureHelperCommon.Services.Units
type = UnitTypes.Moment;
units.Add(new Unit() { UnitType = type, Name = "Nm", Multiplyer = 1d });
units.Add(new Unit() { UnitType = type, Name = "kNm", Multiplyer = 1e-3d });
units.Add(new Unit() { UnitType = type, Name = "kgfm", Multiplyer = 9.8d });
units.Add(new Unit() { UnitType = type, Name = "tfm", Multiplyer = 9.8e-3d });
units.Add(new Unit() { UnitType = type, Name = "kgfm", Multiplyer = 9.81d });
units.Add(new Unit() { UnitType = type, Name = "tfm", Multiplyer = 9.81e-3d });
type = UnitTypes.Curvature;
units.Add(new Unit() { UnitType = type, Name = "1/m", Multiplyer = 1d });
units.Add(new Unit() { UnitType = type, Name = "1/mm", Multiplyer = 1e-3d });