Created comparison value

This commit is contained in:
Lukasz Baldyga committed 2020-11-29 10:05:56 +00:00
1 parent 2b3beffdd4
commit 5844e489da
2 files changed
+73

No files matched your search

@@ -91,6 +91,7 @@
<Compile Include="Verbs\DrawingVerbs\Shape\Square.cs" /> <Compile Include="Verbs\DrawingVerbs\Shape\Square.cs" />
<Compile Include="Verbs\DrawingVerbs\Shape\Triangle.cs" /> <Compile Include="Verbs\DrawingVerbs\Shape\Triangle.cs" />
<Compile Include="Verbs\Value\ValueObjects\PointValue.cs" /> <Compile Include="Verbs\Value\ValueObjects\PointValue.cs" />
<Compile Include="Verbs\Value\ValueTypes\ComparisonValue.cs" />
<Compile Include="Verbs\Value\ValueTypes\ExpressionValue.cs" /> <Compile Include="Verbs\Value\ValueTypes\ExpressionValue.cs" />
<Compile Include="Verbs\Value\DeclareVariable.cs" /> <Compile Include="Verbs\Value\DeclareVariable.cs" />
<Compile Include="Verbs\Value\UpdateVariable.cs" /> <Compile Include="Verbs\Value\UpdateVariable.cs" />
@@ -0,0 +1,72 @@
using System;
using System.Drawing;
namespace Advanced_Software_Engineering.Verbs.Value.ValueTypes {
internal class ComparisonValue : IValue {
private string op;
private IValue value1;
private IValue value2;
public ComparisonValue(IValue value1, IValue value2, string op) {
this.value1 = value1;
this.value2 = value2;
switch (op) {
case "=":
case "!":
case ">":
case "<":
break;
default:
throw new Exception("Invalid operation");
}
this.op = op;
}
public string GetDescription() {
return "Compare two values";
}
public string GetOriginalType() {
return "comparison";
}
public bool isInitialised() {
return true;
}
public bool ToBool() {
switch (op) {
case "=":
return value1.ToBool() == value2.ToBool() && value1.ToDouble() == value2.ToDouble() && value1.ToInt() == value2.ToInt();
case "!":
return value1.ToBool() != value2.ToBool() && value1.ToDouble() != value2.ToDouble() && value1.ToInt() != value2.ToInt();
case ">":
return value1.ToDouble() > value2.ToDouble() && value1.ToInt() > value2.ToInt();
case "<":
return value1.ToDouble() < value2.ToDouble() && value1.ToInt() < value2.ToInt();
default:
throw new Exception("Bad operator");
}
}
public Color ToColor() {
throw new Exception("Cannot convert comparison to color");
}
public double ToDouble() {
throw new Exception("Cannot convert comparison to double");
}
public int ToInt() {
throw new Exception("Cannot convert comparison to int");
}
}
}