C#学生管理系统/学习

题目:

实现一个菜单,针对Student.txt与Class.txt文件进行操作

描述:

I 输入学生信息:输入学生学号、姓名、性别、出生日期、班级名称、联系电话,跟已经存在的学生信息同样格式保存在文件中。如果学号重复,则提示“学号重复,无法插入”
L 显示所有学生:每个学生的信息占一行;每个学生的信息项包括学号、姓名、性别、出生日期、班级名称、联系电话,各项以Tab分隔
C 按班级查询:输入班级名称,显示学生详细信息,包括学号、姓名、性别、联系电话,各项以Tab分隔。多人分行显示。
N 按姓名查询:输入学生姓名,显示学生详细信息,包括学号、姓名、性别、出生日期、班级名称、联系电话,各项以Tab分隔。多人分行显示。
X 退出系统:程序退出

总结:

主要是对文件进行操作。没什么好说的。

  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.IO;
  5 using System.Text;
  6 
  7 namespace NET第二次作业
  8 {
  9     public class Student
 10     {
 11         public String num;
 12         public String name;
 13         public String sex;
 14         public String date_of_birth;
 15         public String classId;
 16         public String className;
 17         public String telephone;
 18     }
 19     public class myClass
 20     {
 21         public String id;
 22         public String classname;
 23     }
 24     class MyFourTest
 25     {
 26         static List students = new List();
 27         static List myClasss = new List();  
 28         public static void Init()
 29         {
 30 
 31             List student_p = new List<string>();
 32             StreamReader student = new StreamReader("C:\\Users\\a1191\\Desktop\\student.txt", Encoding.Default);
 33            
 34             while (!student.EndOfStream)
 35             {
 36                 student_p.Add(student.ReadLine());
 37                // Console.WriteLine(student_p[i++]+"a");
 38             }
 39             for(int j=0; j)
 40             {
 41                 string[] arr = student_p[j].Split(',');
 42                 if(arr[0]!="")
 43                 {
 44                     Student student1 = new Student();
 45                     student1.num = arr[0];
 46                     student1.name = arr[1];
 47                     student1.sex = arr[2];
 48                     student1.date_of_birth = arr[3];
 49                     student1.classId = arr[4];
 50                     student1.telephone = arr[5];
 51                     students.Add(student1);
 52                 }
 53 
 54             }
 55             student.Close();
 56             
 57             StreamReader streamReader = new StreamReader("C:\\Users\\a1191\\Desktop\\class.txt", Encoding.Default);
 58             while (!streamReader.EndOfStream)
 59             {
 60                 String class_ = streamReader.ReadLine();
 61                 string[] arr = class_.Split(',');
 62                 myClass t = new myClass();
 63                 t.classname = arr[1];
 64                 t.id = arr[0];
 65                 myClasss.Add(t);
 66                 for(int i=0; i)
 67                 {
 68                     if(students[i].classId.Equals(arr[0]))
 69                     {
 70                             students[i].className = arr[1];
 71                         //Console.WriteLine(arr[0]);
 72                     }
 73                 }
 74             }
 75             streamReader.Close();
 76         }
 77 
 78         public static void saveInfo(Student t)
 79         {
 80             StreamWriter student = new StreamWriter("C:\\Users\\a1191\\Desktop\\student.txt", true);
 81             //StreamWriter streamReader = new StreamWriter("C:\\Users\\a1191\\Desktop\\class.txt", true);
 82             String s = t.num + ',' + t.name + ',' + t.sex + ',' + t.date_of_birth + ',' + t.classId + ',' + t.telephone;
 83             student.WriteLine(s);
 84             //String c = t.classId + ',' + t.className + ",2005";
 85             //streamReader.WriteLine(c);
 86             student.Close();
 87             //streamReader.Close();
 88         }
 89         public static void InputStudentInfo()
 90         {
 91             Student t = new Student();
 92             Console.WriteLine("请输入学号:");
 93             t.num = Console.ReadLine();
 94             for(int i=0; i)
 95             {
 96                 if(t.num.Equals(students[i].num))
 97                 {
 98                     Console.WriteLine("学号重复,无法插入");
 99                     return;
100                 }
101             }
102             Console.WriteLine("请输入姓名:");
103             t.name = Console.ReadLine();
104             Console.WriteLine("请输入性别:");
105             t.sex = Console.ReadLine();
106             Console.WriteLine("请输入出生日期:");
107             t.date_of_birth = Console.ReadLine();
108             Console.WriteLine("请输入班级名称:");
109             t.className = Console.ReadLine();
110             Console.WriteLine("请输入联系电话:");
111             t.telephone = Console.ReadLine();
112             for(int i=0; i)
113             {
114                 if(t.className.Equals(myClasss[i].classname))
115                 {
116                     t.classId = myClasss[i].id;
117                 }
118             }
119             students.Add(t);
120             //for(int i=0; i121             //{
122             //    Console.WriteLine(students[i].className);
123             //}
124 
125             saveInfo(t);
126         }
127         public static void ShowAllStudent()
128         {
129             Console.WriteLine("学号\t姓名\t性别\t出生日期\t班级名称\t\t联系电话");
130             for(int i=0; i)
131             {
132                 Console.WriteLine(students[i].num + "\t"+ students[i].name + "\t"+ students[i].sex + 
133                     "\t"+ students[i].date_of_birth + "\t"+ students[i].className + "\t" +students[i].telephone);
134             }
135         }
136         public static void GetStudentByClass()
137         {
138             Console.WriteLine("请输入要查询的班级名称:");
139             String name = Console.ReadLine();
140             Console.WriteLine("学号\t姓名\t性别\t联系电话");
141             for (int i = 0; i < students.Count; i++)
142             {
143                 if(students[i].className.Equals(name))
144                 {
145                     Console.WriteLine(students[i].num + "\t" + students[i].name + "\t" + students[i].sex +
146                         "\t"   + students[i].telephone);
147                 }
148             }
149         }
150         public static void GetStudentByName()
151         {
152             Console.WriteLine("请输入要查询的学生姓名:");
153             String name = Console.ReadLine();
154             Console.WriteLine("学号\t姓名\t性别\t出生日期\t班级名称\t\t联系电话");
155             for (int i = 0; i < students.Count; i++)
156             {
157                 if (students[i].name.Equals(name))
158                 {
159                     Console.WriteLine(students[i].num + "\t" + students[i].name + "\t" + students[i].sex +
160                         "\t" + students[i].date_of_birth + "\t" + students[i].className + "\t" + students[i].telephone);
161                 }
162             }
163         }
164 
165         public static void StudentTest()
166         {
167             
168             Init();
169             while (true)
170             {
171                 ConsoleKeyInfo cmd;
172                 Console.WriteLine("欢迎使用学生管理系统\n");
173                 Console.WriteLine("I  输入学生信息\n");
174                 Console.WriteLine("L  显示所有学生\n");
175                 Console.WriteLine("C  按班级查询\n");
176                 Console.WriteLine("N  按姓名查询\n");
177                 Console.WriteLine("X  退出系统\n");
178                 cmd =  Console.ReadKey();
179                 Console.WriteLine("");
180                 switch (cmd.KeyChar)
181                 {
182                     case 'I':
183                         InputStudentInfo();
184                         break;
185                     case 'L':
186                         ShowAllStudent();
187                         break;
188                     case 'C':
189                         GetStudentByClass();
190                         break;
191                     case 'N':
192                         GetStudentByName();
193                         break;
194                     case 'X':
195                         System.Environment.Exit(0);
196                         break;
197                     default:
198                         Console.WriteLine("\n请输入指定字符(区分大小写)\n");
199                         break;
200                 }
201                 Console.ReadKey();
202                 Console.Clear();
203             }
204 
205 
206         }
207 
208     }
209 }

 

你可能感兴趣的:(C#学生管理系统/学习)