Created
October 9, 2018 17:54
-
-
Save sevperez/1dc0746489c9c5c631223f8111c63c2b to your computer and use it in GitHub Desktop.
This file contains hidden or 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_2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var basicStudent = new BasicMathStudent(); | |
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(); | |
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)); | |
} | |
} | |
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); | |
} | |
interface IExponents | |
{ | |
double Power(double num, double power); | |
double SquareRoot(double num); | |
} | |
class BasicCalculator : IArithmetic | |
{ | |
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; | |
} | |
} | |
class AdvancedCalculator : IArithmetic, IExponents | |
{ | |
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); | |
} | |
} | |
class BasicMathStudent | |
{ | |
private BasicCalculator Calculator; | |
public BasicMathStudent() | |
{ | |
this.Calculator = new BasicCalculator(); | |
} | |
public double Calculate(string operation, int operand1, int operand2) | |
{ | |
switch (operation.ToLower()) | |
{ | |
case "add": | |
return this.Calculator.Add(operand1, operand2); | |
case "subtract": | |
return this.Calculator.Subtract(operand1, operand2); | |
case "multiply": | |
return this.Calculator.Multiply(operand1, operand2); | |
case "divide": | |
return this.Calculator.Divide(operand1, operand2); | |
default: | |
throw new ArgumentException(); | |
} | |
} | |
} | |
class AdvancedMathStudent | |
{ | |
private AdvancedCalculator Calculator; | |
public AdvancedMathStudent() | |
{ | |
this.Calculator = new AdvancedCalculator(); | |
} | |
public double Calculate(string operation, int number) | |
{ | |
if (operation.ToLower() == "squareroot") | |
{ | |
return this.Calculator.SquareRoot(number); | |
} | |
else | |
{ | |
throw new ArgumentException(); | |
} | |
} | |
public double Calculate(string operation, int operand1, int operand2) | |
{ | |
switch (operation.ToLower()) | |
{ | |
case "add": | |
return this.Calculator.Add(operand1, operand2); | |
case "subtract": | |
return this.Calculator.Subtract(operand1, operand2); | |
case "multiply": | |
return this.Calculator.Multiply(operand1, operand2); | |
case "divide": | |
return this.Calculator.Divide(operand1, operand2); | |
case "power": | |
return this.Calculator.Power(operand1, operand2); | |
default: | |
throw new ArgumentException(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment