Last active
November 9, 2020 20:35
-
-
Save antklim/52faa9d430236a7d0f0293d05a0a4c3f 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
enum Operation { addition, subtraction, division, multiplication, sqrt } | |
enum Operand { A, B } | |
class CalculatorUseCase { | |
num memory; | |
Operation operation; | |
num operandA; | |
num operandB; | |
CalculatorUseCase( | |
{this.operation = Operation.addition, | |
this.operandA = 0, | |
this.operandB = 0}); | |
@override | |
String toString() => | |
'operation: $operation, A: $operandA, B: $operandB, value: $value'; | |
num get value { | |
switch (operation) { | |
case Operation.addition: | |
return operandA + operandB; | |
case Operation.subtraction: | |
... | |
default: | |
return 0; | |
} | |
} | |
String get format { | |
switch (operation) { | |
case Operation.addition: | |
return '$operandA + $operandB'; | |
case Operation.subtraction: | |
... | |
default: | |
return ''; | |
} | |
} | |
void setOperation(Operation value) { | |
operation = value; | |
} | |
num operandValue(Operand operand) => | |
(operand == Operand.A) ? operandA : operandB; | |
void _setOperandA(num v) => operandA = v; | |
void _setOperandB(num v) => operandB = v; | |
void Function(num) setOperand(Operand operand) => | |
(operand == Operand.A) ? _setOperandA : _setOperandB; | |
void fromMemory(Operand operand) { | |
if (memory == null) return; | |
setOperand(operand)(memory); | |
} | |
void memorise() { | |
memory = value; | |
} | |
void resetMemory() { | |
memory = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment