1. 程式人生 > 其它 >C#實現簡單學生資訊管理(極簡)

C#實現簡單學生資訊管理(極簡)

才學C#不久,老師佈置的;

先建一個.NET Framework控制檯專案

在新增上Main要點用的類(Student StudentMange)

主選單欄:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace ConsoleApp1
  8 {   
  9 
 10    public  class Program
 11     {
12 static void Main(string[] args) 13 { 14 bool flag = true; 15 while (flag == true) 16 { 17 int index = Memu(); 18 switch (index) 19 { 20 case 1: 21 studentMange.Loda();
22 break; 23 case 2: 24 studentMange.Save(); 25 break; 26 case 3: 27 Input(); 28 break; 29 case 4: 30 Find();
31 break; 32 case 5: 33 PrintTitle(); 34 Print(); 35 break; 36 case 0: 37 flag = false; 38 break; 39 40 41 } 42 43 44 } 45 } 46 static void Find() 47 { 48 bool IsFind = false; 49 Console.Write("按學生姓名查詢:"); 50 string name = Console.ReadLine(); 51 foreach(student s in studentMange.studs) 52 { 53 if (s.Name.Equals(name)) 54 { 55 Console.WriteLine(s); 56 IsFind = true; 57 break; 58 } 59 } 60 if (IsFind == false) 61 { 62 Console.WriteLine("沒有找到,姓名為[{0}]的學生", name); 63 64 } 65 } 66 static void PrintTitle() 67 { 68 Console.WriteLine("ID\t\tName\t\tAge"); 69 70 } 71 static void Print() 72 { 73 foreach(student s in studentMange.studs) 74 { 75 s.Show(); 76 } 77 } 78 static void Input() 79 { 80 Console.WriteLine("請輸入學生的相關資訊,完成學生資訊註冊:"); 81 Console.Write("序號:"); 82 int id = int.Parse(Console.ReadLine()); 83 Console.Write("姓名:"); 84 string name = Console.ReadLine(); 85 Console.Write("年齡:"); 86 int age = int.Parse(Console.ReadLine()); 87 88 student s = new student { Id = id, Name = name, Age = age }; 89 studentMange.studs.Add(s); 90 } 91 static int Memu() 92 { 93 Console.WriteLine("\n\n\n========================="); 94 Console.WriteLine("1.裝載學生資訊"); 95 Console.WriteLine("2.儲存學生資訊"); 96 Console.WriteLine("3.輸入學生資訊"); 97 Console.WriteLine("4.查詢學生資訊"); 98 Console.WriteLine("5.列印學生資訊"); 99 Console.WriteLine("0.退出"); 100 int iRet = int.Parse(Console.ReadLine()); 101 if (iRet < 0 || iRet > 5) 102 { 103 return Memu(); 104 } 105 else 106 { 107 return iRet; 108 } 109 110 } 111 } 112 }

向Student寫入程式碼:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApp1
 8 {
 9     [Serializable]
10     public  class student
11     {
12         public int Id { get; set; }
13         public string Name { get; set; }
14         public int Age { get; set; }
15         //為了方便輸出學生的個人資訊,重寫ToString方法
16         public override string ToString()
17         {
18             return string.Format("{0}[{1}\n{2}]", Name, Id, Age);
19         }
20         public void Show()
21         {
22             Console.WriteLine("{0}\n{1}\n{2}", Id, Name, Age);
23         }
24     }
25    
26 }

再向StudentMange寫入:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.IO;
 7 using System.Runtime.Serialization.Formatters.Binary;
 8 
 9 namespace ConsoleApp1
10 {
11    public  class studentMange
12     {
13         public static List<student> studs = new List<student>();
14         public static void Loda()
15         {
16             try
17             {
18                 FileStream fs = new FileStream("stu.dat", FileMode.Open, FileAccess.Read);
19                 BinaryFormatter bf = new BinaryFormatter();
20                 object data = bf.Deserialize(fs);//反序列化操作,得到object型別資料
21                 fs.Close();
22                 studs = (List<student>)data;
23 
24             }
25             catch(Exception ex) { }
26         }
27         public static void Save()
28         {
29             FileStream fs = new FileStream("stu.dat", FileMode.Create, FileAccess.Write);
30             BinaryFormatter bf = new BinaryFormatter();
31             bf.Serialize(fs, studs);
32             fs.Close();
33 
34         }
35     }
36 }