C#實現學生管理系統(tǒng)
本文實例為大家分享了C#實現學生管理系統(tǒng)的具體代碼,供大家參考,具體內容如下
添加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; }//年齡 ?? ??? ?//創(chuàng)建帶參的構造方法 ? ? ? ? public Student(string num,string name,int age) { ? ? ? ? ? ? this.Num = num; ? ? ? ? ? ? this.Name = name; ? ? ? ? ? ? this.Age = age; ? ? ? ? } ?? ??? ?//創(chuàng)建無參的構造方法,在本次代碼中未使用 ? ? ? ? public Student() { } ? ? ? ? //重寫了Tostring()的方法將字典中的值輸出打印 ? ? ? ? public override string ToString() ? ? ? ? { ? ? ? ? ? ? return $"學號:{Num}姓名:{Name}年齡:{Age}"; ? ? ? ? } ?? ??? ?//創(chuàng)建比較器用于比較 ? ? ? ? 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(); ? ? ? ? ? ? ? ? //將輸入的值轉換成小寫的字母,用于用戶區(qū)分大小寫的輸入 ? ? ? ? ? ? ? ? string c = b.ToLowerInvariant(); ? ? ? ? ? ? ? ? if (c.Equals("y")||c.Equals("n")) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Console.WriteLine("選擇錯誤,請重新輸入:");? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? return b; ? ? ? ? } ? ? ? ? //創(chuàng)建添加的方法 ? ? ? ?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("是否繼續(xù)添加(Y/N)):"); ? ? ? ? ? ? ? ? string b = Utility.readConfirmSelection(); ? ? ? ? ? ? ? ? if (b.Equals("n")) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Flag = false; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? //創(chuàng)建一個輸出全部信息的方法 ? ? ? ? public static void Print(Dictionary<string,Student> dc) { ? ? ? ? ? ? //循環(huán)遍歷,將 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); ? ? ? ? } ? ? } }
比較器
創(chuàng)建三個比較器用于比較排序,使用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(); ? ? ? ? } ? ? ? } }
以上就是用一些簡單的代碼完成一個簡易的學生管理系統(tǒng)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
淺析C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別
C#靜態(tài)方法與非靜態(tài)方法的區(qū)別不僅僅是概念上的,那么他們有什么具體的區(qū)別呢?讓我們通過本文向大家介紹下C#中靜態(tài)方法和非靜態(tài)方法的區(qū)別,一起看看吧2017-09-09