示例#1
0
文件: Program.cs 项目: Alvek/Braincad
        static void Main(string[] args)
        {
            // 10) declare 2 objects
            Money m1 = new Money(10, CurrencyTypes.EU);
            Money m2 = new Money(20, CurrencyTypes.EU);
            // 11) do operations
            // add 2 objects of Money
            Money m3 = m1 + m2;

            // add 1st object of Money and double
            m3 = m1 + 3;
            // decrease 2nd object of Money by 1 
            m2--;
            // increase 1st object of Money
            m1++;
            // compare 2 objects of Money
            if (m1 < m2)
            {
            }
            // compare 2nd object of Money and string
            if (m2 < "23")
            {
            }

            // check CurrencyType of every object
            if (m1)
            {
            }
            if (m2)
            {
            }

            // convert 1st object of Money to string
            string money = (string)m1;
        }
示例#2
0
        static void Main(string[] args) {
            // 10) declare 2 objects
            Money m1 = new Money(2.3);
            Money m2 = new Money(3.5M);

            // 11) do operations
            // add 2 objects of Money
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("Adding m1:" + m1 + " and m2:" + m2);
            Money mSum = m1 + m2;
            Console.WriteLine(mSum.ToString());

            Money m3 = new Money(4.0M, CurrencyTypes.USD);
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("Adding m1:" + m1 + " and m3:" + m3);
            try {
                Money res = m1 + m3;
                Console.WriteLine(m3.ToString());
            }
            catch (DifferentCurrencyTypesException) {
                Console.WriteLine("Ok. We're expecting for an exception.");
            }

            // add 1st object of Money and double
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("Adding m1:" + m1 + " to 3.2");
            mSum = m1 + 3.2;
            Console.WriteLine(mSum.ToString());

            // decrease 2nd object of Money by 1 
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("m2 before decreasing: " + m2.ToString());
            Console.WriteLine("m2--");
            Console.WriteLine(m2--);
            Console.WriteLine("--m2");
            Console.WriteLine(--m2);

            // increase 1st object of Money
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("m1 before increasing: " + m1.ToString());
            Console.WriteLine("m1*3");
            Console.WriteLine(m1 * 3);

            // compare 2 objects of Money
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("Comparing: " + m1 + ">" + m2);
            Console.WriteLine(m1 > m2);
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("Comparing: " + m1 + "<" + m2);
            Console.WriteLine(m1 < m2);
            // compare 2nd object of Money and string
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("Comparing: " + m1 + ">" + (Money)"2.5" + " (string)");
            Console.WriteLine(m1 > (Money)"2.5");

            // check CurrencyType of every object
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("Checking CurrencyType of m1:" + m1);
            Console.WriteLine(m1.CurrencyType);

            Console.WriteLine("Checking CurrencyType of m2:" + m2);
            Console.WriteLine(m2.CurrencyType);

            Console.WriteLine("Checking CurrencyType of m3:" + m3);
            Console.WriteLine(m3.CurrencyType);

            // testing true and false operators
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("m1:" + m1 + " is True?");
            if (m1)
                Console.WriteLine("Yes");
            else
                Console.WriteLine("No");

            Money m4 = new Money(0M);

            Console.WriteLine("m4:" + m4 + " is False?");
            if (m4)
                Console.WriteLine("No");
            else
                Console.WriteLine("Yes");

            // convert 1st object of Money to string
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("Converting m1:" + m1 + " to string value:");
            Console.WriteLine((string)m1);

        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Program, that simulates money operations");
            Console.WriteLine(new string('*', 70));

            // 10) declare 2 objects
            Money money1 = new Money(CurrencyTypes.UAH, 400);
            Money money2 = new Money(CurrencyTypes.UAH, 300);

            // 11) do operations
            // add 2 objects of Money
            if (money1 == money2)
            {
                Console.WriteLine("money1 + money2: {1} {0} + {2} {0} = {3} {0}", money1.CurrencyType, money1.Amount, money2.Amount, (money1 + money2).Amount);
            }

            // add 1st object of Money and double
            double d = 150.10d;
            Console.WriteLine("money1 + double: {1} {0} +  {2} = {3} {0}", money1.CurrencyType, money1.Amount,  d, (money1 + d).Amount);

            // decrease 2nd object of Money by 1
            --money2;
            Console.WriteLine("money2 -- = {1} {0}", money2.CurrencyType, money2.Amount);

            // increase 1st object of Money
            int i = 5;
            Console.WriteLine("money1 * int: {1} * {2} =  {3} {0}", money1.CurrencyType, money1.Amount, i, (money1 * i).Amount);

            // compare 2 objects of Money
            //RV: It is very confusing when == and (< or >) can be true at the same time.
            //RV: The implementation of these operator has to be consistent. Objects cannot be equal and at the same time greiter than.
            if (money1 == money2)
            {
                if (money1 > money2)
                {
                    Console.WriteLine("money1 > money2: {0} > {1}", money1.Amount, money2.Amount);
                }
                else if (money1 < money2)
                {
                    Console.WriteLine("money1 < money2: {0} < {1}", money1.Amount, money2.Amount);
                }
            }

            // compare 2nd object of Money and string
            string str = "1000";
            if (money2 < str)
            {
                Console.WriteLine("money2 < string: {0} < {1}", money2.Amount, str);
            }
            else
            {
                Console.WriteLine("money2 > string: {0} > {1}", money2.Amount, str);
            }

            // check CurrencyType of every object
            if (money1)
            {
                Console.WriteLine("money1 true");
            }
            else
            {
                Console.WriteLine("money1 false");
            }

            if (money2)
            {
                Console.WriteLine("money2 true");
            }
            else
            {
                Console.WriteLine("money2 false");
            }

            // convert 1st object of Money to string
            str = money1;
            Console.WriteLine("string: " + str);

            Console.ReadLine();
        }