首页 > 代码库 > 面向对象 —— 教师职工表练习

面向对象 —— 教师职工表练习


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

让用户输入教师个数

一遍一遍的输入每个教师的信息
教师编号如果不是T开头,并且后面是3位数字的话,《暂无》
如果这个编号已被占用,那么自动+1

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

教师生日

教师工资,不能低于3000块


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

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

 

一 teacher 类

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    public 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 = value;
                else
                    _Tname = "<暂无>";
            }
        }
        private DateTime _Tbirthday;

        public DateTime Tbirthday
        {
            get { return _Tbirthday; }
            set { _Tbirthday = value; }
        }

        public int Tage
        {
            get
            {
                return DateTime.Now.Year - _Tbirthday.Year;
            }
        }

        private decimal _Tmoney;

        public decimal Tmoney
        {
            get { return _Tmoney; }
            set { _Tmoney = value; }
        }

    }
}
View Code

 

二 方法

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    public class Test1
    {
        public bool NoIsOK(string s)
        {
            bool end = true; //默认是正确的格式

            if (s.Length != 4) //如果长度不是4,那么格式错误
                end = false;
            else //如果是4位那么继续判断
            {
                if (s.Substring(0, 1) != "T") //判断第一位是否是T
                {
                    end = false;
                }
                else //第一位已经是T,只需要判断后三位是否是数字
                {
                    string ss = s.Substring(1, 3);
                    try
                    {
                        Convert.ToInt32(ss);
                    }
                    catch { end = false; }
                }
            }

            return end;
        }

        public string CreateTno(string s, List<Teacher> list)
        {
            List<int> ilist = new List<int>();
            string end = "";
            bool has = false;

            foreach (Teacher t in list)
            {
                ilist.Add(Convert.ToInt32(t.Tno.Substring(1, 3)));

                if (t.Tno == s)
                    has = true;
            }

            if (has)
            {
                for (int i = 0; i < ilist.Count - 1; i++)
                {
                    for (int j = i + 1; j < ilist.Count; j++)
                    {
                        if (ilist[i] < ilist[j])
                        {
                            int zhong = ilist[i];
                            ilist[i] = ilist[j];
                            ilist[j] = zhong;
                        }
                    }
                }

                string ssss = (ilist[0] + 1).ToString("000");

                end = "T" + ssss;
            }
            else
                end = s;

            return end;
        }

        public decimal MoneyAvg(List<Teacher> list)
        {
            decimal sum = 0;
            foreach (Teacher t in list)
            {
                sum += t.Tmoney;
            }

            return sum / list.Count;
        }

    }
}
View Code

 

三 对象操作

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Test1 t1 = new Test1();
            List<Teacher> tlist = new List<Teacher>();

            Console.Write("请输入教师个数:");
            int tcount = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < tcount; i++)
            {
                Teacher t = new Teacher();

                while (true)
                {
                    Console.Write("请输入第" + (i + 1) + "个教师的编号:");
                    t.Tno = Console.ReadLine();

                    bool tnook = t1.NoIsOK(t.Tno);

                    if (tnook)
                    {
                        t.Tno = t1.CreateTno(t.Tno, tlist);
                        break;
                    }
                    else
                        Console.WriteLine("编号格式不正确!");
                }

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

                tlist.Add(t);
            }

            Console.WriteLine("=====教师信息======");
            foreach (Teacher t in tlist)
            {
                Console.WriteLine(t.Tno + " | " + t.Tname + " | " + t.Tbirthday.ToString("yyyy年MM月dd日") + " | " + t.Tage + " | " + t.Tmoney);
            }
            Console.WriteLine("=====平均工资======");

            Console.WriteLine(t1.MoneyAvg(tlist));

            Console.ReadLine();
        }
    }
}
View Code

 

面向对象 —— 教师职工表练习