summaryrefslogtreecommitdiff
path: root/DSACore/Auxiliary/Calculator/Operator.cs
blob: 440e21e222cd3ee16d0c2bf77ff2f271ca4cd56e (plain)
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
using System;
using DSACorev.Auxiliary.Calculator;

namespace DSACore.Auxiliary.Calculator
{
    /// <summary>
    /// The Operator Class represents a binary operator with tow Arguments and an Operation type
    /// </summary>
    public class Operator : ISolvable
    {
        private readonly ISolvable arg1, arg2;

        public Operator(ISolvable arg1, ISolvable arg2, Ops operatorType)
        {
            this.arg1 = arg1;
            this.arg2 = arg2;
            this.OperatorType = operatorType;
        }

        public Ops OperatorType { get; set; }

        public int Solve()
        {
            int result;
            switch (this.OperatorType)
            {
                case Ops.Dice:
                    result = Dice.Roll(this.arg1.Solve(), this.arg2.Solve());
                    break;
                case Ops.Multiply:
                    result = this.arg1.Solve() * this.arg2.Solve();
                    break;
                case Ops.Add:
                    result = this.arg1.Solve() + this.arg2.Solve();
                    break;
                case Ops.Subtract:
                    result = this.arg1.Solve() - this.arg2.Solve();
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return result;
        }

        public override string ToString()
        {
            return $"({this.arg1} {this.OperatorType} {this.arg2})";
        }
    }
}