面向对象 封装练习题

教师有这样几个属性
教师编号
教师姓名
教师生日
教师工资

让用户输入教师个数

一遍一遍的输入每个教师的信息


教师编号如果不是T开头,并且后面是3位数字的话,
如果这个编号已被占用,那么自动+1

如果不对,一直到以T开头,后三位是数字为止

教师姓名如果为空,<暂无>

教师生日

教师工资,不能低于3000块


==========教师信息==========
T001 张三 2000-1-1 17 ¥3000.12

==============平均工资==============
4500.23

  class Teacher
    {
        //教师编号
        private string _Tno;

        public string Tno
        {
            get { return _Tno; }
            set { _Tno = value; }
        }

        //教师姓名
        private string _Tname;

        public string Tname
        {
            get { return _Tname; }
            set
            {
                if (value == "")
                    _Tname = "<暂无>";
            }
        }

        //教师生日
        private DateTime _Tbirthday;

        public DateTime Tbirthday
        {
            get { return _Tbirthday; }
            set { _Tbirthday = value; }
        }
        public int Age
        {
            get
            {
                int age = DateTime.Now.Year - _Tbirthday.Year;
                return age;
            }
        }

        //教师工资
        private decimal _Tmoney;

        public decimal Tmoney
        {
            get { return _Tmoney; }
            set
            {
                if (value < 3000)
                    value = 3000;
                _Tmoney = value;
            }
        }


    }
View Code

方法

  class Math
    {
        public bool IsOk(string a)
        {
            //教师编号如果不是T开头,并且后面是3位数字的话,《暂无》
            bool Isok = true;
            if (a.Length != 4)
                Isok = false;
            else
            {
                if (!a.StartsWith("T"))
                    Isok = false;
                else
                {
                    try
                    {
                        int isshu = Convert.ToInt32(a.Substring(1, 3));
                        Isok = true;
                    }
                    catch
                    {
                        Isok = false;
                    }
                }

            }

            return Isok;
        }
        //如果这个编号已被占用,那么自动+1
        public string CreatTno(string a, List<Teacher> t)
        {
            string end = "";
       
            bool ss = false;
            List<int> shu = new List<int>();
            foreach (Teacher i in t)
            {
         
                shu.Add(Convert.ToInt32 (i.Tno.Substring(1,3)));
                if (a == i.Tno)
                    ss = true;
            }
            if (ss)
            {
                for (int i = 0; i < shu.Count ; i++)
                {
                    for (int j = i + 1; j < shu.Count; i++)
                    {
                        if (shu[i] < shu[j])
                        {
                            int zhong = shu[i];
                            shu[i] = shu[j];
                            shu[j] = zhong;
                        }
                    }
                }
 
                string aa = (shu[0]+1).ToString("000");
                end = "T" + aa;
            }
            else
                end = a;
            return end;
        }



        public decimal avg( List<Teacher> t)
        {
            decimal sum = 0;
            int count = 0;
            for (int i = 0; i < t.Count; i++)
            {
               sum = sum + t[i].Tmoney;
               count++;
            }
            decimal avg = sum / count;
            return avg;
        }
    }
View Code

开始使用

    class Program
    {
        static void Main(string[] args)
        {
            //让用户输入教师个数
            Console.Write("让用户输入教师个数:");
            int shu = Convert.ToInt32(Console.ReadLine());
            Math b = new Math();
            List<Teacher> t = new List<Teacher>();
            for (int i = 0; i < shu; i++)
            {
                Teacher a = new Teacher();


                while (true)
                {
                    //教师编号
                    Console.Write("请输入第" + (i + 1) + "个教师编号:");
                    a.Tno = Console.ReadLine();
                    bool ss = b.IsOk(a.Tno);
                    if (ss)
                    {
                      a.Tno=  b.CreatTno(a.Tno, t);
                        break;
                    }
                    else
                    { Console.WriteLine("格式错误!!"); }


                }
                //教师姓名
                Console.Write("请输入第" + (i + 1) + "个教师姓名:");
                a.Tname = Console.ReadLine();
                //教师生日
                Console.Write("请输入第" + (i + 1) + "个教师生日:");
                a.Tbirthday = Convert.ToDateTime(Console.ReadLine());
                //教师工资
                Console.Write("请输入第" + (i + 1) + "个教师工资:");
                a.Tmoney = Convert.ToDecimal(Console.ReadLine());
                t.Add(a);
            }

            Console.WriteLine("=====================教师信息=========================");
            foreach (Teacher i in t)
            { Console.WriteLine(i.Tno+"	"+i.Tname+"	"+i.Tbirthday+"	"+i.Age+"	"+i.Tmoney); }

            Console.WriteLine(b.avg(t));
            Console.ReadLine();
        }
    }
View Code
原文地址:https://www.cnblogs.com/zhangwei99com/p/6716766.html