コード例 #1
0
                private static bool Equals(AmountSigned left, AmountSigned right)
                {
                    if (left is null && right is null)
                    {
                        return(true);
                    }


                    if (left?.GetType() != right?.GetType())
                    {
                        return(false);
                    }

                    if (left.GetType() != typeof(AmountSigned) || right.GetType() != typeof(AmountSigned))
                    {
                        return(false);
                    }

                    return(left.ToString().Equals(right.ToString()));
                }
コード例 #2
0
            private AmountSigned ConvertTo(AmountSigned amountSigned, string convertTo)
            {
                // Контроль типов: нельзя конвертировать валюту в саму себя
                if (amountSigned.Amount.Currency == convertTo)
                {
                    return(amountSigned);
                }

                var cost = _conversionsCost[amountSigned.Amount.Currency][convertTo];

                return(new AmountSigned
                {
                    Sign = amountSigned.Sign,
                    Amount = new Amount
                    {
                        Currency = convertTo,
                        Value = amountSigned.Amount.Value * cost
                    }
                });
            }
コード例 #3
0
            public override object VisitExpression(CurrencyComputerParser.ExpressionContext context)
            {
                var amounts       = context.amountComposite();
                var left          = (AmountSigned)VisitAmountComposite(amounts[0]);
                var leftConverted = ConvertTo(left, _targetCurrency);

                if (leftConverted != left)
                {
                    _logger?.LogDebug("{OperationNumber}:Converted from {Source} to {Dest}.", _operationsLogger++, left, leftConverted);
                }

                var right = amounts.Length == 2
                    ? (AmountSigned)VisitAmountComposite(amounts[1])
                    : (AmountSigned)VisitExpression(context.expression());
                var rightConverted = ConvertTo(right, _targetCurrency);

                if (rightConverted != right)
                {
                    _logger?.LogDebug("{OperationNumber}:Converted from {Source} to {Dest}.", _operationsLogger++, right, rightConverted);
                }


                var operationKey   = $"{leftConverted.Sign}{rightConverted.Sign}";
                var resultComputed = Operations[operationKey](leftConverted.Amount.Value, rightConverted.Amount.Value);

                var result = new AmountSigned
                {
                    Sign = resultComputed < 0
                        ? "-"
                        : "+",
                    Amount = new Amount
                    {
                        Currency = _targetCurrency,
                        Value    = Math.Abs(resultComputed)
                    }
                };

                _logger?.LogDebug("{OperationNumber}:Result {Result} from left {Left} and right {Right} tokens.", _operationsLogger++, result, leftConverted, rightConverted);

                return(result);
            }