C#键控集合(键值)和IDictionary与策略模式结合——商场收银系统

发布于:2022-12-03 ⋅ 阅读:(344) ⋅ 点赞:(0)

了解C#集合相关知识请看以前的文章

C#集合、定义集合、索引符、键控集合(键值)和IDictionary_dlwlrma_516的博客-CSDN博客icon-default.png?t=M85Bhttps://blog.csdn.net/dlwlrma_516/article/details/127139139?spm=1001.2014.3001.5501

了解策略模式请看以前的文章C#策略模式、策略与简单工厂结合模式——商场收银系统_dlwlrma_516的博客-CSDN博客icon-default.png?t=M85Bhttps://blog.csdn.net/dlwlrma_516/article/details/125801170?spm=1001.2014.3001.5501

优点:当商场增加了优惠方案时,直接定义新的具体策略类,使用Add(键值,实例化新的具体策略类)方法添加即可

Form1.cs(在窗体中的程序)

使用到的命名空间

DictionaryBase基类需要使用到using System.Collections;

using System;
using System.Collections;
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;

一共有四种类:

Context类,用于维护一个对Strategy对象的引用

Strategy类,策略类,定义所有支持的算法的公共接口

ConcreteStrategy类,具体策略类,封装了具体的算法或行为,继承Strategy类

分别对应以下程序的:CashContext类;CashSuper类;CashNormal等类

(对应商场收银主要分了三种收费模式:正常收费、满300返100、打8折)

CashSupers类,继承DictionaryBase类实现IDictionary接口,通过键值进行索引

策略类及具体策略类

        /// <summary>
        /// (父类)现金收取超类的抽象方法,收取现金,参数为原价,返回为当前价
        /// 策略类Strategy,定义所有支持的算法的公共接口
        /// </summary>
        public abstract class CashSuper
        {
            public abstract double acceptCash(double money);
        }

        /// <summary>
        /// 正常收费子类
        /// 具体策略类,封装了具体的算法或行为,继承于Strategy
        /// </summary>
        class CashNormal : CashSuper
        {
            /// <summary>
            /// 正常收费,原价返回
            /// </summary>
            /// <param name="money"></param>
            /// <returns></returns>
            public override double acceptCash(double money)
            {
                return money;
            }
        }

        /// <summary>
        /// 打折收费子类
        /// 打折收费,初始化时,必需要输入折扣率,如打八折,就是0.8
        /// 具体策略类,封装了具体的算法或行为,继承于Strategy
        /// </summary>
        class CashRebate : CashSuper
        {
            private double moneyRebate = 1d;
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="moneyRebate"></param>
            public CashRebate(string moneyRebate)
            { 
                this.moneyRebate = double.Parse(moneyRebate);
            }

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

        /// <summary>
        /// 返利收费子类
        /// 返利收费,初始化时必须要输入返利条件和返利值,比如满300反100,则moneyCondition为300,moneyReturn为100
        /// 具体策略类,封装了具体的算法或行为,继承于Strategy
        /// </summary>
        class CashReturn : CashSuper
        {
            private double moneyCondition = 0.0d;
            private double moneyReturn = 0.0d;

            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="moneyCondition"></param>
            /// <param name="moneyReturn"></param>
            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;
            }
        }

通过策略模式来封装算法

对Strategy对象的引用

        //策略模式(Strategy):它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户

        
        /// <summary>
        /// Context上下文,用一个ConcreteStrategy来配置,维护一个对Strategy对象的引用
        /// </summary>
        class CashContext
        {
            //声明一个CashSuper对象(父类)
            private CashSuper cs;

            //通过构造方法,传入具体的收费策略(子类)
            public CashContext(CashSuper csuper)
            {
                //初始化时,传入具体的策略对象
                this.cs = csuper;
            }
            
            //上下文接口
            //根据收费策略的不同,获得计算结果
            //输入参数为单种产品的总原价(单价乘以个数)
            public double GetResult(double money)
            { 
                //根据具体的策略对象,调用其算法的方法
                return cs.acceptCash(money);
            }

        }

使用CashSupers类继承DictionaryBase类,集合可以实现类似IDictionary接口,允许项通过键值(如字符串名)进行索引


        /// <summary>
        /// 集合实现IDictionary接口(基类DictionaryBase)
        /// 通过键访问索引符
        /// </summary>
        public class CashSupers : DictionaryBase
        {
            public void Add(string newID, CashSuper newCashSuper)
            {
                Dictionary.Add(newID, newCashSuper);
            }

            public void Remove(string cashSuperID)
            {
                Dictionary.Remove(cashSuperID);
            }

            public CashSupers()
            {

            }

            public CashSuper this[string cashSuperID]
            {
                get
                {
                    return (CashSuper)Dictionary[cashSuperID];
                }
                set
                {
                    Dictionary[cashSuperID] = value;
                }
            }
        }

载入窗体时,设置初始值

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //在ComboBox中加下拉选项
            cbxType.Items.AddRange(new object[] { "正常收费", "满300返100", "打8折" });
            cbxType.SelectedIndex = 0;
        }

窗体中的控件属性事件方法

单击“确定”键,按照优惠方式,计算出当前货物的价格

实例化CashSupers集合类

添加项时,需要填入键值(类型为string)和实例化对应的CashSuper子类

利用策略模式根据下拉选择框,生成相应的对象,实例化策略类、某个子类


        /// <summary>
        /// 声明一个double变量total来计算总计
        /// </summary>
        double total = 0.0d;

        private void btnOK_Click(object sender, EventArgs e)
        {
            CashContext context = null;

            CashSupers cashSuperList = new CashSupers();

            cashSuperList.Add("正常收费", new CashNormal());
            cashSuperList.Add("满300返100", new CashReturn("300", "100"));
            cashSuperList.Add("打8折", new CashRebate("0.8"));

            //利用策略模式根据下拉选择框,生成相应的对象,实例化策略类、某个子类
            //实例化不同的策略,最终在调用context.GetResult();时,所获得的结果就不尽相同
            context = new CashContext(cashSuperList[cbxType.SelectedItem.ToString()]);

            double totalPrices = 0d;
            if (txtPrice.Text == "" || Convert.ToDouble(txtPrice.Text) == 0)
            {
                MessageBox.Show("请输入货物的单价");
            }
            else if (txtNum.Text == "" || Convert.ToDouble(txtNum.Text) == 0)
            {
                MessageBox.Show("请输入货物的数量");
            }
            else if(context != null)
            {
                totalPrices = context.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));

                //将每个商品合计计入总计
                total = total + totalPrices;

                //在列表框中显示信息
                //通过多台可以得到收取费用的结果
                lbxList.Items.Add("单价:" + txtPrice.Text + "数量:" + txtNum.Text + "合计:" + totalPrices.ToString());

                textBox5.Text = total.ToString();
            }
        }

单击“重置”按钮,重新计算所有货物价格

        private void btnReset_Click(object sender, EventArgs e)
        {
            //清空单价和数量
            txtPrice.Text = "";
            txtNum.Text = "";

            //清空优惠选项
            cbxType.SelectedIndex = 0;

            //清空明细列表
            lbxList.Items.Clear();

            //清空总计数据
            total = 0;
            textBox5.Text = "";
        }

Program.cs(主程序)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

类图如下:

 

 

 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到