商场收银改进C#版--融合了工厂和策略设计模式

其实,我自己开始没看书也作了整合。

可以运行,但代码奇丑无比。又不精简。

看了书之后,慢慢领略高手的思想吧。

主要还是对类,对象,实例,多态之类的不太熟悉。

有信心慢慢练习中。。。

 1     double total = 0.0d;
 2         private void MainFormLoad(object sender, EventArgs e)
 3         {
 4             cbxType.Items.AddRange(new object[] {"正常收费","满300返100","打8折"});
 5             cbxType.SelectedIndex = 0;
 6         }
 7         
 8         void Button1Click(object sender, System.EventArgs e)
 9         {
10             CashContext cc = new CashContext(cbxType.SelectedItem.ToString());
11             double totalPrices = 0d;
12             totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text)
13                                             * Convert.ToDouble(txtNum.Text));
14             total = total + totalPrices;
15             lbxList.Items.Add("单价: " + txtPrice.Text + "   数量: " + txtNum.Text +  " " 
16                               + cbxType.SelectedItem + "   合计: " + totalPrices.ToString());
17             lblResult.Text = total.ToString();
18         }
19         
20         void Button2Click(object sender, EventArgs e)
21         {
22             txtPrice.Text = Convert.ToString(0.00);
23             txtNum.Text = Convert.ToString(0);
24             lblResult.Text = Convert.ToString(0.00);
25             lbxList.Items.Clear();
26             cbxType.SelectedIndex = 0;
27             
28         }
29         
30     }
31     abstract class CashSuper
32     {
33         public abstract double acceptCash(double money);
34     }
35     class CashNormal : CashSuper
36     {
37         public override double acceptCash(double money)
38         {
39             return money;
40         }
41     }
42     class CashRebate : CashSuper
43     {
44         private double moneyRebate = 1d;
45         public CashRebate(string moneyRebate)
46         {
47             this.moneyRebate = double.Parse(moneyRebate);
48         }
49         public override double acceptCash(double money)
50         {
51             return money * moneyRebate;
52         }
53     }
54     class CashReturn : CashSuper
55     {
56         private double moneyCondition = 0.0d;
57         private double moneyReturn = 0.0d;
58         public CashReturn(string moneyCondition, string moneyReturn)
59         {
60             this.moneyCondition = double.Parse(moneyCondition);
61             this.moneyReturn = double.Parse(moneyReturn);
62         }
63         public override double acceptCash(double money)
64         {
65             double result = money;
66             if(money >= moneyCondition)
67                 result = money - Math.Floor(money / moneyCondition) * moneyReturn;
68             
69             return result;
70         }
71     }
72     
73     class CashContext
74     {
75         CashSuper cs = null;
76     
77         public  CashContext(string type)
78         {
79             switch (type)
80             {
81                 case "正常收费":
82                     CashNormal cs0= new CashNormal();
83                     cs = cs0;
84                     break;
85                 case "满300返100":
86                     CashReturn cr1 = new CashReturn("300", "100");
87                     cs = cr1;
88                     break;
89                 case "打8折":
90                     CashRebate cr2 = new CashRebate("0.8");
91                     cs = cr2;
92                     break;
93             }
94         }
95         public double GetResult(double money)
96         {
97             return cs.acceptCash(money);
98         }
99     }

输出:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值