C#简易商城收银系统v1.1简单工厂实现(2-2)

C#简易商城收银系统v1.1简单工厂实现(2-2)

  1. 当初:
    • C#简易商城收银系统v1.0
  2. 现在:
    • 用之前的工厂模式对商城收银系统v1.0进行升级


可以参考之前的 C#简易商城收银系统v1.0 随笔     https://www.cnblogs.com/zaohuojian/p/11494997.html

添加CashSuper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 商城收银软件
{
abstract class CashSuper  //现金收费抽象类
{
public abstract double acceptCash(double money); //现金收取的抽象方法.参数为原价,返回当前价格
}
}

 

添加CasHNormal类,并引用CashSuper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 商城收银软件
{
class CasHNormal : CashSuper //正常收费类
{
public override double acceptCash(double money)
{
return money; //正常收费,原价返回
}
}
}

 

添加CashRebate类,并引用CashSuper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 商城收银软件
{
class CashRebate : CashSuper//打折收费类
{
private double moneyRebate = 1d;
public CashRebate(string moneyRebate)
{
//打折收费初始化
//必须输入折扣率,如8折
this.moneyRebate = double.Parse(moneyRebate);

}
public override double acceptCash(double money)
{
return money * moneyRebate;
}
}
}

 

添加CashRrturn类,并引用CashSuper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 商城收银软件
{
class CashReturn : CashSuper //返利收费子类
{
private double moneyCondition = 0.0d;
private double moneyReturn = 0.0d;
public CashReturn(string moneyCondition,string moneyReturn)
{
this.moneyCondition = double.Parse(moneyCondition);
this.moneyReturn = double.Parse(moneyReturn);
}
public override double acceptCash(double money)
{
double result = money;
if (money>=moneyCondition)//若大于返利,就减去返利值
{
result = money - Math.Floor(money / moneyCondition) * moneyReturn;
}
return result;
}
}
}

 

添加CashFactory类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 商城收银软件
{
 class CashFactory //现金收费工厂类
{

public static CashSuper createCashAccept(string type) 
{
CashSuper cs = null;
switch (type)
{
case "正常收费":
cs = new CasHNormal();
break;
case "满300返100":
cs = new CashReturn("300","100");
break;
case "打八折":
cs = new CashRebate("0.8");
break;
}
return cs
}
}
}

 

客户端主要类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 商城收银软件
{
public partial class Form1 : Form //客户端类
{
public Form1()
{
InitializeComponent();
}
double total = 0.0d;//声明一个double变量,用total来计算总计
private void button1_Click(object sender, EventArgs e)
{
CashSuper csuper = CashFactory.createCashAccept(cbxType.SelectedItem.ToString());
double totalPrices = 0d;
totalPrices = csuper.acceptCash(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNum.Text));


total = total + totalPrices;
lbxList.Text = "单价:" + txtPrice.Text + "数量:" + txtNum.Text + " " + cbxType.SelectedItem + "合计:" + totalPrices.ToString();  
lblResult.Text = total.ToString();


}
private void Form1_Load(object sender, EventArgs e)
{
cbxType.Items.AddRange(new object[] { "正常收费", "打8折", "满300返100"});
cbxType.SelectedIndex = 0;
}
  }
}

 

总结 面向对象简单工厂实例

添加多个不同商品折扣类型

定义工厂类

客户端实例化出来

多看 多敲 慢慢的就可以领悟其中的设计模式

转载于:https://www.cnblogs.com/zaohuojian/p/11495150.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值