大话设计之策略模式实现简易收银系统

收银系统 计算方式 一直会变 今天 是满300 返100 明天 可能 就是满 300 返50 我们不可能 一直 去改程序 所以 我们还要 用到反射 来实现 动态的 更改 计算方式 

这里面我 们先创建一下xml文件 代码如下

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

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        double total = 0.0d;

        private void Form1_Load(object sender, EventArgs e)
        {
            //WriteXml();
            ReadXml();
        }

        DataSet ds;
        private void ReadXml()
        {
            ds = new DataSet();
            ds.ReadXml(Application.StartupPath + @"\CashAcceptType.xml");

            foreach (DataRowView dr in ds.Tables[0].DefaultView)
            {
                cbxType.Items.Add(dr["name"].ToString());
            }

            cbxType.SelectedIndex = 0;

        }


        private void WriteXml()
        {
            string file = Application.StartupPath + @"\CashAcceptType.xml";
                XmlDocument xml = new XmlDocument();
                XmlNode head = xml.CreateXmlDeclaration("1.0","utf-8",null);

                xml.AppendChild(head);

            //创建一级节点
            XmlElement root = xml.CreateElement("CashAcceptType");

            List<User_xml> list = new List<User_xml>();

           list.Add( new User_xml("正常收费", "CashNormal", ""));
            list.Add(new User_xml("满300返100", "CashReturn", "300,100"));

            list.Add(new User_xml("满200返50", "CashReturn", "200,50"));
            list.Add(new User_xml("打8折", "CashRebate", "0.8"));

            foreach (var item in list)
            {
              var note=  Xml_Add_note(xml, item);
                root.AppendChild(note);
            }

         

            xml.AppendChild(root);
            xml.Save(file);

        }

        private static XmlElement Xml_Add_note(XmlDocument xml, User_xml user)
        {

            XmlElement note = xml.CreateElement("type");

            XmlElement note_name = xml.CreateElement("name");
            note_name.InnerText = user.name;

            XmlElement note_class = xml.CreateElement("class");
            note_class.InnerText = user.@class;

            XmlElement note_para = xml.CreateElement("para");
            note_para.InnerText = user.para;

            note.AppendChild(note_name);
            note.AppendChild(note_class);
            note.AppendChild(note_para);

            return note;
          

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DataRow dr = (ds.Tables[0].Select("name='" + cbxType.SelectedItem.ToString() + "'"))[0];

            string[] args = null;
            if (dr["para"].ToString()!= ""){
                args = dr["para"].ToString().Split(',');
            }

            CashSuper su = (CashSuper)Assembly.Load("WindowsFormsApp1").CreateInstance("WindowsFormsApp1."+dr["class"].ToString(),false,BindingFlags.Default,null,args,null,null);
            CashContext cs = new CashContext(su);

            double totalPrices = 0d;
            totalPrices = cs.AcceptCash(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();


        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            total = 0d;
            txtPrice.Text = "0.00";
            txtNum.Text = "1";
            lbxList.Items.Clear();
            lblResult.Text = "0.00";
        }
    }


    class User_xml
    {
        public int Id { set; get; }
        public string name { get; set; }
        public string @class { get; set; }
        public string @para { set; get; }

        public User_xml(string _name,string _class,string _para)
        {
            this.name = _name;
            this.@class = _class;
            this.para = _para;
        }

      
        
    }
}

还需要 创建 几个收款 方式 类 这里面 我们只需 用到 三种 一种是打折 一种是满多少 返多少 还有一种是 正常模式 

代码 就不一一贴了 代码 下载 地址如下

https://download.csdn.net/download/mjkmjk485485/10779311

猜你喜欢

转载自blog.csdn.net/mjkmjk485485/article/details/83989986
今日推荐