A special type used to indicate an accounting balance.
상속: IFormattable
예제 #1
0
        public void AddDebitCreditBalancesSecondLargerTest()
        {
            Balance b1 = new Balance(50M, BalanceType.Debit);
            Balance b2 = new Balance(100M, BalanceType.Credit);

            Balance b3 = b1 + b2;
            Assert.AreEqual(b3.Magnitude, 50M);
            Assert.AreEqual(b3.BalanceType, BalanceType.Credit);
        }
예제 #2
0
        public void AddTwoDebitBalancesTest()
        {
            Balance b1 = new Balance(100M, BalanceType.Debit);
            Balance b2 = new Balance(50M, BalanceType.Debit);

            Balance b3 = b1 + b2;
            Assert.AreEqual(b3.Magnitude, 150M);
            Assert.AreEqual(b3.BalanceType, BalanceType.Debit);
        }
예제 #3
0
        public void EqualsFalseTest()
        {
            Balance b1 = new Balance(100M, BalanceType.Debit);
            Balance b2 = new Balance(100M, BalanceType.Credit);

            bool result = b1 == b2;
            Assert.IsFalse(result);

            Balance b3 = new Balance(100M, BalanceType.Credit);
            Balance b4 = new Balance(50M, BalanceType.Credit);

            result = b3 == b4;
            Assert.IsFalse(result);
        }
예제 #4
0
        public void EqualsMethodTest()
        {
            Balance b1 = new Balance(100M, BalanceType.Debit);
            Balance b2 = new Balance(100M, BalanceType.Debit);

            bool result = b1.Equals(b2);
            Assert.IsTrue(result);
        }
예제 #5
0
        public void SubtractTwoCreditBalancesTest()
        {
            Balance b1 = new Balance(100M, BalanceType.Credit);
            Balance b2 = new Balance(50M, BalanceType.Credit);

            Balance b3 = b1 - b2;
            Assert.AreEqual(b3.Magnitude, 50M);
            Assert.AreEqual(b3.BalanceType, BalanceType.Credit);
        }
예제 #6
0
        public void NotEqualsTrueTest()
        {
            Balance b1 = new Balance(100M, BalanceType.Debit);
            Balance b2 = new Balance(100M, BalanceType.Credit);

            bool result = b1 != b2;
            Assert.IsTrue(result);

            Balance b3 = new Balance(100M, BalanceType.Credit);
            Balance b4 = new Balance(50M, BalanceType.Credit);

            result = b3 != b4;
            Assert.IsTrue(result);
        }
예제 #7
0
        /// <summary>
        /// Adds two balances.
        /// </summary>
        /// <param name="lhs">The left-hand side.</param>
        /// <param name="rhs">The right-hand side.</param>
        /// <returns>A resulting balance.</returns>
        public static Balance operator +(Balance lhs, Balance rhs)
        {
            Balance result = new Balance();

            if (lhs.BalanceType == rhs.BalanceType) {
                result.Magnitude = lhs.Magnitude + rhs.Magnitude;
                result.BalanceType = lhs.BalanceType;
            }
            else {
                if (lhs.Magnitude >= rhs.Magnitude) {
                    result.Magnitude = lhs.Magnitude - rhs.Magnitude;
                    result.BalanceType = lhs.BalanceType;
                }
                else {
                    result.Magnitude = rhs.Magnitude - lhs.Magnitude;
                    result.BalanceType = rhs.BalanceType;
                }
            }

            return result;
        }
예제 #8
0
        /// <summary>
        /// Attempts to parse a Balance given a string and the type of balance expected.
        /// </summary>
        /// <param name="balanceText">The text to attempt to parse.</param>
        /// <param name="expectedBalanceType">The type of balance expected. If null, 'CR' or 'DR' must be specified in the balance text.</param>
        /// <param name="resultBalance">If parsing is sucessful, this will contain the parsed balance.</param>
        /// <returns>True if the text could be parsed, otherwise false.</returns>
        public static bool TryParse(string balanceText, BalanceType? expectedBalanceType, out Balance resultBalance)
        {
            bool result = true;

            decimal magnitude = 0M;
            BalanceType type = BalanceType.Credit;

            balanceText = (balanceText ?? string.Empty).Trim().ToUpper();

            // What type of balance is it?
            if (balanceText.EndsWith("CR")) {
                type = BalanceType.Credit;
                balanceText = balanceText.Substring(0, balanceText.Length - 2);
            } else if (balanceText.EndsWith("DR")) {
                type = BalanceType.Debit;
                balanceText = balanceText.Substring(0, balanceText.Length - 2);
            } else {
                if (expectedBalanceType != null && expectedBalanceType.HasValue) {
                    type = expectedBalanceType.Value;
                } else {
                    result = false;
                }
            }

            // Get the value (magnitude) of the balance
            if (result == true) {
                result = decimal.TryParse(balanceText.Trim(), NumberStyles.Any, CultureInfo.CurrentCulture, out magnitude);
                magnitude = Math.Abs(magnitude);
            }

            resultBalance = new Balance(magnitude, type);
            return result;
        }
예제 #9
0
 /// <summary>
 /// Attempts to parse a Balance given a string.
 /// </summary>
 /// <param name="balanceText">The text to attempt to parse.</param>
 /// <param name="resultBalance">If parsing is sucessful, this will contain the parsed balance.</param>
 /// <returns>True if the text could be parsed, otherwise false.</returns>
 public static bool TryParse(string balanceText, out Balance resultBalance)
 {
     return TryParse(balanceText, null, out resultBalance);
 }