Created
October 12, 2018 16:26
-
-
Save sevperez/8f1241d925c040c75b5df288b0256813 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace isp_3 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var basicStudent = new BasicMathStudent(new BasicCalculator()); | |
Console.WriteLine(basicStudent.Calculate("add", 3, 4)); | |
Console.WriteLine(basicStudent.Calculate("subtract", 3, 4)); | |
Console.WriteLine(basicStudent.Calculate("multiply", 3, 4)); | |
Console.WriteLine(basicStudent.Calculate("divide", 3, 4)); | |
Console.WriteLine("---------------------------"); | |
var advancedStudent = new AdvancedMathStudent(new AdvancedCalculator()); | |
Console.WriteLine(advancedStudent.Calculate("add", 3, 4)); | |
Console.WriteLine(advancedStudent.Calculate("subtract", 3, 4)); | |
Console.WriteLine(advancedStudent.Calculate("multiply", 3, 4)); | |
Console.WriteLine(advancedStudent.Calculate("divide", 3, 4)); | |
Console.WriteLine(advancedStudent.Calculate("power", 3, 4)); | |
Console.WriteLine(advancedStudent.Calculate("squareroot", 25)); | |
} | |
} | |
public interface IArithmetic | |
{ | |
int Add(int num1, int num2); | |
int Subtract(int num1, int num2); | |
int Multiply(int num1, int num2); | |
double Divide(int num1, int num2); | |
} | |
public interface IExponents | |
{ | |
double Power(double num, double power); | |
double SquareRoot(double num); | |
} | |
public abstract class Calculator | |
{ | |
public abstract double execute(string operation, int operand1, int operand2); | |
public virtual double execute(string operation, int number) | |
{ | |
throw new ArgumentException(); | |
} | |
} | |
public class BasicCalculator : Calculator, IArithmetic | |
{ | |
public override double execute(string operation, int operand1, int operand2) | |
{ | |
switch (operation.ToLower()) | |
{ | |
case "add": | |
return this.Add(operand1, operand2); | |
case "subtract": | |
return this.Subtract(operand1, operand2); | |
case "multiply": | |
return this.Multiply(operand1, operand2); | |
case "divide": | |
return this.Divide(operand1, operand2); | |
default: | |
throw new ArgumentException(); | |
} | |
} | |
public int Add(int num1, int num2) | |
{ | |
return num1 + num2; | |
} | |
public int Subtract(int num1, int num2) | |
{ | |
return num1 - num2; | |
} | |
public int Multiply(int num1, int num2) | |
{ | |
return num1 * num2; | |
} | |
public double Divide(int num1, int num2) | |
{ | |
return (double)num1 / (double)num2; | |
} | |
} | |
public class AdvancedCalculator : Calculator, IArithmetic, IExponents | |
{ | |
public override double execute(string operation, int operand1, int operand2) | |
{ | |
switch (operation.ToLower()) | |
{ | |
case "add": | |
return this.Add(operand1, operand2); | |
case "subtract": | |
return this.Subtract(operand1, operand2); | |
case "multiply": | |
return this.Multiply(operand1, operand2); | |
case "divide": | |
return this.Divide(operand1, operand2); | |
case "power": | |
return this.Power(operand1, operand2); | |
default: | |
throw new ArgumentException(); | |
} | |
} | |
public override double execute(string operation, int number) | |
{ | |
if (operation.ToLower() == "squareroot") | |
{ | |
return this.SquareRoot(number); | |
} | |
else | |
{ | |
throw new ArgumentException(); | |
} | |
} | |
public int Add(int num1, int num2) | |
{ | |
return num1 + num2; | |
} | |
public int Subtract(int num1, int num2) | |
{ | |
return num1 - num2; | |
} | |
public int Multiply(int num1, int num2) | |
{ | |
return num1 * num2; | |
} | |
public double Divide(int num1, int num2) | |
{ | |
return (double)num1 / (double)num2; | |
} | |
public double Power(double num, double power) | |
{ | |
return Math.Pow(num, power); | |
} | |
public double SquareRoot(double num) | |
{ | |
return Math.Sqrt(num); | |
} | |
} | |
public abstract class MathStudent | |
{ | |
public Calculator Calculator; | |
public abstract double Calculate(string operation, int operand1, int operand2); | |
public virtual double Calculate(string operation, int number) | |
{ | |
throw new ArgumentException(); | |
} | |
} | |
public class BasicMathStudent : MathStudent | |
{ | |
public BasicMathStudent(Calculator calculator) | |
{ | |
this.Calculator = calculator; | |
} | |
public override double Calculate(string operation, int operand1, int operand2) | |
{ | |
return this.Calculator.execute(operation, operand1, operand2); | |
} | |
} | |
public class AdvancedMathStudent : MathStudent | |
{ | |
public AdvancedMathStudent(Calculator calculator) | |
{ | |
this.Calculator = calculator; | |
} | |
public override double Calculate(string operation, int number) | |
{ | |
return this.Calculator.execute(operation, number); | |
} | |
public override double Calculate(string operation, int operand1, int operand2) | |
{ | |
return this.Calculator.execute(operation, operand1, operand2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment