C#实现学生管理系统

本文为大家分享了C#实现学生管理系统的具体代码,供大家参考。

添加3个类,分别实现 IComparer接口,实现对Student类的三个字段的排序。

1、学生类:学号、姓名、年龄
2、请选择:1、添加学生信息。2、删除学生信息 2、查询学生信息。
3、重复的学号不能添加。
4、查询学生信息功能中有:1、查询所有(按学号排序)2、查询所有(按姓名排序),2、查询所有(按年龄排序)4、按学号查询(查没有,则打印查无此学生)5、退出

学生类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01
{

    class Student
    {
        public string Num { get; set; }//学号
        public string Name { get; set; }//姓名
        public int Age { get; set; }//年龄
        //创建带参的构造方法
        public Student(string num,string name,int age) {
            this.Num = num;
            this.Name = name;
            this.Age = age;
        }
        //创建无参的构造方法,在本次代码中未使用
        public Student() { }
        //重写了Tostring()的方法将字典中的值输出打印
        public override string ToString()
        {

            return $"学号:{Num}姓名:{Name}年龄:{Age}";
        }
        //创建比较器用于比较
        public int CompareTo(Student other)
        {
            return this.Num.CompareTo(other.Num);
        }

    }
}

工具类(Utility)

工具类中封装了一些方法用于调用,使得主方法中的代码简洁

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01
{
    //判断输入的数字是否正确
    class Utility
    {
        public static int readMenuSelection() {
        int c;
        for (; ; ) {
             c = int.Parse(Console.ReadLine()); 
            if (c != 1 && c != 2 && c != 3 && c != 4) {
                    Console.WriteLine("选择错误,请重新输入:"); 
            } else break;
        }
        return c;
    }
        public static int readMenuSelection2()
        {
            int c;
            for (; ; )
            {
                c = int.Parse(Console.ReadLine());
                if (c != 1 && c != 2 && c != 3 && c != 4&&c!=5)
                {
                    Console.WriteLine("选择错误,请重新输入:");
                }
                else break;
            }
            return c;
        }

        //用于确认选择的输入。该方法从键盘读取‘Y'或'N',并将其作为方法的返回值。
        public static string readConfirmSelection()
        {
            string b;
            for (; ; )
            {
                b = Console.ReadLine();
                //将输入的值转换成小写的字母,用于用户区分大小写的输入
                string c = b.ToLowerInvariant();
                if (c.Equals("y")||c.Equals("n"))
                {
                    break;
                }
                else
                {
                    Console.WriteLine("选择错误,请重新输入:"); 
                }
            }
            return b;
        }
        //创建添加的方法
       public static void AddStudent(Dictionary<string, Student> dc)
        {
            bool Flag = true;
            while (Flag)
            {
                Console.WriteLine("请输入学号:");
                string num = Console.ReadLine();

                Console.WriteLine("请输入姓名:");
                string name = Console.ReadLine();

                Console.WriteLine("请输入年龄:");
                int age = int.Parse(Console.ReadLine());
                Student student = new Student(num, name, age);
                //判断输入的学号是否重复
                if (!dc.ContainsKey(num))
                {
                    dc.Add(student.Num, student);
                    Console.WriteLine("添加成功");
                }
                else
                {
                    Console.WriteLine("已存在,添加失败");
                }

                Console.WriteLine("是否继续添加(Y/N)):");
                string b = Utility.readConfirmSelection();
                if (b.Equals("n"))
                {
                    Flag = false;
                }

            }
            }
        //创建一个输出全部信息的方法
        public static void Print(Dictionary<string,Student> dc) {
            //循环遍历,将 Dictionary<K,V> 类中的值赋值给item
            //需要重写Tostring方法,否则输出的值为该值得命名空间
            foreach (var item in dc.Values)
            {
                Console.WriteLine(item);
            }
        
        }
        //删除学生信息
        public static void DeleteStudent(Dictionary<string, Student> dc) {
            Console.WriteLine("请输入要删除的学生学号");
            string num = Console.ReadLine();
            //判断num值是否在该类中
            if (dc.ContainsKey(num))
            {
                //根据提供的num移除目标
                dc.Remove(num);
                Console.WriteLine("删除成功");
            }
            else
            {
                Console.WriteLine("该学号的学生信息不存在");
            }


        }
        //将排序后的元素输出
        internal static void Print(List<Student> students)
        {
            foreach (var item in students)
            {
                Console.WriteLine(item);
            }
        }
        //按照学号查询,如果没查到返回查无此人
        public static void QueryByNum(Dictionary<string, Student> dc)
        {
            Console.WriteLine("请输入你要查询的学号");
            string num = Console.ReadLine();
            //
            if (dc.ContainsKey(num))
            {
                Console.WriteLine(dc[num]);
            }
            else
            {
                Console.WriteLine("查无此人");
            }
        }
        //按年龄排序
        public static void Age(Dictionary<string, Student> dc)
        {
            List<Student> students = new List<Student>();
            //字典中无序,将字典中的值存放到有序的list集合中
            students.AddRange(dc.Values);
            //调用NameSort的方法
            students.Sort(new AgeSort());
            Utility.Print(students);
        }

        //按名字排序
        public static void NameSort(Dictionary<string, Student> dc)
        {
            List<Student> students = new List<Student>();
            //字典中无序,将字典中的值存放到有序的list集合中
            students.AddRange(dc.Values);
            //调用NameSort的方法
            students.Sort(new NameSort());
            Utility.Print(students);
        }

        //按学号排序
        public static void NumSort(Dictionary<string, Student> dc)
        {
            List<Student> students = new List<Student>();
            //字典中无序,将字典中的值存放到有序的list集合中
            students.AddRange(dc.Values);
            //调用NameSort的方法
            students.Sort(new NumSort());
            Utility.Print(students);

        }
    }
}

