The examples in this article share with you the specific code of C# implementing the student management system for your reference. The specific content is as follows
Add 3 classes to implement the IComparer interface, and implement the sorting of the three fields of the Student class.
1. Student category: student number, name, age
2. Please select: 1. Add student information. 2. Delete student information 2. Query student information.
3. Repeated student numbers cannot be added.
4. The functions of querying student information include: 1. Query all (sorted by student number) 2. Query all (sorted by name), 2. Query all (sorted by age) 4. Query by student number (sorted by student number) (check without, print without this student) 5. Exit
Student category
using System; using ; using ; using ; using ; namespace _01 { class Student { public string Num { get; set; }//Student number public string Name { get; set; }//Name public int Age { get; set; }//age //Create a constructor with parameters public Student(string num,string name,int age) { = num; = name; = age; } //Create a constructor without parameters, not used in this code public Student() { } //Rewrite the Tostring() method to output the value in the dictionary to print the public override string ToString() { return $"Student ID:{Num}Name:{Name}age:{Age}"; } //Create a comparator for comparison public int CompareTo(Student other) { return (); } } }
Tool class (Utility)
Some methods are encapsulated in the tool class for calling, making the code in the main method simple
using System; using ; using ; using ; using ; namespace _01 { //Judge whether the entered number is correct class Utility { public static int readMenuSelection() { int c; for (; ; ) { c = (()); if (c != 1 && c != 2 && c != 3 && c != 4) { ("Select error, please re-enter:"); } else break; } return c; } public static int readMenuSelection2() { int c; for (; ; ) { c = (()); if (c != 1 && c != 2 && c != 3 && c != 4&&c!=5) { ("Select error, please re-enter:"); } else break; } return c; } // Used to confirm the selection input. This method reads 'Y' or 'N' from the keyboard and takes it as the return value of the method. public static string readConfirmSelection() { string b; for (; ; ) { b = (); //Convert the input value to lowercase letters for user case sensitive input string c = (); if (("y")||("n")) { break; } else { ("Select error, please re-enter:"); } } return b; } //Create the added method public static void AddStudent(Dictionary<string, Student> dc) { bool Flag = true; while (Flag) { ("Please enter the student number:"); string num = (); ("Please enter your name:"); string name = (); ("Please enter age:"); int age = (()); Student student = new Student(num, name, age); //Judge whether the entered student number is duplicated if (!(num)) { (, student); ("Added successfully"); } else { ("Already existed, failed to add"); } ("Whether to continue adding (Y/N)):"); string b = (); if (("n")) { Flag = false; } } } //Create a method to output all information public static void Print(Dictionary<string,Student> dc) { //Loop traversal, assign the value in the Dictionary<K,V> class to item //The Tostring method needs to be overridden, otherwise the output value is the value of this value. foreach (var item in ) { (item); } } //Delete student information public static void DeleteStudent(Dictionary<string, Student> dc) { ("Please enter the student number you want to delete"); string num = (); //Judge whether the num value is in this class if ((num)) { //Remove target according to the provided num (num); ("Delete successfully"); } else { ("The student information for this student number does not exist"); } } //Output the sorted elements internal static void Print(List<Student> students) { foreach (var item in students) { (item); } } //Check according to the student number, if you don't find it, you will not be able to return to check if you don't have this person public static void QueryByNum(Dictionary<string, Student> dc) { ("Please enter the student number you want to query"); string num = (); // if ((num)) { (dc[num]); } else { ("No such person"); } } //Sorted by age public static void Age(Dictionary<string, Student> dc) { List<Student> students = new List<Student>(); //Unorder in the dictionary, store the values in the dictionary into an ordered list collection (); //Calling the NameSort method (new AgeSort()); (students); } //Sort by name public static void NameSort(Dictionary<string, Student> dc) { List<Student> students = new List<Student>(); //Unorder in the dictionary, store the values in the dictionary into an ordered list collection (); //Calling the NameSort method (new NameSort()); (students); } //Sort by student number public static void NumSort(Dictionary<string, Student> dc) { List<Student> students = new List<Student>(); //Unorder in the dictionary, store the values in the dictionary into an ordered list collection (); //Calling the NameSort method (new NumSort()); (students); } } }
Comparator
Create three comparators for comparison sorting, using the IComparer<> interface
1. Age comparator
using System; using ; using ; using ; using ; namespace _01 { class NameSort : IComparer<Student> { public int Compare(Student x, Student y) { return (); } } }
2. Name comparator`
using System; using ; using ; using ; using ; namespace _01 { class NameSort : IComparer<Student> { public int Compare(Student x, Student y) { return (); } } }
3. Student ID Comparator
using System; using ; using ; using ; using ; namespace _01 { //Construct the comparator and make comparison class NumSort : IComparer<Student> { public int Compare(Student x, Student y) { return (); } } }
Code in the main method
using System; using ; using ; using ; using ; using ; namespace _01 { class Program { static void Main(string[] args) { Student stu1 = new Student("007","Li Hua",12); Student stu2 = new Student("004", "Zhang San", 13); //Dictionary<data type, data type> Object name = new Dictionary<data type, data type>(); Dictionary<string, Student> ha = new Dictionary<string, Student>(); //Add student class object to dictionary (,stu1); (,stu2); bool Flag = true; while (Flag) { ("Please select: 1. Add student information. 2. Delete student information 3. Query student information; 4. Exit"); int a = (); switch (a) { case 1: //Add to (ha); //Output (ha); break; case 2: //delete (ha); (ha); break; case 3: //Inquiry ("1. Query all (sorted by student number) 2. Query all (sorted by name), 3. Query all (sorted by age)" + "4. Query by student number (check without, print without this student) 5. Exit"); int q = Utility.readMenuSelection2(); switch (q) { case 1: //Query all (sorted by student number) (ha); break; case 2: //Query all (sorted by name) (ha); break; case 3: //Query all (sorted by age) (ha); break; case 4: //Check by student number (If you check it, you will not be able to check this student if you print it) (ha); break; case 5: //quit break; default: break; } break; case 4: ("Confirm whether to exit(Y/N)"); string b = (); if (("y")) { Flag = false; } //quit break; default: break; } } (); } } }
The above is to use some simple code to complete a simple student management system
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.