【深入学习.Net】.泛型集合【体检管理系统】
基于泛型List的体检管理系统(蜗牛爬坡)
第五章【体检管理系统】
一、项目展示图(基于.net core6.0)

二、首先准备两个Model类
- HealthCheckItem(项目类):Name(项目名称)、Description(项目描述)、Price(当前项目价格)
- HealthCheckSet(套餐类):Name(套餐名称)、Items(套餐项目集合)、Price(套餐项目总价格)
- 代码如下

1 /// <summary>
2 /// 检查项目类
3 /// </summary>
4 public class HealthCheckItem
5 {
6 public HealthCheckItem(string name, string description, decimal price)
7 {
8 Name = name;
9 Description = description;
10 Price = price;
11 }
12
13 /// <summary>
14 /// 项目名称
15 /// </summary>
16 public string Name { get; set; }
17 /// <summary>
18 /// 项目描述
19 /// </summary>
20 public string Description { get; set; }
21 /// <summary>
22 /// 项目价格
23 /// </summary>
24 public decimal Price { get; set; }
25 }
1 /// <summary>
2 /// 套餐类
3 /// </summary>
4 public class HealthCheckSet
5 {
6 /// <summary>
7 /// 套餐名称
8 /// </summary>
9 public string Name { get; set; }
10
11 /// <summary>
12 ///套餐包含的项目
13 /// </summary>
14 public List<HealthCheckItem> Items { get; set; }
15
16 /// <summary>
17 /// 包含项目的总价
18 /// </summary>
19 public decimal Price { get; set; }
20
21 /// <summary>
22 /// 计算总价的方法
23 /// </summary>
24 public void CalcPrice()
25 {
26 decimal totalPrice = 0;
27 foreach (var item in Items)
28 {
29 totalPrice+=item.Price;
30 }
31 this.Price = totalPrice;
32 }
33
34 public HealthCheckSet()
35 {
36 Items = new List<HealthCheckItem>();
37 }
38 public HealthCheckSet(string name, List<HealthCheckItem> items)
39 {
40 Name = name;
41 Items = items;
42 }
43 }
三、首先创建两个项目对象集合,在添加多个初始项目对象

1 private List<HealthCheckItem> AllItems = new List<HealthCheckItem>(); //所有项目对象集合
2 private List<HealthCheckItem> items = new List<HealthCheckItem>();// 套餐项目集合
3 private HealthCheckItem item1, item2, item3, item4, item5, item6, item7; //初始项目
四、创建套餐对象、和套餐对象集合

1 private HealthCheckSet healthSet; //初始套餐对象
2 private List<HealthCheckSet> healthSets = new List<HealthCheckSet>(); //套餐对象集合
五、直接上代码(Tips:本项目无需点击添加相应控件事件)

