C#基础第六天-作业答案-利用面向对象的思想去实现名片
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace day1 { class PersonCard { private string name; public string Name //姓名 { get { return name; } set { name = value; } } private int age; //年龄 public int Age { get { return age; } set { if (value > 0 && value < 100) { age = value; } else { age = 18; } } } private string sex; //性别 public string Sex { get { return sex; } set { if (value == "男" || value == "女") { sex = value; } else { sex = "男"; } } } private string hobby; //爱好 public string Hobby { get { return hobby; } set { hobby = value; } } private string phone; //电话 public string Phone { get { return phone; } set { phone = value; } } private string state; //状态 public string State { get { return state; } set { state = value; } } } } class Program { static void Main(string[] args) { string NAME = string.Empty; int AGE = 0; string SEX = string.Empty; string HOBBY = string.Empty; string PHONE = string.Empty; string STATE = string.Empty; //(1.新增 2.修改) int i = 0; //Key值 PersonCard personcard; //人员信息类 Dictionary<string, PersonCard> PersonCardList = new Dictionary<string, PersonCard>(); //人员信息表 Dictionary<string, PersonCard> PersonCardListCopy = new Dictionary<string, PersonCard>(); //复制人员信息表 while (true) { Console.WriteLine("请输入操作方案:1.新增;2.查询全部;3.单条查询;4.修改;5.删除;6.查询已删除信息"); string type = Console.ReadLine(); //接收操作类型 if (type == "1") //新增 { Console.WriteLine("姓名:"); NAME = Console.ReadLine(); Console.WriteLine("年龄:"); AGE = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("性别:"); SEX = Console.ReadLine(); Console.WriteLine("爱好:"); HOBBY = Console.ReadLine(); Console.WriteLine("电话:"); PHONE = Console.ReadLine(); personcard = new PersonCard(); //实例化人员信息类 personcard.Name = NAME; personcard.Age = AGE; personcard.Sex = SEX; personcard.Hobby = HOBBY; personcard.Phone = PHONE; personcard.State = "1"; PersonCardList.Add(i.ToString(), personcard); //集合添加一行 i++; } if (type == "2") //查询全部 { foreach (var item in PersonCardList) { Console.WriteLine("姓名:" + item.Value.Name + " " + "年龄:" + item.Value.Age + " " + "性别:" + item.Value.Sex + " " + "爱好:" + item.Value.Hobby + " " + "联系方式:" + item.Value.Phone + "状态:" + item.Value.State); } } if (type == "3") //单条查询 { Console.WriteLine("请输入您要查询的人员信息"); string NAMEFind = Console.ReadLine(); if (PersonCardList.Values.Where(o => o.Name == NAMEFind).Count() > 0) //lingq 人名存在数大于零 { IEnumerable<KeyValuePair<string, PersonCard>> abc = PersonCardList.Where(o => o.Value.Name == NAMEFind); foreach (KeyValuePair<string, PersonCard> kk in abc) { Console.WriteLine("姓名:" + kk.Value.Name + " " + "年龄:" + kk.Value.Age + " " + "性别:" + kk.Value.Sex + " " + "爱好:" + kk.Value.Hobby + " " + "联系方式:" + kk.Value.Phone + "状态:" + kk.Value.State); } } else { Console.WriteLine("对不起,没有您要查询的人员信息"); } } if (type == "4") //修改 { Console.WriteLine("请输入您要修改的人员"); string NAMEUpdate = Console.ReadLine(); if (PersonCardList.Values.Where(o => o.Name == NAMEUpdate).Count() > 0) { IEnumerable<KeyValuePair<string, PersonCard>> abc = PersonCardList.Where(o => o.Value.Name == NAMEUpdate); Console.WriteLine("请输入您要修改的信息(姓名,年龄,性别,爱好,电话)"); string message = Console.ReadLine(); Console.WriteLine("请输入修改后的内容"); string Message = Console.ReadLine(); foreach (KeyValuePair<string, PersonCard> kk in abc) { if (message == "姓名") { kk.Value.Name = Message; kk.Value.State = "2"; } if (message == "年龄") { kk.Value.Age = Convert.ToInt32(Message); kk.Value.State = "2"; } if (message == "性别") { kk.Value.Sex = Message; kk.Value.State = "2"; } if (message == "爱好") { kk.Value.Hobby = Message; kk.Value.State = "2"; } if (message == "电话") { kk.Value.Phone = Message; kk.Value.State = "2"; } Console.WriteLine("姓名:" + kk.Value.Name + " " + "年龄:" + kk.Value.Age + " " + "性别:" + kk.Value.Sex + " " + "爱好:" + kk.Value.Hobby + " " + "联系方式:" + kk.Value.Phone + "状态:" + kk.Value.State); } } else { Console.WriteLine("对不起,没有您要修改的人员"); } } if (type == "5") //删除信息 { Console.WriteLine("请输入您要删除的人员"); string NAMEDelete = Console.ReadLine(); if (PersonCardList.Values.Where(o => o.Name == NAMEDelete).Count() > 0) //lingq 人名存在数大于零 { List<KeyValuePair<string, PersonCard>> abc = PersonCardList.Where(o => o.Value.Name == NAMEDelete).ToList(); foreach (KeyValuePair<string, PersonCard> kk in abc) { PersonCardListCopy.Add(kk.Key, kk.Value); PersonCardList.Remove(kk.Key); } foreach (var item in PersonCardList) { Console.WriteLine("姓名:" + item.Value.Name + " " + "年龄:" + item.Value.Age + " " + "性别:" + item.Value.Sex + " " + "爱好:" + item.Value.Hobby + " " + "联系方式:" + item.Value.Phone + "状态:" + item.Value.State); } } else { Console.WriteLine("对不起,没有您要删除的人员"); } } if (type == "6") //删除信息 { foreach (var item in PersonCardListCopy) { Console.WriteLine("姓名:" + item.Value.Name + " " + "年龄:" + item.Value.Age + " " + "性别:" + item.Value.Sex + " " + "爱好:" + item.Value.Hobby + " " + "联系方式:" + item.Value.Phone + "状态:" + item.Value.State); } } Console.ReadKey(); } } }
本系列教程:
C#基础总结之八面向对象知识点总结-继承与多态-接口-http://www.cnblogs.com/spring_wang/p/6113531.html
C#基础总结之七面向对象知识点总结1http://www.cnblogs.com/spring_wang/p/6113526.html
C#基础总结之六 DataTable (临时表/数据源) 和Datatable 名片练习http://www.cnblogs.com/spring_wang/p/6113520.html
C#基础总结之五Dictionary<string, string[]>和while循环http://www.cnblogs.com/spring_wang/p/6113514.html
C#基础总结之四List-Hashtable-冒泡排序http://www.cnblogs.com/spring_wang/p/6113504.html
C#基础总结之三循环控制-for-数组-乘法表-arraylisthttp://www.cnblogs.com/spring_wang/p/6113496.html
C#基础总结之二循环控制-运算符http://www.cnblogs.com/spring_wang/p/6113484.html
C#基础总结之一变量常量-if嵌套语句-witch结构-类型转换http://www.cnblogs.com/spring_wang/p/6113476.html
C#基础课程之六(临时表)DataTable使用方法http://www.cnblogs.com/spring_wang/p/6113454.html
C#基础课程之五集合(HashTable,Dictionary)http://www.cnblogs.com/spring_wang/p/6113404.html
C#基础课程之四集合(ArrayList、List<泛型>)http://www.cnblogs.com/spring_wang/p/6113396.html
C#基础课程之三循环语句http://www.cnblogs.com/spring_wang/p/6113383.html
C#基础课程之二变量常量及流程控制http://www.cnblogs.com/spring_wang/p/6113372.html
C#基础课程之一注释和控制台、一些常识http://www.cnblogs.com/spring_wang/p/6113361.html
C#基础第九天-作业答案-储蓄账户(SavingAccount)和信用账户(CreditAccount) http://www.cnblogs.com/spring_wang/p/6113291.html
C#基础第九天-作业-储蓄账户(SavingAccount)和信用账户(CreditAccount) http://www.cnblogs.com/spring_wang/p/6113285.html
C#基础第八天-作业答案-设计类-面向对象方式实现两个帐户之间转账http://www.cnblogs.com/spring_wang/p/6113274.html
C#基础第八天-作业-设计类-面向对象方式实现两个帐户之间转账http://www.cnblogs.com/spring_wang/p/6113258.html
C#基础第七天-作业答案-利用面向对象的思想去实现名片-动态添加http://www.cnblogs.com/spring_wang/p/6113232.html
C#基础第七天-作业-利用面向对象的思想去实现名片-动态添加http://www.cnblogs.com/spring_wang/p/6113224.html
C#基础第六天-作业-利用面向对象的思想去实现名片http://www.cnblogs.com/spring_wang/p/6113028.html
C#基础第六天-作业答案-利用面向对象的思想去实现名片http://www.cnblogs.com/spring_wang/p/6113033.html
C#基础第五天-作业答案-用DataTable制作名片集http://www.cnblogs.com/spring_wang/p/6113022.html
C#基础第五天-作业-用DataTable制作名片集http://www.cnblogs.com/spring_wang/p/6113013.html
C#基础第四天-作业答案-Hashtable-list<KeyValuePair>泛型实现名片http://www.cnblogs.com/spring_wang/p/6113005.html
C#基础第四天-作业-Hashtable-list<KeyValuePair>泛型实现名片http://www.cnblogs.com/spring_wang/p/6113000.html
C#基础第三天-作业答案-集合-冒泡排序-模拟名片http://www.cnblogs.com/spring_wang/p/6112888.html
C#基础第三天-作业-集合-冒泡排序-模拟名片http://www.cnblogs.com/spring_wang/p/6112885.html
C#基础第二天-作业答案-九九乘法表-打印星星http://www.cnblogs.com/spring_wang/p/6112881.html
C#基础第二天-作业-九九乘法表-打印星星http://www.cnblogs.com/spring_wang/p/6112875.html
C#基础第一天-作业答案http://www.cnblogs.com/spring_wang/p/6112872.html
C#基础第一天-作业http://www.cnblogs.com/spring_wang/p/6112867.html
C#-string.Format对C#字符串格式化http://www.cnblogs.com/spring_wang/p/6077098.html
作者: 王春天 出处: http://www.cnblogs.com/spring_wang/ Email: spring_best@yeah.net QQ交流:903639067
QQ群:322581894 关于作者: 大连天翼信息科技有限公司 技术总监。 SNF快速开发平台 创始人。应用平台架构师、IT规划咨询专家、业务流程设计专家。 专注于快速开发平台的开发、代码生成器。同时专注于微软平台项目架构、管理和企业解决方案,多年项目开发与管理经验,精通DotNet系列技术Vue、.NetCore、MVC、Webapi、C#、WinForm等,DB(SqlServer、Oracle等)技术,移动端开发。熟悉Java、VB及PB开发语言。在面向对象、面向服务以及数据库领域有一定的造诣。现从事项目实施、开发、架构等工作。并从事用友软件产品U8、U9、PLM 客开工作。 如有问题或建议,请多多赐教! 本文版权归作者和CNBLOGS博客共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过邮箱或QQ 联系我,非常感谢。