You are on page 1of 5

To calculate the area and perimeter of regular polygon using

OOP and Factory Design Pattern IN C#

public interface IRegularPolygon


{
int NumberOfSides { get; set; }
double SideLength { get; set; }
double getPerimeter();
double getArea();

}
// you can also use abstract class the code is as follows
public abstract class RegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
public double getArea();
}

//The Square class that implement IRegularPolygon interface


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RegularPolygon.BL
{
public class Square : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public Square(double SideLength)
{
this.NumberOfSides=4;
this.SideLength = SideLength;
}
/// <summary>
/// Get the Perimeter of the square
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of a square
/// </summary>
/// <returns></returns>
public double getArea()
{
return SideLength * SideLength;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(),
"Perimeter Of Sqaure :" + getPerimeter());
}

}
}

//The EquilateralTriangle class that implement IRegularPolygon interface


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RegularPolygon.BL
{
public class EquilateralTriangle : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public EquilateralTriangle(double SideLength)
{
this.NumberOfSides=3;
this.SideLength = SideLength;
}
/// <summary>
/// Get the Perimeter of EquilateralTriangle
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of EquilateralTriangle
/// </summary>
/// <returns></returns>
public double getArea()
{
return SideLength * SideLength * Math.Sqrt(3) / 4;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(),
"Perimeter Of Sqaure :" + getPerimeter());
}

}
}

//The RegularPentagon class that implement IRegularPolygon interface


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RegularPolygon.BL
{
public class RegularPentagon : IRegularPolygon
{
public int NumberOfSides { get; set; }
public double SideLength { get; set; }
public double Apothem { get; set; }

public RegularPentagon(double SideLength, double Apothem)


{
this.NumberOfSides = 5;
this.SideLength = SideLength;
this.Apothem = Apothem;
}
/// <summary>
/// Get the Perimeter of RegularPentagon
/// </summary>
/// <returns></returns>
public double getPerimeter()
{
return NumberOfSides * SideLength;
}
/// <summary>
/// Get the area of RegularPentagon
/// </summary>
/// <returns></returns>
public double getArea()
{
return 2.5 * SideLength * Apothem;
}
public override string ToString()
{
return String.Format("{0}-{1}", "Area of Sqare:" + getArea(),
"Perimeter Of Sqaure :" + getPerimeter());
}

}
}
// RegPolygonFactory class is a factory class that instantiate object based on
shape
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RegularPolygon.BL
{
public class RegPolygonFactory
{
/// <summary>
/// Create instance of an object based on the type of shape
/// </summary>
/// <param name="shapeType"></param>
/// <param name="side"></param>
/// <returns></returns>
public IRegularPolygon getShape(string shapeType, double sideLength)
{
return getShape(shapeType, sideLength, 0.0);
}
public IRegularPolygon getShape(string shapeType, double sideLength,
double apothem)
{
if (shapeType == null) throw new ArgumentNullException("shapeType");
if (sideLength < 0.0) throw new
ArgumentOutOfRangeException(sideLength.ToString());
if (apothem < 0.0) throw new
ArgumentOutOfRangeException(apothem.ToString());
else if (shapeType.Equals("SQUARE"))
{
return new Square(sideLength);
}
else if (shapeType.Equals("EQUILATERALTRIANGLE"))
{
return new EquilateralTriangle(sideLength);
}
//shapeType.equalsIgnoreCase("REGULARPENTAGON")
else if (shapeType.Equals("REGULARPENTAGON"))
{
return new RegularPentagon(sideLength, apothem);
}
return null;
}

}
}

//For unit Test: To test for instance square


using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RegularPolygon.BL;

namespace RegularPolygonTest.BL
{
[TestClass]
public class SquareTest
{
RegPolygonFactory regularPolygon = new RegPolygonFactory();

/// <summary>
/// Test the Area Of Square with valid input(side=4)
/// </summary>
[TestMethod]
public void getAreaOfSquare_ValidArgument()
{
//Arrange
double sideLength = 4;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * sideLength;
//Act
var actual = square.getArea();

//Assert
Assert.AreEqual(expected, actual);
}
/// <summary>
/// Test the Perimeter Of Square with valid input(side=6, edges=4)
/// </summary>
[TestMethod]
public void getPerimeterOfSquare_ValidAregument()
{
//Arrange
double sideLength = 6;
double NumberOfSides = 4;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * NumberOfSides;
//Act
var actual = square.getPerimeter();

//Assert
Assert.AreEqual(expected, actual);
}
/// <summary>
/// Test the Perimeter Of Square with invalid input(edges=6)
/// </summary>
[TestMethod]
public void getPerimeterOfSquare_InValidPostiveAregument()
{
//Arrange
double sideLength = 5;
double NumberOfSides = 6;
var square = regularPolygon.getShape("SQUARE", sideLength);
var expected = sideLength * NumberOfSides;
//Act
var actual = square.getPerimeter();

//Assert
Assert.AreEqual(expected, actual);
}
}
}

You might also like