1 public FrmMain()
2 {
3 InitializeComponent();
4 Load += FrmMain_Load;
5 }
6
7 /// <summary>
8 /// 窗体加载事件
9 /// </summary>
10 /// <param name="sender"></param>
11 /// <param name="e"></param>
12 private void FrmMain_Load(object? sender, EventArgs e)
13 {
14 btnAdd.Enabled = false;
15 btnDelete.Enabled = false;
16 ComboBox();
17 HealthTao();
18 btnNameAdd.Click += BtnNameAdd_Click;
19 btnAdd.Click += BtnAdd_Click;
20 cbNameList.SelectedIndexChanged += CbNameList_SelectedIndexChanged;
21 btnDelete.Click += BtnDelete_Click;
22 }
23 /// <summary>
24 /// 删除事件
25 /// </summary>
26 /// <param name="sender"></param>
27 /// <param name="e"></param>
28 private void BtnDelete_Click(object? sender, EventArgs e)
29 {
30 if (dataGridView1.SelectedRows.Count > 0)
31 {
32 int index = dataGridView1.SelectedRows[0].Index;
33 healthSets[cbNameList.SelectedIndex].Items.RemoveAt(index);
34 dataGridView1.DataSource = null;
35 Add();
36 }
37 else
38 {
39 MessageBox.Show("表中没有该选项!");
40 cbHealthList.SelectedIndex = 0;
41 }
42 }
43
44 /// <summary>
45 /// 套餐列表选中改变事件
46 /// </summary>
47 /// <param name="sender"></param>
48 /// <param name="e"></param>
49 private void CbNameList_SelectedIndexChanged(object? sender, EventArgs e)
50 {
51 if(cbNameList.Text != String.Empty)
52 {
53 btnAdd.Enabled = true;
54 btnDelete.Enabled = true;
55 dataGridView1.DataSource = null;
56 Add();
57 lbName.Text = cbNameList.Text;
58 }
59 }
60 /// <summary>
61 /// 套餐添加项目事件
62 /// </summary>
63 /// <param name="sender"></param>
64 /// <param name="e"></param>
65 private void BtnAdd_Click(object? sender, EventArgs e)
66 {
67 if (dataGridView1.SelectedRows.Count <= 0)
68 {
69 cbHealthList.SelectedIndex = 0;
70 }
71 if (cbNameList.Text != String.Empty)
72 {
73 var name = cbNameList.Text;
74 var heal = cbHealthList.Text;
75 int index = cbHealthList.SelectedIndex;
76 if ((healthSets[cbNameList.SelectedIndex].Items.Any(m => m.Name == heal)))
77 {
78 MessageBox.Show("已添加过该项目");
79 }
80 else
81 {
82 dataGridView1.DataSource = null;
83 healthSets[cbNameList.SelectedIndex].Items.Add(AllItems[index]);
84 Add();
85 MessageBox.Show("添加成功!");
86 int healIndex = cbHealthList.SelectedIndex;
87 if (healIndex < cbHealthList.Items.Count - 1)
88 {
89 cbHealthList.SelectedIndex = healIndex + 1;
90 }
91 else
92 {
93 cbHealthList.SelectedIndex = 0;
94 }
95 }
96 }
97 else
98 {
99 MessageBox.Show("请选择相应套餐!");
100 }
101 }
102 /// <summary>
103 /// 添加套餐点击事件
104 /// </summary>
105 /// <param name="sender"></param>
106 /// <param name="e"></param>
107 private void BtnNameAdd_Click(object? sender, EventArgs e)
108 {
109 if (tbName.Text.Trim() != String.Empty)
110 {
111 healthSet = new HealthCheckSet();
112 healthSet.Name = tbName.Text;
113 if (!healthSets.Any(m => m.Name == tbName.Text))
114 {
115 cbNameList.Items.Add(healthSet.Name);
116 healthSets.Add(healthSet);
117 tbName.Text = String.Empty;
118 dataGridView1.DataSource = null;
119 int index = cbNameList.FindString(healthSet.Name);
120 cbNameList.SelectedIndex = index;
121 MessageBox.Show("添加成功!");
122 }
123 else
124 {
125 MessageBox.Show("已存在该套餐了!");
126 tbName.Text = String.Empty;
127 }
128 }
129 else
130 {
131 MessageBox.Show("请输入套餐名称!");
132 }
133
134 }
135 /// <summary>
136 /// 下拉框绑定
137 /// </summary>
138 private void ComboBox()
139 {
140 item1 = new HealthCheckItem("身高", "用于检查身高", 5);
141 item2 = new HealthCheckItem("体重", "用于检查体重", 15);
142 item3 = new HealthCheckItem("视力", "用于检查视力", 5);
143 item4 = new HealthCheckItem("听力", "用于检查听力", 10);
144 item5 = new HealthCheckItem("肝功能", "用于检查肝功能", 45);
145 item6 = new HealthCheckItem("B超", "用于检查B超", 55);
146 item7 = new HealthCheckItem("心电图", "用于检查心脏", 65);
147 AllItems.Add(item1);
148 AllItems.Add(item2);
149 AllItems.Add(item3);
150 AllItems.Add(item4);
151 AllItems.Add(item5);
152 AllItems.Add(item6);
153 AllItems.Add(item7);
154 foreach (var item in AllItems)
155 {
156 cbHealthList.Items.Add(item.Name);
157 }
158 cbHealthList.SelectedIndex = 0;
159
160 }
161
162 /// <summary>
163 /// 初始套餐/项目对象
164 /// </summary>
165 private void HealthTao()
166 {
167 items.Add(item1);
168 items.Add(item2);
169 items.Add(item3);
170 items.Add(item4);
171 healthSet = new HealthCheckSet("入学套餐", items);
172 healthSets.Add(healthSet);
173 cbNameList.Items.Add(healthSet.Name);
174 healthSet.CalcPrice();
175 }
176
177 /// <summary>
178 /// 表格刷新共用方法
179 /// </summary>
180 private void Add()
181 {
182 healthSets[cbNameList.SelectedIndex].CalcPrice();
183 lbPrice.Text = healthSets[cbNameList.SelectedIndex].Price.ToString("C2");
184 dataGridView1.DataSource = healthSets[cbNameList.SelectedIndex].Items;
185 dataGridView1.Columns["Name"].HeaderText = "项目名称";
186 dataGridView1.Columns["Description"].HeaderText = "项目描述";
187 dataGridView1.Columns["Price"].HeaderText = "项目价格";
188 }
189 }
六、小结:本项目采用泛型集合实现,进一步对控件的使用和深入学习面向对象的编程思想,有更深的学习和了解
【深入学习.Net】.泛型集合【体检管理系统】的更多相关文章
- LINQ学习系列-----3.1 查询非泛型集合
一.问题起源 LINQ to object在设计时,是配合IEnumerable<T>接口的泛型集合类型使用的,例如字典.数组.List<T>等,但是对于继承了IEnumera ...
- WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)
WebAPI调用笔记 前言 即时通信项目中初次调用OA接口遇到了一些问题,因为本人从业后几乎一直做CS端项目,一个简单的WebAPI调用居然浪费了不少时间,特此记录. 接口描述 首先说明一下,基于 ...
- LINQ学习系列-----3.1 查询非泛型集合和多个分组
一.查询非泛型集合 1.问题起源 LINQ to object在设计时,是配合IEnumerable<T>接口的泛型集合类型使用的,例如字典.数组.List<T>等,但是对于继 ...
- 泛型学习第三天——C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
定义一个类: public class UserInfo { public System.Guid ID { get; set; } public string LoginName ...
- 【学习笔记】C#中的泛型和泛型集合
一.什么是泛型? 泛型是C#语言和公共语言运行库(CLR)中的一个新功能,它将类型参数的概念引入.NET Framework.类型参数使得设计某些类和方法成为可能,例如,通过使用泛型类型参数T,可以大 ...
- c#利用泛型集合,为自己偷偷懒。
有人说"越懒"的程序员进步的越快!其实还挺有道理.亲身体验,从刚出来工作到现在,自己变"懒"了许多,但感觉写出来的代码确有了不少提升.刚开始啊,同样的代码,赋值 ...
- 编写高质量代码改善C#程序的157个建议[泛型集合、选择集合、集合的安全]
前言 软件开发过程中,不可避免会用到集合,C#中的集合表现为数组和若干集合类.不管是数组还是集合类,它们都有各自的优缺点.如何使用好集合是我们在开发过程中必须掌握的技巧.不要小看这些技巧,一旦在开 ...
- ConvertHelper与泛型集合
在机房重构时.我们常常会用到ConvertHelper. 它把从数据库中查询到的dateTable(也是一个暂时表)转化为泛型,然后再填充到DataGridView控件中. ConvertHelper ...
- C# 找出泛型集合中的满足一定条件的元素 List.Wher()
在学习的过程中,发现泛型集合List<T>有一个Where函数可以筛选出满足一定条件的元素,结合Lambda表达式使用特别方便,写出来与大家分享. 1.关于Func<> Fun ...
随机推荐
- [补漏]shift&算法
题意:regular number 给你一个字符串,要你输出所有(每位都符合要求的)子串,输入时告诉你每位只能填的数集. 思路: bitsetc[x]存每个数字可以存在的字符串位的二进制集合.(如3可 ...
- Vmware虚拟机安装及相关配置流程
1.Vmware虚拟软件安装 1.1下载地址 vmware 12 pro 的版本稳定性较好,所有我们最好选择该版本 下载地址:https://www.onlinedown.net/soft/10053 ...
- 接口测试postman深度挖掘应用②
上一篇文章我们已经介绍了postman的基本用法,以及接口测试原理.未关注博主的可以关注下博主,后期测试安全,以及各种工具用法,编程语言都会有讲解的 老样子:①先补习基础知识: ②补充上一章节由于过 ...
- c++ 平衡树
平衡树的性质 它其实就是一个 BST(Binary Search Tree 二叉搜索树). 当然,不同的平衡树会有自己的特性 BST 的性质 只有一个:任意一个节点的左子树的所有节点都比它的优先级高, ...
- Puppeteer学习笔记 (1)- 什么是Puppeteer
本文链接:https://www.cnblogs.com/hchengmx/p/11006263.html 1. phantomjs介绍 在介绍puppeteer之前必须介绍一下phantomjs,p ...
- SAP创建XML 文件
TYPES: BEGIN OF xml_line_type, data(256) TYPE x, END OF xml_line_type, xml_tab_type TYPE TABLE OF xm ...
- 用Python做了个图片识别系统(附源码)
本项目将使用python3去识别图片是否为色情图片,会使用到PIL这个图像处理库,并且编写算法来划分图像的皮肤区域 介绍一下PIL: PIL(Python Image Library)是一种免费的图像 ...
- 女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!
1. 背景 MySQL实现事务.崩溃恢复.集群的主从复制,底层都离不开日志,所以日志是MySQL的精华所在.只有了解MySQL日志,才算是彻底搞懂MySQL. 今天一灯就带你深入浅出的学习MySQL的 ...
- 自建批量更改标准BO数据程序
by zyi
- Java开发学习(七)----DI依赖注入之自动装配与集合注入
一.自动配置 上一篇博客花了大量的时间把Spring的注入去学习了下,总结起来就两个字麻烦.麻烦在配置文件的编写配置上.那有更简单方式么?有,自动配置 1.1 依赖自动装配 IoC容器根据bean所依 ...