Skip to content

Commit

Permalink
feat: Add CalcResult class
Browse files Browse the repository at this point in the history
  • Loading branch information
Vovanda committed Jul 28, 2020
1 parent 02cf1ff commit b89766e
Showing 1 changed file with 241 additions and 0 deletions.
241 changes: 241 additions & 0 deletions csharp/Platform.Numbers/CalcResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
using System;

namespace Platform.Numbers
{
public class CalcResult<T>
{
public CalcResult() : this(default) { }

public CalcResult(T value)
{
Value = value;
IsValid = true;
}

#region Arithmetic operations
public CalcResult<T> Add(T a)
{
if (IsValid)
{
try
{
_value = Arithmetic<T>.Add(_value, a);
}
catch (Exception e)
{
Message = e.Message;
IsValid = false;
}
}
return this;
}

public CalcResult<T> Subtract(T a)
{
if (IsValid)
{
try
{
_value = Arithmetic<T>.Subtract(_value, a);
}
catch (Exception e)
{
Message = e.Message;
IsValid = false;
}
}
return this;
}

public CalcResult<T> Multiply(T a)
{
if (IsValid)
{
try
{
_value = Arithmetic<T>.Multiply(_value, a);
}
catch (Exception e)
{
Message = e.Message;
IsValid = false;
}
}
return this;
}

public CalcResult<T> Divide(T a)
{
if (IsValid)
{
try
{
_value = Arithmetic<T>.Divide(_value, a);
}
catch (Exception e)
{
Message = e.Message;
IsValid = false;
}
}
return this;
}

public CalcResult<T> Add(CalcResult<T> a)
{
if (IsValid &= a.IsValid)
{
try
{
_value = Arithmetic<T>.Add(_value, a._value);
}
catch (Exception e)
{
Message = e.Message;
IsValid = false;
}
}
return this;
}

public CalcResult<T> Subtract(CalcResult<T> a)
{
if (IsValid &= a.IsValid)
{
try
{
_value = Arithmetic<T>.Subtract(_value, a._value);
}
catch (Exception e)
{
Message = e.Message;
IsValid = false;
}
}
return this;
}

public CalcResult<T> Multiply(CalcResult<T> a)
{
if (IsValid &= a.IsValid)
{
try
{
_value = Arithmetic<T>.Multiply(_value, a._value);
}
catch (Exception e)
{
Message = e.Message;
IsValid = false;
}
}
return this;
}

public CalcResult<T> Divide(CalcResult<T> a)
{
if (IsValid &= a.IsValid)
{
try
{
_value = Arithmetic<T>.Divide(_value, a._value);
}
catch (Exception e)
{
Message = e.Message;
IsValid = false;
}
}
return this;
}

public CalcResult<T> Increment()
{
if (IsValid)
{
try
{
_value = Arithmetic<T>.Increment(_value);
}
catch (Exception e)
{
Message = e.Message;
IsValid = false;
}
}
return this;
}

public CalcResult<T> Decrement()
{
if (IsValid)
{
try
{
_value = Arithmetic<T>.Decrement(_value);
}
catch (Exception e)
{
Message = e.Message;
IsValid = false;
}
}
return this;
}
#endregion

public T Value
{
get => _value;
set
{
_value = value;
IsValid = true;
}
}

public bool IsValid;

public string Message { get; private set; }

private T _value;

#region Operator overloading
public static CalcResult<T> operator ++(CalcResult<T> x) => x.Increment();

public static CalcResult<T> operator --(CalcResult<T> x) => x.Decrement();

public static CalcResult<T> operator +(CalcResult<T> x, T y) => x.Add(y);

public static CalcResult<T> operator -(CalcResult<T> x, T y) => x.Subtract(y);

public static CalcResult<T> operator *(CalcResult<T> x, T y) => x.Multiply(y);

public static CalcResult<T> operator /(CalcResult<T> x, T y) => x.Divide(y);

public static CalcResult<T> operator +(CalcResult<T> x, CalcResult<T> y) => x.Add(y);

public static CalcResult<T> operator -(CalcResult<T> x, CalcResult<T> y) => x.Subtract(y);

public static CalcResult<T> operator *(CalcResult<T> x, CalcResult<T> y) => x.Multiply(y);

public static CalcResult<T> operator /(CalcResult<T> x, CalcResult<T> y) => x.Divide(y);
#endregion

//public static explicit operator CalcResult<T>(T value)
//{
// return new CalcResult<T>(value);
//}


public static implicit operator T(CalcResult<T> v)
{
return v.Value;
}

public static implicit operator CalcResult<T>(T v)
{
return new CalcResult<T>(v);
}
}
}

0 comments on commit b89766e

Please sign in to comment.