收银系统 计算方式 一直会变 今天 是满300 返100 明天 可能 就是满 300 返50 我们不可能 一直 去改程序 所以 我们还要 用到反射 来实现 动态的 更改 计算方式
这里面我 们先创建一下xml文件 代码如下
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153using 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
最后
以上就是听话哈密瓜最近收集整理的关于大话设计之策略模式实现简易收银系统的全部内容,更多相关大话设计之策略模式实现简易收银系统内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复