Beispiel #1
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            double      totalPrices = 0d;
            CashContext csuper      = new CashContext(cbxType.SelectedItem.ToString());

            totalPrices = csuper.GetResult(Convert.ToDouble(txtPrice.Text)
                                           * Convert.ToDouble(txtNum.Text));
            total = total + totalPrices;
            lbxList.Items.Add("单价:" + txtPrice.Text + "数量:" + txtNum.Text +
                              " " + cbxType.SelectedItem + " 合计:" + totalPrices.ToString());
            lblResult.Text = total.ToString();
        }
Beispiel #2
0
        private void receipt_Click(object sender, EventArgs e)
        {
            double totalPrice = 0d;

            /* 使用简单工厂模式实现
             * Cash cash = CashFactory.createCash(way.SelectedItem.ToString());
             * totalPrice = cash.receipt(Convert.ToDouble(pricebox.Text) * Convert.ToDouble(numbox.Text));
             * total += totalPrice;
             */

            /* 使用策略模式实现
             * string strway = way.SelectedItem.ToString();
             * CashContext cashcontext = new CashContext(strway);
             * totalPrice = cashcontext.getReceipt(Convert.ToDouble(pricebox.Text) * Convert.ToDouble(numbox.Text));
             * total += totalPrice;
             * recordlist.Items.Add("单价:" + pricebox.Text +
             *  "数量" + numbox.Text + " " + way.SelectedItem + "合计:" + totalPrice.ToString());
             * sum.Text = total.ToString();
             */

            //使用反射机制结合策略模式
            CashContext cashcontext = new CashContext();

            DataRow dr = ((DataRow[])xmlConfig.Tables[0].Select("name='" + way.SelectedItem.ToString() + "'"))[0];

            //声明一个参数的对象数组
            object[] args = null;
            if (dr["para"].ToString() != "")
            {
                args = dr["para"].ToString().Split(',');
            }
            cashcontext.CashImp = (Cash)Assembly.Load("CashRegister").CreateInstance(
                "CashRegister.strategy." + dr["class"].ToString(), false,
                BindingFlags.Default, null, args, null, null
                );
            totalPrice = cashcontext.getReceipt(Convert.ToDouble(pricebox.Text) * Convert.ToDouble(numbox.Text));
            total     += totalPrice;
            recordlist.Items.Add("单价:" + pricebox.Text +
                                 "数量" + numbox.Text + " " + way.SelectedItem + "合计:" + totalPrice.ToString());
            sum.Text = total.ToString();
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            Console.Write("//策略模式\n");

            string output     = "";
            double Price      = 350.0d;
            double Number     = 10.0d;
            double totalPrice = 0.0d;
            //CashContext csuper = new CashContext("正常收費");
            //CashContext csuper = new CashContext("滿300送100");
            CashContext csuper = null;


            Console.Write("#1 正常收費\n");

            csuper = new CashContext("正常收費");

            totalPrice = csuper.GetResult(Price * Number);
            total     += totalPrice;
            output     = string.Format("Unit Price:{0:###.###}\nQuantity:{1:###}\nSum:{2:###.###}\n\n", Price, Number, total);
            Console.Write(output);

            Console.Write("#2 打八折\n");
            csuper     = new CashContext("打八折");
            total      = 0.0d;
            totalPrice = csuper.GetResult(Price * Number);
            total     += totalPrice;
            output     = string.Format("Unit Price:{0:###.###}\nQuantity:{1:###}\nSum:{2:###.###}\n\n", Price, Number, total);
            Console.Write(output);

            Console.Write("#3 滿300送100\n");
            csuper     = new CashContext("滿300送100");
            total      = 0.0d;
            totalPrice = csuper.GetResult(Price * Number);
            total     += totalPrice;
            output     = string.Format("Unit Price:{0:###.###}\nQuantity:{1:###}\nSum:{2:###.###}\n", Price, Number, total);
            Console.Write(output);
            Console.Read();
        }