/
CalculateAction.cs
74 lines (63 loc) · 1.96 KB
/
CalculateAction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace RPN
{
public class CalculateAction : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
CalculationStack stack;
CalculationMode mode;
public CalculateAction(CalculationStack stack, CalculationMode mode) {
this.stack = stack;
this.mode = mode;
}
public event EventHandler CanExecuteChanged;
/// <summary>
/// Wszystkie przyciski obsługiwane są za pomocą tego samego handlera, tylko z różnymi prametrami
/// w zależności od typu i wartości paramteru wykonywane będą inne operacje
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
{
handleCommands(parameter);
handleComma(parameter);
handleNumbers(parameter);
}
private void handleNumbers(object parameter){
int number = 0;
if (int.TryParse(parameter.ToString(), out number))
stack.Append(number);
}
private void handleComma(object parameter)
{
if(parameter.ToString().Equals("."))
stack.SetComma();
}
private void handleCommands(object parameter)
{
if (parameter is OperationType)
{
OperationType type = (OperationType)parameter;
Operation o = OperationBuilder.Build(stack, mode, type);
if (o != null)
o.Execute();
}
}
public void RaiseCanExecuteChanged()
{
var handler = CanExecuteChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
}