开发应用-命令模式(C#实现POS收银功能)
开发应用-命令模式(C#实现POS收银功能)
命令模式(Command Pattern)相对简单,但应用广泛,不可不察也!
命令模式(Command Pattern)代码参考:
/// <summary>
/// NULL Object 模式
/// </summary>
public class CommandNull : CommandBase
{
public override void Execute()
{
MessageBox.Show("输入非法命令!");
}
}
/// <summary>
/// 输入收银金额
/// </summary>
public class CommandReceiveMoney : CommandBase
{
private decimal _money = 0;
public CommandReceiveMoney(POS pos, decimal money)
{
_POS = pos;
_money = money;
}
public override void Execute()
{
_POS.ReceiveMoney(_money);
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// <summary>
/// 打印小票命令
/// </summary>
public class CommandPrintBill : CommandBase
{
public CommandPrintBill(POS pos)
{
_POS = pos;
}
public override void Execute()
{
_POS.PrintBill();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// <summary>
/// 由收银台输入货品编号,返回Stock实体对象。
/// 这里可以从SQL Server取数。
/// </summary>
public class POS
{
private ucMonitor _Monitor; //收银显示屏
private IList _StockList = new ArrayList(); //货品列表
private decimal _ReceivedMoney = 0; //客户支付(现金或借记卡)的金额
private decimal _ReceivableTotal = 0;//客户应付金额
private bool _ReceiveMode = false;
public bool IsReceiveMode { get { return _ReceiveMode; } }
public POS(ucMonitor monitor)
{
_Monitor = monitor;
this.Reset();
}
/// <summary>
/// 重设收银台
/// </summary>
public void Reset()
{
_ReceiveMode = false;
_ReceivedMoney = 0;
_ReceivableTotal = 0;
_StockList.Clear();
_Monitor.ShowWelcome();
_Monitor.ShowStatus(PosMode.InputStock);
}
/// <summary>
/// 检测输入命令
/// </summary>
public CommandBase ParseCommand(string commandString)
{
//首长检查输入的命令是否货品编号
Stock stock = this.GetStock(commandString);
if (stock != null)
return new CommandInputStock(this, stock);
else
return CommandFactory.CreateCommand(this, commandString);
}
//测试用.可创建两个货品对象
private Stock GetStock(string code)
{
code = code.ToUpper();
if (code == "VC".ToUpper())
return new Stock("Vit.C", decimal.Parse("5.8"));
else if (code == "PP".ToUpper())
return new Stock("Panicilin", decimal.Parse("15.20"));
else
return null;
}
// 这里可以从SQL Server取数。
private static Stock GetStockFromSQL(string code)
{
//连接SQL Server.查询记录,返回Datarow,将Datarow 转换为Stock
return null;
}
/// <summary>
/// 输入货品
/// </summary>
/// <param name="stock"></param>
public void InputStock(Stock stock)
{
_StockList.Add(stock);
//累加应收金额
_ReceivableTotal = stock.Price;
_Monitor.ShowCurrentStock(stock);
}
/// <summary>
/// 结帐
/// </summary>
public void CheckOut()
{
if (_ReceivedMoney > 0 || _ReceivableTotal > 0)
{
if (_ReceivedMoney >= _ReceivableTotal)
{
this.PrintBill();//打印小票
this.Reset(); //处理下一位客户
MessageBox.Show("打印完毕,处理下一位客户。");
}
else
{
MessageBox.Show("金额不足,不能结帐....");
}
}
else
{
MessageBox.Show("大哥,没有输入货品,不能结帐!");
}
}
/// <summary>
/// 打印小票
/// </summary>
public void PrintBill()
{
string msg = string.Format("总共{0}个货品\r\n\r\n正在打印小票...", _StockList.Count);
MessageBox.Show(msg);
}
/// <summary>
/// 设置收银模式
/// </summary>
public void SetReceiveMode()
{
_ReceiveMode = true;
_Monitor.ShowStatus(PosMode.CheckBill);
}
/// <summary>
/// 收款
/// </summary>
/// <param name="money"></param>
public void ReceiveMoney(decimal money)
{
_ReceivedMoney = money;
_Monitor.ShowReceive(money);
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
扫一扫加微信
请下载附件 (Source for VIP)
命令模式(Command Pattern)相对简单,但应用广泛,不可不察也!
本示范程序是摘抄08年写的一套POS系统(仅供参考):
命令模式(Command Pattern)类关系图:
命令模式(Command Pattern)代码参考:
/// <summary>
/// 定义命令基类
/// </summary>
public class CommandBase
{
protected POS _POS;
/// <summary>
/// 执行命令
/// </summary>
public virtual void Execute() { }
}
/// 定义命令基类
/// </summary>
public class CommandBase
{
protected POS _POS;
/// <summary>
/// 执行命令
/// </summary>
public virtual void Execute() { }
}
/// <summary>
/// NULL Object 模式
/// </summary>
public class CommandNull : CommandBase
{
public override void Execute()
{
MessageBox.Show("输入非法命令!");
}
}
/// <summary>
/// 输入货品, 检测输入的命令是货品编号
/// </summary>
public class CommandInputStock : CommandBase
{
private Stock _stock;
public CommandInputStock(POS pos, Stock stock)
{
_POS = pos;
_stock = stock;
}
public override void Execute()
{
_POS.InputStock(_stock);
}
}
/// 输入货品, 检测输入的命令是货品编号
/// </summary>
public class CommandInputStock : CommandBase
{
private Stock _stock;
public CommandInputStock(POS pos, Stock stock)
{
_POS = pos;
_stock = stock;
}
public override void Execute()
{
_POS.InputStock(_stock);
}
}
/// <summary>
/// 收银命令,进入收银状态
/// </summary>
public class CommandSetReceiveMode : CommandBase
{
public CommandSetReceiveMode(POS pos)
{
_POS = pos;
}
public override void Execute()
{
_POS.SetReceiveMode();
}
}
/// 收银命令,进入收银状态
/// </summary>
public class CommandSetReceiveMode : CommandBase
{
public CommandSetReceiveMode(POS pos)
{
_POS = pos;
}
public override void Execute()
{
_POS.SetReceiveMode();
}
}
/// <summary>
/// 输入收银金额
/// </summary>
public class CommandReceiveMoney : CommandBase
{
private decimal _money = 0;
public CommandReceiveMoney(POS pos, decimal money)
{
_POS = pos;
_money = money;
}
public override void Execute()
{
_POS.ReceiveMoney(_money);
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// <summary>
/// 打印小票命令
/// </summary>
public class CommandPrintBill : CommandBase
{
public CommandPrintBill(POS pos)
{
_POS = pos;
}
public override void Execute()
{
_POS.PrintBill();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// <summary>
/// 收银/结帐命令
/// </summary>
public class CommandCheckOut : CommandBase
{
public CommandCheckOut(POS pos)
{
_POS = pos;
}
public override void Execute()
{
_POS.CheckOut();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// 收银/结帐命令
/// </summary>
public class CommandCheckOut : CommandBase
{
public CommandCheckOut(POS pos)
{
_POS = pos;
}
public override void Execute()
{
_POS.CheckOut();
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
/// <summary>
/// 取消当前交易
/// </summary>
public class CommandCancelTransaction : CommandBase
{
public CommandCancelTransaction(POS pos)
{
_POS = pos;
}
public override void Execute()
{
if (DialogResult.Yes == MessageBox.Show("要取消交易吗?", "确认程序", MessageBoxButtons.YesNo))
{
_POS.Reset();
}
}
}
/// 取消当前交易
/// </summary>
public class CommandCancelTransaction : CommandBase
{
public CommandCancelTransaction(POS pos)
{
_POS = pos;
}
public override void Execute()
{
if (DialogResult.Yes == MessageBox.Show("要取消交易吗?", "确认程序", MessageBoxButtons.YesNo))
{
_POS.Reset();
}
}
}
/// <summary>
/// 退出系统命令
/// </summary>
public class CommandExit : CommandBase
{
public CommandExit(POS pos)
{
_POS = pos;
}
public override void Execute()
{
if (DialogResult.Yes == MessageBox.Show("要退出系统吗?", "确认程序", MessageBoxButtons.YesNo))
{
Application.Exit();
}
}
}
/// 退出系统命令
/// </summary>
public class CommandExit : CommandBase
{
public CommandExit(POS pos)
{
_POS = pos;
}
public override void Execute()
{
if (DialogResult.Yes == MessageBox.Show("要退出系统吗?", "确认程序", MessageBoxButtons.YesNo))
{
Application.Exit();
}
}
}
/// <summary>
/// 由收银台输入货品编号,返回Stock实体对象。
/// 这里可以从SQL Server取数。
/// </summary>
public class POS
{
private ucMonitor _Monitor; //收银显示屏
private IList _StockList = new ArrayList(); //货品列表
private decimal _ReceivedMoney = 0; //客户支付(现金或借记卡)的金额
private decimal _ReceivableTotal = 0;//客户应付金额
private bool _ReceiveMode = false;
public bool IsReceiveMode { get { return _ReceiveMode; } }
public POS(ucMonitor monitor)
{
_Monitor = monitor;
this.Reset();
}
/// <summary>
/// 重设收银台
/// </summary>
public void Reset()
{
_ReceiveMode = false;
_ReceivedMoney = 0;
_ReceivableTotal = 0;
_StockList.Clear();
_Monitor.ShowWelcome();
_Monitor.ShowStatus(PosMode.InputStock);
}
/// <summary>
/// 检测输入命令
/// </summary>
public CommandBase ParseCommand(string commandString)
{
//首长检查输入的命令是否货品编号
Stock stock = this.GetStock(commandString);
if (stock != null)
return new CommandInputStock(this, stock);
else
return CommandFactory.CreateCommand(this, commandString);
}
//测试用.可创建两个货品对象
private Stock GetStock(string code)
{
code = code.ToUpper();
if (code == "VC".ToUpper())
return new Stock("Vit.C", decimal.Parse("5.8"));
else if (code == "PP".ToUpper())
return new Stock("Panicilin", decimal.Parse("15.20"));
else
return null;
}
// 这里可以从SQL Server取数。
private static Stock GetStockFromSQL(string code)
{
//连接SQL Server.查询记录,返回Datarow,将Datarow 转换为Stock
return null;
}
/// <summary>
/// 输入货品
/// </summary>
/// <param name="stock"></param>
public void InputStock(Stock stock)
{
_StockList.Add(stock);
//累加应收金额
_ReceivableTotal = stock.Price;
_Monitor.ShowCurrentStock(stock);
}
/// <summary>
/// 结帐
/// </summary>
public void CheckOut()
{
if (_ReceivedMoney > 0 || _ReceivableTotal > 0)
{
if (_ReceivedMoney >= _ReceivableTotal)
{
this.PrintBill();//打印小票
this.Reset(); //处理下一位客户
MessageBox.Show("打印完毕,处理下一位客户。");
}
else
{
MessageBox.Show("金额不足,不能结帐....");
}
}
else
{
MessageBox.Show("大哥,没有输入货品,不能结帐!");
}
}
/// <summary>
/// 打印小票
/// </summary>
public void PrintBill()
{
string msg = string.Format("总共{0}个货品\r\n\r\n正在打印小票...", _StockList.Count);
MessageBox.Show(msg);
}
/// <summary>
/// 设置收银模式
/// </summary>
public void SetReceiveMode()
{
_ReceiveMode = true;
_Monitor.ShowStatus(PosMode.CheckBill);
}
/// <summary>
/// 收款
/// </summary>
/// <param name="money"></param>
public void ReceiveMoney(decimal money)
{
_ReceivedMoney = money;
_Monitor.ShowReceive(money);
}
}
// 来源:www.CSFramework.com, C/S结构框架学习网
扫一扫加微信
请下载附件 (Source for VIP)
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网