【深入学习.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 /// <summary>
三、首先创建两个项目对象集合,在添加多个初始项目对象


- 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 ...
随机推荐
- 个人冲刺(一)——体温上报app(二阶段)
冲刺任务:完成app登录和注册页面的布局 activity_register.xml <?xml version="1.0" encoding="utf-8&quo ...
- 树莓派使用Docker部署EdgeX(jakarta版本)
使用Docker部署EdgeX 老师安排我搞边缘计算,搞了很久都没能明白边缘计算是什么,甚至对其兴趣不大,前一阵弄好了lorawan网关,该做网关内部的边缘计算了,发现自己已经慢慢地学了进去,总是想要 ...
- 面试常问的dubbo的spi机制到底是什么?
前言 dubbo是一款微服务开发框架,它提供了 RPC通信 与 微服务治理 两大关键能力.作为spring cloud alibaba体系中重要的一部分,随着spring cloud alibaba在 ...
- PyTorch DataLoader NumberWorkers Deep Learning Speed Limit Increase
这意味着训练过程将按顺序在主流程中工作. 即:run.num_workers. ,此外, ,因此,主进程不需要从磁盘读取数据:相反,这些数据已经在内存中准备好了. 这个例子中,我们看到了20%的加 ...
- 将 Ubuntu 16.04 LTS 的 Unity 启动器移动到桌面底部命令
将 Ubuntu 16.04 LTS 的 Unity 启动器移动到桌面底部命令: gsettings set com.canonical.Unity.Launcher launcher-positio ...
- 菜鸟学git的基本命令及常见错误
Git init //在当前项目工程下履行这个号令相当于把当前项目git化,变身!\ git config --global user.name "xxx" # 配置用户名 git ...
- 离线环境使用NuGet
更新记录 本文迁移自Panda666原博客,原发布时间:2021年5月13日. 在Visual Studio中直接使用GUI的方式操作NuGet包非常的方便.如果喜欢命令行,也可以使用包管理控制台或者 ...
- VR技术赋能五大领域,不止高级,更高效!
除了VR游戏.VR影视作品,究竟还有哪些产业领域会应用到VR技术并为生活带来改变呢?今天就帮大家好好梳理一下~ VR赋能交通,不只是高级 最近在网上看到了VR考驾照的新闻,网友都赞叹,现在学车都这么高 ...
- Elasticsearch学习系列三(搜索案例实战)
Query DSL Es提供了基于JSON的完整查询DSL(Domain Specific Language 特定域的语言)来定义查询.将查询DSL视为查询的AST(抽象语法树).它由两种子句组成: ...
- 使用aggregation API扩展你的kubernetes API
Overview What is Kubernetes aggregation Kubernetes apiserver aggregation AA 是Kubernetes提供的一种扩展API的方法 ...