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