1. 程式人生 > >C#實現簡單的學生管理系統增刪改查

C#實現簡單的學生管理系統增刪改查

1.Programs.cs

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

namespace StudentSystem
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("歡迎使用2018級學生管理系統");
            Console.WriteLine("===============================================");
            Console.WriteLine("請選擇如下操作:");
            Console.WriteLine("A:新增  B:查詢  C:刪除  D:修改  E退出系統");
            string ch;
         //   Hashtable stu = new Hashtable();
            Dictionary<string, Student> stu = new Dictionary<string, Student>();
            Student student=new Student();
            Console.Write("請輸入選擇功能:");
            ch = Console.ReadLine();
            while(ch!="E")
            {
                switch (ch)
                {
                    case "A":
                        AddStudent(stu);
                        break;
                       
                    case "B":
                        QueryStudent(stu);
                        break;

                    case "C":
                        DeleteStudent(stu);
                        break;
                    case "D":
                        UpdateStudent(stu);
                        break;
                    case "E":
                        break;
                }

                Console.Write("請輸入選擇功能:");
                ch = Console.ReadLine();
            }
            foreach(KeyValuePair<string,Student> de in stu)
            {
                Console.WriteLine(de.Value);
            }
           // Console.WriteLine(student.showMSG());

            Console.ReadLine();
            
        }
        public static void AddStudent(Dictionary<string, Student> dic)
        {
            string stuID;
            Console.WriteLine("請輸入新增學生資訊:");
            Console.WriteLine("請輸入學生學號:");
            stuID = Console.ReadLine();
            while (dic.ContainsKey(stuID))
            {
                Console.WriteLine("該學生已存在,請重新輸入:");
                stuID = Console.ReadLine();
            }
            Student student = new Student();

           
            Console.WriteLine("請輸入學生姓名:");
            string Name = Console.ReadLine();
            Console.WriteLine("請輸入學生性別:");
            string Sex = Console.ReadLine();
            Console.WriteLine("請輸入學生班級:");
            string Class = Console.ReadLine();
            Console.WriteLine("請輸入學生語文成績:");
            string Chinese = Console.ReadLine();
            Console.WriteLine("請輸入學生數學成績:");
            string Math = Console.ReadLine();
            Console.WriteLine("請輸入學生英語成績:");
            string English = Console.ReadLine();
            student.setStuID(stuID);
            student.setName(Name);
            student.setSex(Sex);
            student.setClass(Class);
            student.setChinese(Chinese);
            student.setMath(Math);
            student.setEnglish(English);
            dic.Add(stuID, student);
            Console.WriteLine("新增學生成功");
        }
        public static void QueryStudent(Dictionary<string, Student> dic)
        {
            string stuID;
            Student student = new Student();
            Console.WriteLine("請輸入查詢學生的學號:");
            stuID = Console.ReadLine();
            while (!dic.ContainsKey(stuID))
            {
                Console.WriteLine("該學生不存在,請重新輸入:");
                stuID=Console.ReadLine();
            }
            dic[stuID].showMSG();
        }
        public static void DeleteStudent(Dictionary<string, Student> dic)
        {
            string stuID;
            Console.WriteLine("請輸入刪除學生的學號:");
            stuID = Console.ReadLine();
            while (!dic.ContainsKey(stuID))
            {
                Console.WriteLine("該學生不存在,請重新輸入:");
                stuID=Console.ReadLine();
            }
            dic.Remove(stuID);
            Console.WriteLine("成功刪除。");
        }
        public static void UpdateStudent(Dictionary<string, Student> dic)
        {
            string stuID;
            string id;
            Console.WriteLine("請輸入修改學生的學號:");
            stuID = Console.ReadLine();
            while (!dic.ContainsKey(stuID))
            {
                Console.WriteLine("該學生不存在,請重新輸入:");
                stuID = Console.ReadLine();
            }

            Console.WriteLine("1.修改姓名  2.修改性別  3.修改班級  4.修改語文成績  5.修改數學成績  6.修改英語成績");
            Console.WriteLine("請輸入選擇修改的序號:");
            id = Console.ReadLine();
            switch(id)
            {
                case "1":
                   Console.WriteLine("請輸入學生姓名:");
                   string Name = Console.ReadLine();
                    dic[stuID].setName(Name);   
                    break;
                case "2":
                     Console.WriteLine("請輸入學生性別:");
                     string Sex = Console.ReadLine();
                     dic[stuID].setSex(Sex);
                    break;
                case "3":
                     Console.WriteLine("請輸入學生班級:");
                     string Class = Console.ReadLine();
                     dic[stuID].setClass(Class);
                     break;
                case "4":
                     Console.WriteLine("請輸入學生語文成績:");
                     string Chinese = Console.ReadLine();
                     dic[stuID].setChinese(Chinese);
                     break;
                case "5":
                     Console.WriteLine("請輸入學生數學成績:");
                     string Math = Console.ReadLine();
                     dic[stuID].setMath(Math);
                     break;
                case "6":
                    Console.WriteLine("請輸入學生英語成績:");
                    string English = Console.ReadLine();
                     dic[stuID].setEnglish(English);
                     break;

            }


        }

    }
}

2.Student.cs(學生類)

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

namespace StudentSystem
{
    class Student
    {
        string stuID;
        string Name;
        string Sex;
        string Class;
        string Chinese;
        string Math;
        string English;

        public Student(string stuID,string  Name,string Sex,string Class,string Chinese,string Math,string English)
        {
            this.stuID = stuID;
        }
        public Student()
        { }

        public void setStuID(string stuID)
        {
            this.stuID = stuID;
        }
        public void setName(string Name)
        {
            this.Name = Name;
        }
        public void setSex(string Sex)
        {
            this.Sex = Sex;
        }
        public void setClass(string Class)
        {
            this.Class = Class;
        }
        public void setChinese(string Chinese)
        {
            this.Chinese = Chinese;
        }
             public void setMath(string Math)
        {
            this.Math = Math;
        }
         public void setEnglish(string English)
        {
            this.English = English;
        }
    
        public void showMSG()
        {
            Console.WriteLine(" 學號:" + stuID + " 姓名:" + Name + "班級"+Class+"性別 " + Sex + " 語文" + Chinese + " 數學" + Math + "英語 " + English);

        }
       

    }
}