using System.ServiceModel;

using MathServiceInterface;

using MathServiceInterface.DTOs;

using MathServiceInterface.Enumerations;

using MathServiceInterface.Faults;

namespace MathServiceLibrary

{

public class MathService : IMathService

{

public double PerformBinaryOperation(BinaryOperation binaryOperation)

{

double result = 0;

switch (binaryOperation.Operation)

{

case BinaryOpEnum.Add:

result = binaryOperation.Operand1 + binaryOperation.Operand2;

break;

case BinaryOpEnum.Subtract:

result = binaryOperation.Operand1 - binaryOperation.Operand2;

break;

case BinaryOpEnum.Multiply:

result = binaryOperation.Operand1 * binaryOperation.Operand2;

break;

case BinaryOpEnum.Divide:

if (binaryOperation.Operand2 == 0)

{

throw new FaultException<BinaryOperationFault>(

new BinaryOperationFault(binaryOperation.Operation,

"Divide By Zero"));

}

result = binaryOperation.Operand1 / binaryOperation.Operand2;

break;

default:

throw new FaultException<BinaryOperationFault>(

new BinaryOperationFault(binaryOperation.Operation,

"Undefined Operation Specified"));

}

if (double.IsNaN(result) || double.IsInfinity(result))

{

throw new FaultException<BinaryOperationFault>(

new BinaryOperationFault(binaryOperation.Operation,

"Arithmetic Overflow"));

}

return result;

}

public double CalculateSlope(TwoDimensionalPoint point1, TwoDimensionalPoint point2)

{

if (point1.Equals(point2))

{

throw new FaultException<SlopeCalculationFault>(

new SlopeCalculationFault(

"Two distinct points must be provided"));

}

double deltaY = point2.Y - point1.Y;

double deltaX = point2.X - point1.X;

if (deltaX == 0)

{

throw new FaultException<SlopeCalculationFault>(

new SlopeCalculationFault("Undefined Slope"));

}

double slope = deltaY / deltaX;

if (double.IsNaN(slope) || double.IsInfinity(slope))

{

throw new FaultException<SlopeCalculationFault>(

new SlopeCalculationFault(

"Slope Calculation Resulted in Arithmetic Overflow"));

}

return slope;

}

}

}