基于泛型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】.泛型集合【体检管理系统】的更多相关文章

  1. LINQ学习系列-----3.1 查询非泛型集合

    一.问题起源 LINQ to object在设计时,是配合IEnumerable<T>接口的泛型集合类型使用的,例如字典.数组.List<T>等,但是对于继承了IEnumera ...

  2. WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)

    WebAPI调用笔记   前言 即时通信项目中初次调用OA接口遇到了一些问题,因为本人从业后几乎一直做CS端项目,一个简单的WebAPI调用居然浪费了不少时间,特此记录. 接口描述 首先说明一下,基于 ...

  3. LINQ学习系列-----3.1 查询非泛型集合和多个分组

    一.查询非泛型集合 1.问题起源 LINQ to object在设计时,是配合IEnumerable<T>接口的泛型集合类型使用的,例如字典.数组.List<T>等,但是对于继 ...

  4. 泛型学习第三天——C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合

    定义一个类: public class UserInfo    {        public System.Guid ID { get; set; } public string LoginName ...

  5. 【学习笔记】C#中的泛型和泛型集合

    一.什么是泛型? 泛型是C#语言和公共语言运行库(CLR)中的一个新功能,它将类型参数的概念引入.NET Framework.类型参数使得设计某些类和方法成为可能,例如,通过使用泛型类型参数T,可以大 ...

  6. c#利用泛型集合,为自己偷偷懒。

    有人说"越懒"的程序员进步的越快!其实还挺有道理.亲身体验,从刚出来工作到现在,自己变"懒"了许多,但感觉写出来的代码确有了不少提升.刚开始啊,同样的代码,赋值 ...

  7. 编写高质量代码改善C#程序的157个建议[泛型集合、选择集合、集合的安全]

    前言   软件开发过程中,不可避免会用到集合,C#中的集合表现为数组和若干集合类.不管是数组还是集合类,它们都有各自的优缺点.如何使用好集合是我们在开发过程中必须掌握的技巧.不要小看这些技巧,一旦在开 ...

  8. ConvertHelper与泛型集合

    在机房重构时.我们常常会用到ConvertHelper. 它把从数据库中查询到的dateTable(也是一个暂时表)转化为泛型,然后再填充到DataGridView控件中. ConvertHelper ...

  9. C# 找出泛型集合中的满足一定条件的元素 List.Wher()

    在学习的过程中,发现泛型集合List<T>有一个Where函数可以筛选出满足一定条件的元素,结合Lambda表达式使用特别方便,写出来与大家分享. 1.关于Func<> Fun ...

随机推荐

  1. 手把手教你使用Git管理你的软件代码

    什么是分布式版本控制系统?Git有哪些常用命令?什么是仓库?Git的操作区域包括哪些?Git有哪些常用对象(object)?git rebase和git merge的区别是什么?git reset,g ...

  2. 论文解读(LA-GNN)《Local Augmentation for Graph Neural Networks》

    论文信息 论文标题:Local Augmentation for Graph Neural Networks论文作者:Songtao Liu, Hanze Dong, Lanqing Li, Ting ...

  3. conda install和pip install区别

    conda ≈ pip(python包管理) + virtualenv(虚拟环境) + 非python依赖包管理 级别不一样conda和yum比较类似,可以安装很多库,不限于Python.conda是 ...

  4. 【转载】浅谈大规模k8s集群关于events的那些坑

    原文链接:一流铲屎官二流程序员[浅谈大规模k8s集群关于events的那些坑] 背景 随着k8s集群规模的增加,集群内的object数量也与日俱增,那么events的数量也会伴随其大量增加,那么当用户 ...

  5. Python基础学习笔记_01

    Python的介绍 1989年圣诞节创造,1991年正真出生,目前更新到3.0版本 具有最庞大的"代码库",人称"胶水语言",无所不能 一种跨平台的计算机程序设 ...

  6. 密码学系列之:PKI的证书格式表示X.509

    目录 简介 一个证书的例子 X.509证书的后缀 .pem .cer, .crt, .der .p7b, .p7c .p12 .pfx 证书的层级结构和交叉认证 x.509证书的使用范围 总结 简介 ...

  7. SAP MM- BAPI_PO_CHANGE 更新PO version 信息(version management)

    目的 Version 信息的Complated 字段,自动打勾 实例程序 *&--------------------------------------------------------- ...

  8. python小题目练习(十二)

    题目:如下图所示 代码展示: """Author:mllContent:春节集五福Date:2020-01-17"""import rand ...

  9. this关键字、static关键字、方法的调用

    1.带有static关键字的方法,不可使用this关键字.因为其调用方法为类名.方法名(建议这种方式,调用不需要对象的参与),不存在对象. 2.实例方法调用必须有对象的存在,先创建对象,通过引用.的方 ...

  10. python之多进程and多线程

    图文来自互联网 一.什么是进程和线程 (https://jq.qq.com/?_wv=1027&k=rX9CWKg4) 进程是分配资源的最小单位,线程是系统调度的最小单位. 当应用程序运行时最 ...