比较器

创建三个比较器用于比较排序,使用IComparer<>接口

1.年龄比较器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01
{
    class NameSort : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }
}

2.姓名比较器`

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01
{
    class NameSort : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }
}

3.学号比较器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01
{
    //构造比较器,进行比较
    class NumSort : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.Num.CompareTo(y.Num);
        }
    }
}

主方法中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace _01
{
    class Program
    {
        static void Main(string[] args)
        {
            Student stu1 = new Student("007","李华",12);
            Student stu2 = new Student("004", "张三", 13);
            //Dictionary<数据类型,数据类型> 对象名 = new Dictionary<数据类型,数据类型>();

            Dictionary<string, Student> ha = new Dictionary<string, Student>();
            //将学生类对象添加到字典中
            ha.Add(stu1.Num,stu1);
            ha.Add(stu2.Num,stu2);

            bool Flag = true;
            while (Flag)
            {
            Console.WriteLine("请选择:1、添加学生信息。2、删除学生信息 3、查询学生信息;4.退出");
            int a = Utility.readMenuSelection();
            switch (a)
            {
                case 1:
                    //添加
                    Utility.AddStudent(ha);
                    //输出
                    Utility.Print(ha);
                    break;
                case 2:
                    //删除
                    Utility.DeleteStudent(ha);
                    Utility.Print(ha);
                    break;
                case 3:
                        //查询
                        Console.WriteLine("1、查询所有(按学号排序)2、查询所有(按姓名排序),3、查询所有(按年龄排序)" +
                       "4、按学号查询(查没有,则打印查无此学生)5、退出");
                        int q = Utility.readMenuSelection2();
                        switch (q)
                        {
                            case 1:
                                //查询所有(按学号排序)
                               Utility.NumSort(ha);
                                break;
                            case 2:
                                //查询所有(按姓名排序)
                                Utility.NameSort(ha);
                                break;
                            case 3:
                                //查询所有(按年龄排序)
                                Utility.Age(ha);
                                break;
                            case 4:
                                //按学号查询(查没有,则打印查无此学生)
                                Utility.QueryByNum(ha);
                                break;
                            case 5:
                                //退出
                                break;
                            default:
                                break;
                        }
                        break;
                case 4:
                    Console.WriteLine("确认是否退出(Y/N)");
                    string b = Utility.readConfirmSelection();
                    if (b.Equals("y"))
                    {
                        Flag = false;
                    }
                    //退出
                    break;
                default:
                    break;
            }

            }
            Console.ReadKey();

        }

 
    }
}

以上就是用一些简单的代码完成一个简易的学生管理系统

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程宝库

 前言有时,我们可能需要从 PDF 文档中提取表格数据,例如,当PDF发票的表格中存储了一些有用的信息,需要提取数据以进行进一步分析时。在这篇文章中,我将介绍如何使用Spire. ...