Esempio n. 1
0
        public void SimpleFactory_Success_Test()
        {
            Operation oper;

            oper         = OperationFactory.CreateOperate("+");
            oper.NumberA = 1;
            oper.NumberB = 2;
            var addResult = oper.GetResult();

            Assert.AreEqual(addResult, 1 + 2);

            oper         = OperationFactory.CreateOperate("-");
            oper.NumberA = 1;
            oper.NumberB = 2;
            var subResult = oper.GetResult();

            Assert.AreEqual(subResult, 1 - 2);

            oper         = OperationFactory.CreateOperate("*");
            oper.NumberA = 1;
            oper.NumberB = 2;
            var mulResult = oper.GetResult();

            Assert.AreEqual(mulResult, 1 * 2);

            oper         = OperationFactory.CreateOperate("/");
            oper.NumberA = 1;
            oper.NumberB = 2;
            var divResult = oper.GetResult();

            Assert.AreEqual(divResult, 1.00 / 2.00);
        }
Esempio n. 2
0
        public void SimpleFactoryTestMethod()
        {
            Operation oper = null;
            double    numA = 99;
            double    numB = 9.9;

            oper         = OperationFactory.CreateOperate(OperationType.ADD);
            oper.NumberA = numA;
            oper.NumberB = numB;
            Assert.AreEqual(typeof(OperationAdd), oper.GetType());
            Assert.AreEqual(numA + numB, oper.GetResult());

            oper         = OperationFactory.CreateOperate(OperationType.SUB);
            oper.NumberA = numA;
            oper.NumberB = numB;
            Assert.AreEqual(typeof(OperationSub), oper.GetType());
            Assert.AreEqual(numA - numB, oper.GetResult());

            oper         = OperationFactory.CreateOperate(OperationType.MUL);
            oper.NumberA = numA;
            oper.NumberB = numB;
            Assert.AreEqual(typeof(OperationMul), oper.GetType());
            Assert.AreEqual(numA * numB, oper.GetResult());

            oper         = OperationFactory.CreateOperate(OperationType.DIV);
            oper.NumberA = numA;
            oper.NumberB = numB;
            Assert.AreEqual(typeof(OperationDiv), oper.GetType());
            Assert.AreEqual(numA / numB, oper.GetResult());
        }
        public void Run()
        {
            var oper = OperationFactory.CreateOperate("*");

            oper.NumberA = 1;
            oper.NumberB = 2;
            Console.WriteLine(oper.GetResult());
        }
Esempio n. 4
0
        private void OperationNum(string s)
        {
            CalcOperation oper;

            oper         = OperationFactory.CreateOperate(s);
            oper.NumberA = Number;
            oper.NumberB = double.Parse(OperationResult);
            Number       = oper.GetResult();
        }
        /// <summary>
        /// 计算器
        /// </summary>
        /// <returns></returns>
        public ActionResult Calculator()
        {
            Operation oper;

            oper         = OperationFactory.CreateOperate("+");
            oper.NumberA = 1;
            oper.NumberB = 2;
            double result = oper.GetResult();

            return(View());
        }
        public ActionResult SimpleFactory(string id)
        {
            Operation oper;

            oper         = OperationFactory.CreateOperate("+");
            oper.NumberA = 5;
            oper.NumberB = 6;
            double result = oper.GetResult();

            return(Content(result.ToString()));
        }
Esempio n. 7
0
 public void SimpleFactory_DivEqualZeroException_Test()
 {
     try
     {
         Operation oper;
         oper         = OperationFactory.CreateOperate("/");
         oper.NumberA = 1;
         oper.NumberB = 0;
         var result = oper.GetResult();
     }
     catch (Exception ex)
     {
         Assert.AreEqual(ex.Message, "除數不能為0");
     }
 }
Esempio n. 8
0
 public void SimpleFactory_OprationFailException_Test()
 {
     try
     {
         Operation oper;
         oper         = OperationFactory.CreateOperate("!!");
         oper.NumberA = 1;
         oper.NumberB = 2;
         var result = oper.GetResult();
     }
     catch (Exception ex)
     {
         Assert.AreEqual(ex.Message, "不支援此運算符號");
     }
 }
        public void Test_CreateOperate_Empty()
        {
            // given
            double expectedResult = 0;

            var operation = OperationFactory.CreateOperate("");

            operation.NumberA = _operation.NumberA;
            operation.NumberB = _operation.NumberB;

            // when
            double result = operation.GetResult();

            // then
            Assert.AreEqual(expectedResult, result);
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("请输入:");
                OperationFactory Of = new OperationFactory();
                Operation        o  = Of.CreateOperate("+");
                Console.WriteLine(o.GetResult());
                Console.ReadKey();
            }



            //while(true)
            //{
            //    List<char> strValue = new List<char>();
            //    string enterNumber = Console.ReadLine();
            //    if (enterNumber.Contains("+"))
            //    {
            //        int i = 0;
            //        foreach (var item in enterNumber)
            //        {
            //            if (item == '+')
            //            {
            //                i = i + 1;
            //            }
            //        }
            //        if (i > 1)
            //        {
            //            Console.WriteLine("输入无效");
            //        }
            //        else
            //        {
            //            string[] tempArry = enterNumber.Split('+');

            //            OperationAdd oper = new OperationAdd();
            //            oper.NumberA = Convert.ToInt32(tempArry[0]);
            //            oper.NumberB = Convert.ToInt32(tempArry[1]);
            //            double result = oper.GetResult();
            //            Console.WriteLine(result);
            //        }
            //    }
            //}
        }