Converted some files to IValue instead of primitives

This commit is contained in:
Lukasz Baldyga committed 2020-11-27 19:46:36 +00:00
1 parent 17f3f2aae4
commit 95ad56f18a
5 files changed
+44 -38

No files matched your search

@@ -1,18 +1,20 @@
namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
using Advanced_Software_Engineering.Verbs.Value;
namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
/// <summary>
/// Circle IVerb class.
/// </summary>
public class Circle : IVerb {
private readonly Drawer drawer;
private readonly double scale;
private readonly IValue scale;
/// <summary>
/// Circle constructor creates the Circle IVerb.
/// </summary>
/// <param name="drawer">drawer</param>
/// <param name="scale">scale of the circle</param>
public Circle(Drawer drawer, double scale) {
public Circle(Drawer drawer, IValue scale) {
this.drawer = drawer;
this.scale = scale;
}
@@ -21,6 +23,7 @@
/// Execute the Circle IVerb, which is to draw a circle with the drawer.
/// </summary>
public void ExecuteVerb() {
double scale = this.scale.ToDouble();
drawer.DrawCircle(scale);
}
@@ -29,7 +32,7 @@
/// </summary>
/// <returns></returns>
public string GetDescription() {
return "Draws a circle radius " + scale.ToString() + ", with origin of the pen";
return "Draws a circle with a certain radius, with origin of the pen";
}
}
}
@@ -1,4 +1,5 @@
using System.Drawing;
using Advanced_Software_Engineering.Verbs.Value;
using System.Drawing;
namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
@@ -7,7 +8,8 @@ namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
/// </summary>
public class DrawTo : IVerb {
private readonly Drawer drawer;
private readonly Point moveToPoint;
private readonly IValue moveToPointX;
private readonly IValue moveToPointY;
/// <summary>
/// Creates the DrawTo intstance
@@ -15,16 +17,17 @@ namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
/// <param name="drawer">drawer</param>
/// <param name="x">X position to move to</param>
/// <param name="y">Y position to move to</param>
public DrawTo(Drawer drawer, int x, int y) {
public DrawTo(Drawer drawer, IValue x, IValue y) {
this.drawer = drawer;
moveToPoint = new Point(x, y);
moveToPointX = x;
moveToPointY = y;
}
/// <summary>
/// Draws the line
/// </summary>
public void ExecuteVerb() {
drawer.DrawLine(moveToPoint);
drawer.DrawLine(new Point(moveToPointX.ToInt(), moveToPointY.ToInt()));
}
/// <summary>
@@ -32,7 +35,7 @@ namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
/// </summary>
/// <returns></returns>
public string GetDescription() {
return "Draw line " + moveToPoint.X + ", " + moveToPoint.Y;
return "Draws a line";
}
}
}
@@ -1,4 +1,5 @@
using System.Drawing;
using Advanced_Software_Engineering.Verbs.Value;
using System.Drawing;
namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
@@ -7,14 +8,14 @@ namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
/// </summary>
public class FillColor : IVerb {
private readonly Drawer drawer;
private readonly Color color;
private readonly IValue color;
/// <summary>
/// Create the FillColor instance
/// </summary>
/// <param name="drawer">drawer</param>
/// <param name="color">The color of the fill</param>
public FillColor(Drawer drawer, Color color) {
public FillColor(Drawer drawer, IValue color) {
this.drawer = drawer;
this.color = color;
}
@@ -23,7 +24,7 @@ namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
/// Set the fill color
/// </summary>
public void ExecuteVerb() {
drawer.SetFillColor(color);
drawer.SetFillColor(color.ToColor());
}
/// <summary>
@@ -31,7 +32,7 @@ namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
/// </summary>
/// <returns>Description of the fill color</returns>
public string GetDescription() {
return "Set fill color to: " + color.ToString();
return "Set fill color to the specified color";
}
}
}
@@ -1,4 +1,5 @@
using System.Drawing;
using Advanced_Software_Engineering.Verbs.Value;
using System.Drawing;
namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
@@ -7,14 +8,14 @@ namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
/// </summary>
public class PenColor : IVerb {
private readonly Drawer drawer;
private readonly Color color;
private readonly IValue color;
/// <summary>
/// Creates a PenColor instance
/// </summary>
/// <param name="drawer">drawer</param>
/// <param name="color">Pen color</param>
public PenColor(Drawer drawer, Color color) {
public PenColor(Drawer drawer, IValue color) {
this.drawer = drawer;
this.color = color;
}
@@ -23,7 +24,7 @@ namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
/// Sets the pen color
/// </summary>
public void ExecuteVerb() {
drawer.SetPenColor(color);
drawer.SetPenColor(color.ToColor());
}
/// <summary>
@@ -31,7 +32,7 @@ namespace Advanced_Software_Engineering.Verbs.DrawingVerbs {
/// </summary>
/// <returns>The description pen color</returns>
public string GetDescription() {
return "Set pen color to: " + color.ToString();
return "Set pen color";
}
}
}
@@ -10,14 +10,14 @@ namespace Advanced_Software_Engineering.Verbs.Value {
private const int DIVIDE = 3;
private Drawer drawer;
private string variable1;
private string variable2;
private IValue variable1;
private IValue variable2;
private int operation;
private bool evaluated = false;
private IValue evaluatedValue;
public ExpressionValue(Drawer drawer, string variable1, string variable2, int operation) {
public ExpressionValue(Drawer drawer, IValue variable1, IValue variable2, int operation) {
this.drawer = drawer;
this.variable1 = variable1;
this.variable2 = variable2;
@@ -30,35 +30,33 @@ namespace Advanced_Software_Engineering.Verbs.Value {
private IValue Evaluate() {
if (evaluated) return evaluatedValue;
IValue value1 = drawer.GetVariable(variable1);
IValue value2 = drawer.GetVariable(variable2);
string value1type = value1.GetOriginalType();
string outputType = variable1.GetOriginalType();
switch (value1type) {
switch (outputType) {
case "bool":
//operations as standard for bool expressions eg
// (a + b) => a or b, (a * b) => a and b, (a ^ b) => a xor b
if (operation == ADD) evaluatedValue = ValueFactory.CreateValue(value1.ToBool() || value2.ToBool());
else if (operation == SUBTRACT) evaluatedValue = ValueFactory.CreateValue(value1.ToBool() ^ value2.ToBool());
else if (operation == MULTIPLY) evaluatedValue = ValueFactory.CreateValue(value1.ToBool() && value2.ToBool());
if (operation == ADD) evaluatedValue = ValueFactory.CreateValue(variable1.ToBool() || variable1.ToBool());
else if (operation == SUBTRACT) evaluatedValue = ValueFactory.CreateValue(variable1.ToBool() ^ variable1.ToBool());
else if (operation == MULTIPLY) evaluatedValue = ValueFactory.CreateValue(variable1.ToBool() && variable1.ToBool());
else if (operation == DIVIDE) throw new Exception("Bool cannot be divided");
else throw new Exception("Bad operation");
break;
case "int":
if (operation == ADD) evaluatedValue = ValueFactory.CreateValue(value1.ToInt() + value2.ToInt());
else if (operation == SUBTRACT) evaluatedValue = ValueFactory.CreateValue(value1.ToInt() - value2.ToInt());
else if (operation == MULTIPLY) evaluatedValue = ValueFactory.CreateValue(value1.ToInt() * value2.ToInt());
else if (operation == DIVIDE) evaluatedValue = ValueFactory.CreateValue(value1.ToInt() / value2.ToInt());
if (operation == ADD) evaluatedValue = ValueFactory.CreateValue(variable1.ToInt() + variable1.ToInt());
else if (operation == SUBTRACT) evaluatedValue = ValueFactory.CreateValue(variable1.ToInt() - variable1.ToInt());
else if (operation == MULTIPLY) evaluatedValue = ValueFactory.CreateValue(variable1.ToInt() * variable1.ToInt());
else if (operation == DIVIDE) evaluatedValue = ValueFactory.CreateValue(variable1.ToInt() / variable1.ToInt());
else throw new Exception("Bad operation");
break;
case "double":
if (operation == ADD) evaluatedValue = ValueFactory.CreateValue(value1.ToDouble() + value2.ToDouble());
else if (operation == SUBTRACT) evaluatedValue = ValueFactory.CreateValue(value1.ToDouble() - value2.ToDouble());
else if (operation == MULTIPLY) evaluatedValue = ValueFactory.CreateValue(value1.ToDouble() * value2.ToDouble());
else if (operation == DIVIDE) evaluatedValue = ValueFactory.CreateValue(value1.ToDouble() / value2.ToDouble());
if (operation == ADD) evaluatedValue = ValueFactory.CreateValue(variable1.ToDouble() + variable1.ToDouble());
else if (operation == SUBTRACT) evaluatedValue = ValueFactory.CreateValue(variable1.ToDouble() - variable1.ToDouble());
else if (operation == MULTIPLY) evaluatedValue = ValueFactory.CreateValue(variable1.ToDouble() * variable1.ToDouble());
else if (operation == DIVIDE) evaluatedValue = ValueFactory.CreateValue(variable1.ToDouble() / variable1.ToDouble());
else throw new Exception("Bad operation");
break;