C#基础第六天-作业答案-利用面向对象的思想去实现名片

Posted snf全栈工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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#基础第六天-作业答案-利用面向对象的思想去实现名片的主要内容,如果未能解决你的问题,请参考以下文章

python运维开发之第六天

IT十八掌作业_java基础第六天_接口与适配器模式多态内部类

Python 基础学习第十六天

我的Python成长之路---第六天---Python基础(20)---2016年2月20日(晴)

第六天

java 第六章 面向对象基础