C#中集合的练习

利⽤ArrayList或者是List<>做⼀个⼩型的学⽣管理系统

  • 添加学⽣
  • 移除学⽣【根据学号移除】
  • 查询学⽣【根据姓名查询学⽣、根据性别查询学⽣、根据年龄查询学⽣】
  • 按身⾼排序学⽣
  • 按年龄排序学⽣
  • 按成绩排序学⽣【从⾼到低】(成绩【数语英】【数学同分⽐语⽂】【语⽂还同分⽐英 语】)

下面是代码(偷懒全写在一个文件里):

using System;
using System.Collections;
using System.Collections.Generic;

namespace practice
{
    /// <summary>
    /// 学生类
    /// </summary>
    class Student
    {
        /// <summary>
        /// 学生姓名
        /// </summary>
        public string name;

        /// <summary>
        /// 学生学号
        /// </summary>
        public string id;

        /// <summary>
        /// 性别
        /// </summary>
        public string gender;

        /// <summary>
        /// 身高
        /// </summary>
        public int height;

        /// <summary>
        /// 年龄
        /// </summary>
        public int age;

        /// <summary>
        /// 总分
        /// </summary>
        public float score;

        /// <summary>
        /// 数学成绩
        /// </summary>
        public float mathScore;

        /// <summary>
        /// 语文成绩
        /// </summary>
        public float chineseScore;

        /// <summary>
        /// 英语成绩
        /// </summary>
        public float englishScore;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        /// <param name="gender"></param>
        /// <param name="height"></param>
        /// <param name="age"></param>
        /// <param name="mathScore"></param>
        /// <param name="chineseScore"></param>
        /// <param name="englishScore"></param>
        public Student(string name, string id, string gender, int height, int age, float mathScore, float chineseScore, float englishScore)
        {
            this.name = name;
            this.id = id;
            this.gender = gender;
            this.height = height;
            this.age = age;
            this.mathScore = mathScore;
            this.chineseScore = chineseScore;
            this.englishScore = englishScore;

            score = mathScore + chineseScore + englishScore;
        }

    }

    /// <summary>
    /// 学生信息管理系统类
    /// </summary>
    class SystemOfStudentInfo
    {
        private SystemOfStudentInfo() { }

        private static SystemOfStudentInfo system;

        /// <summary>
        /// 建立保存学生信息的集合students
        /// </summary>
        public static List<Student> students = new List<Student>();

        /// <summary>
        /// 单例
        /// </summary>
        public static SystemOfStudentInfo System
        {
            get
            {
                if (system == null)
                {
                    system = new SystemOfStudentInfo();
                }
                return system;
            }
        }

        /// <summary>
        /// 添加学生信息
        /// </summary>
        /// <param name="student"></param>
        public void AddStudent(Student student)
        {
            if (student == null)
            {
                Console.WriteLine("错误的传入参数!");
                return;
            }
            students.Add(student);
        }

        /// <summary>
        /// 删除学生信息
        /// </summary>
        /// <param name="student"></param>
        public void DeleteStudent(Student student)
        {
            if (student == null)
            {
                Console.WriteLine("错误的传入参数!");
                return;
            }
            for (int i = 0; i < students.Count; i++)
            {
                if (students[i].id.Equals(student.id))
                {
                    students.Remove(student);
                }
            }
        }

        /// <summary>
        /// 打印所有传入集合中的学生信息
        /// </summary>
        public void PrintAllStudent(List<Student> list)
        {
            foreach (var item in list)
            {
                Console.WriteLine($"姓名:{item.name},年龄:{item.age},学号:{item.id},性别:{item.gender},身高:{item.height}," +
                    $"总分:{item.score},数学:{item.mathScore},语文:{item.chineseScore},英语:{item.englishScore}");
            }
        }

        /// <summary>
        /// 根据姓名查询学生方法
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<Student> SelectStudentByName(string name)
        {
            if (name == null)
            {
                Console.WriteLine("错误的传入参数!");
                return null;
            }
            //定义集合保存查出的信息
            List<Student> list = new List<Student>();

            foreach (var item in students)
            {
                if (item.name.Equals(name))
                {
                    list.Add(item);
                }
            }

            return list;

        }

        /// <summary>
        /// 根据年龄查询学生
        /// </summary>
        /// <param name="age"></param>
        /// <returns></returns>
        public List<Student> SelectStudentByAge(int age)
        {
            //健壮性待做

            List<Student> list = new List<Student>();

            foreach (var item in students)
            {
                if (item.age == age)
                {
                    list.Add(item);
                }
            }

            return list;

        }

        /// <summary>
        /// 根据性别查询学生
        /// </summary>
        /// <param name="gender"></param>
        /// <returns></returns>
        public List<Student> SelectStudentByGender(string gender)
        {
            //健壮性待做

            List<Student> list = new List<Student>();

            foreach (var item in students)
            {
                if (item.gender.Equals(gender))
                {
                    list.Add(item);
                }
            }

            return list;
        }

    }

    /// <summary>
    /// 根据身高排序
    /// </summary>
    class StudentSortByHeight : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {

            if (x == null)
            {
                return -1;
            }
            if (y == null)
            {
                return 1;
            }

            int a = x.height;
            int b = y.height;

            if (a < b)
            {
                return -1;
            }
            else if (a > b)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

    }

    /// <summary>
    /// 根据年龄排序
    /// </summary>
    class StudentSortByAge : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            if (x == null)
            {
                return -1;
            }
            if (y == null)
            {
                return 1;
            }

            int a = x.age;
            int b = y.age;

            if (a < b)
            {
                return -1;
            }
            else if (a > b)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }

    /// <summary>
    /// 根据成绩排序
    /// </summary>
    class StudentSortByScore : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            if (x == null)
            {
                return 1;
            }
            if (y == null)
            {
                return -1;
            }

            float a = x.score;
            float b = y.score;

            if (a < b)
            {
                return 11;
            }
            else if (a > b)
            {
                return -1;
            }
            else
            {
                a = x.mathScore;
                b = y.mathScore;

                if (a < b)
                {
                    return 1;
                }
                else if (a > b)
                {
                    return -1;
                }
                else
                {
                    a = x.chineseScore;
                    b = y.chineseScore;

                    if (a < b)
                    {
                        return 1;
                    }
                    else if (a > b)
                    {
                        return -1;
                    }
                    else
                    {
                        a = x.englishScore;
                        b = y.englishScore;
                        if (a < b)
                        {
                            return 1;
                        }
                        else if (a > b)
                        {
                            return -1;
                        }
                        else
                        {
                            return 0;
                        }
                    }
                }
                
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
		//以下全部是测试
            Student zhangsan = new Student("张三", "S0001",  "男", 170, 22, 90f, 80f, 59f);
            Student zhangsan02 = new Student("张三", "S0006",  "女", 190, 21, 99f, 90f, 59f);
            Student lisi = new Student("李四", "S0002",  "男", 173, 23, 90f, 79f, 60f);
            Student wangwu = new Student("王五", "S0003",  "男", 180, 22, 50f, 99f, 89f);
            Student zhaoliu = new Student("赵六", "S0004",  "女", 160, 24, 60f, 100f, 100f);
            Student shanchu = new Student("一会就被删除的人不配有名字", "S0005",  "女", 160, 24, 60f, 100f, 100f);


            SystemOfStudentInfo.System.AddStudent(zhangsan);
            SystemOfStudentInfo.System.AddStudent(zhangsan02);
            SystemOfStudentInfo.System.AddStudent(lisi);
            SystemOfStudentInfo.System.AddStudent(wangwu);
            SystemOfStudentInfo.System.AddStudent(zhaoliu);
            SystemOfStudentInfo.System.AddStudent(shanchu);

            Console.WriteLine("执行完添加操作后:");
            Console.WriteLine("------------------------------------------------------");
            SystemOfStudentInfo.System.PrintAllStudent(SystemOfStudentInfo.students);
            Console.WriteLine("------------------------------------------------------");

            SystemOfStudentInfo.System.DeleteStudent(shanchu);
            Console.WriteLine("执行完删除操作后:");
            Console.WriteLine("------------------------------------------------------");
            SystemOfStudentInfo.System.PrintAllStudent(SystemOfStudentInfo.students);
            Console.WriteLine("------------------------------------------------------");

            List<Student> list = new List<Student>();

            list = SystemOfStudentInfo.System.SelectStudentByName("张三");
            Console.WriteLine("按姓名查找的结果:");
            Console.WriteLine("------------------------------------------------------");
            SystemOfStudentInfo.System.PrintAllStudent(list);
            Console.WriteLine("------------------------------------------------------");


            list = SystemOfStudentInfo.System.SelectStudentByAge(22);
            Console.WriteLine("按年龄查找的结果:");
            Console.WriteLine("------------------------------------------------------");
            SystemOfStudentInfo.System.PrintAllStudent(list);
            Console.WriteLine("------------------------------------------------------");


            list = SystemOfStudentInfo.System.SelectStudentByGender("男");
            Console.WriteLine("按性别查找的结果:");
            Console.WriteLine("------------------------------------------------------");
            SystemOfStudentInfo.System.PrintAllStudent(list);
            Console.WriteLine("------------------------------------------------------");


            SystemOfStudentInfo.students.Sort(new StudentSortByHeight());
            Console.WriteLine("按身高排序的结果(从矮到高):");
            Console.WriteLine("------------------------------------------------------");
            SystemOfStudentInfo.System.PrintAllStudent(SystemOfStudentInfo.students);
            Console.WriteLine("------------------------------------------------------");


            SystemOfStudentInfo.students.Sort(new StudentSortByAge());
            Console.WriteLine("按年龄排序的结果(从小到大):");
            Console.WriteLine("------------------------------------------------------");
            SystemOfStudentInfo.System.PrintAllStudent(SystemOfStudentInfo.students);
            Console.WriteLine("------------------------------------------------------");


            SystemOfStudentInfo.students.Sort(new StudentSortByScore());
            Console.WriteLine("按分数排序的结果(从高到低):");
            Console.WriteLine("------------------------------------------------------");
            SystemOfStudentInfo.System.PrintAllStudent(SystemOfStudentInfo.students);
            Console.WriteLine("------------------------------------------------------");


        }
    }
}

有疑问的可以私信博主。
点个关注,给个赞呗!

发布了32 篇原创文章 · 获赞 15 · 访问量 5287

猜你喜欢

转载自blog.csdn.net/maybe_ice/article/details/104544808