Created
February 24, 2022 22:36
-
-
Save flaviorl-net/afcb97535e79693e824c2145640dad52 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
public class CalculadoraStrategy | |
{ | |
public enum Operacao | |
{ | |
None, | |
Sum, | |
Sub, | |
Mult, | |
Div | |
} | |
private readonly ContextStrategy<int, StrcNumeros, Operacao> _contextStrategy = new ContextStrategy<int, StrcNumeros, Operacao>(); | |
public CalculadoraStrategy(string operacao) | |
{ | |
Operacao op = Operacao.None; | |
try | |
{ | |
op = (Operacao)Enum.Parse(typeof(Operacao), operacao, true); | |
} | |
catch (ArgumentException) | |
{ | |
Console.WriteLine("Operação inválida!"); | |
return; | |
} | |
_contextStrategy.AddList(Operacao.Sum, new Sum()); | |
_contextStrategy.AddList(Operacao.Sub, new Sub()); | |
_contextStrategy.AddList(Operacao.Mult, new Mult()); | |
_contextStrategy.AddList(Operacao.Div, new Div()); | |
SelecionarCalculo(op); | |
} | |
private void SelecionarCalculo(Operacao operacao) | |
{ | |
_contextStrategy.SetStrategy(operacao); | |
} | |
public int Calcular(int numero1, int numero2) | |
{ | |
return _contextStrategy.Execute(new StrcNumeros() { Numero1 = numero1, Numero2 = numero2 }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment