C#中集合的練習(arraylist/list)

利⽤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("------------------------------------------------------");


        }
    }
}

有疑問的可以私信博主。
點個關注,給個讚唄!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章