C#学生管理系统公开源代码:
整个项目所有的方法名如下图:
项目核心逻辑图如下:
**1 首先,键立实体类
private int _id;
private string _name;
private string _sex;
private int _age;
private int _score;
public int Id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string Sex
{
get
{
return _sex;
}
set
{
_sex = value;
}
}
public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
public int Score
{
get
{
return _score;
}
set
{
_score = value;
}
}
public Entity(int id, string name, string sex, int age, int score)
{
this._id = id;
this._name = name;
this._sex = sex;
this._age = age;
this._score = score;
}
public Entity()
{
}
public override String ToString()
{
return "\t\t\t\t\t\t学号:" + Id + "\t姓名:" + Name + "\t性别:" + Sex + "\t年龄" + Age + "\t分数:" + Score;
}
**2工具类 **`
List<Entity> ls = new List<Entity>();
private Studao sd = new Studao();
Entity e = new Entity();
#region //遍历五条数据
public void Init()
{
for (int i = 0; i < 5; i++)
{
Entity e = new Entity();
e.Id = i;
e.Name = "学生" + i;
if (i % 2 == 0)
{
e.Sex = "男";
}
else
{
e.Sex = "女";
}
e.Age = i;
if ((60 + i) > 100)
{
e.Score = 100;
}
else
{
e.Score = 80 + i;
}
ls.Add(e);
}
foreach (Entity item in ls)
{
Console.WriteLine(item.ToString());
}
showMain();
}
#endregion
#region //主菜单
public void showMain()
{
Console.WriteLine();
Console.WriteLine("\t\t\t\t********** **********");
Console.WriteLine("\t\t\t\t 欢迎使用学生管理系统 ");
Console.WriteLine("\t\t\t\t********** **********");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\t\t\t\t1.查看学员信息 2.增加学员信息 3.修改学员信息 4.删除学员信息 5.退出学生管理系统");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
Console.WriteLine("键盘输入你的选项:");
int i = int.Parse(Console.ReadLine());
switch (i)
{
case 1:
FindStudent();
break;
case 2:
AddStudent();
break;
case 3:
EditStudent();
break;
case 4:
RemoveStudent();
break;
case 5:
Exit();
break;
default:
break;
}
Console.ReadKey();
}
#endregion
#region //查看学员信息
public void FindStudent()
{
Console.WriteLine();
Console.WriteLine("\t\t\t\t********** **********");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\t\t\t\t 1.查看单个学员信息 2.查看所有学员信息 3.返回上级菜单");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t\t\t\t********** **********");
Console.WriteLine();
Console.WriteLine("键盘输入你的选项:");
try
{
int i1 = int.Parse(Console.ReadLine());
switch (i1)
{
case 1://查看单个
FindSingleStudent();
break;
case 2://查看所有
FindStudentAll();
break;
case 3: //返回上级菜单
showMain();
break;
default:
Console.WriteLine("超出选择范围,请重新输入!!!");
FindStudent();
break;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
FindStudent();
throw;
}
}
#endregion
#region //查看单个学员信息
public void FindSingleStudent()
{
Console.WriteLine();
Console.WriteLine("\t\t\t\t********** **********");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\t\t\t\t 1.根据学号查看 2.查看最高分学员 3.查看最低分学员 4.返回上级菜单");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t\t\t\t********** **********");
try
{
int i2 = int.Parse(Console.ReadLine());
switch (i2)
{
case 1://根据学号查看
sd.findEntityByID(ls);
FindSingleStudent();
break;
case 2://查看最高分
sd.FindMaxScoreStudent(ls);
FindSingleStudent();
break;
case 3://查看最低分
sd.FindMinScoreStudent(ls);
FindSingleStudent();
break;
case 4://返回上级菜单
FindStudent();
break;
default:
Console.WriteLine("超出选择范围,请重新输入!!!");
FindSingleStudent();
break;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
FindSingleStudent();
throw;
}
}
#endregion
#region //查看所有学员信息
public void FindStudentAll()
{
Console.WriteLine();
Console.WriteLine("\t\t\t\t********** **********");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\t\t\t\t1.根据年龄排序 2.根据分数排序 3.根据学号排序 4.查看总分与平均分 5.返回上级菜单");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t\t\t\t********** **********");
try
{
int i3 = int.Parse(Console.ReadLine());
switch (i3)
{
case 1://根据年龄排序
sd.FindStudentOrderAge(ls);
FindStudentAll();
break;
case 2://根据分数排序
sd.FindStudentOrderScore(ls);
FindStudentAll();
break;
case 3://根据学号排序
sd.FindStudentOrderNo(ls);
FindStudentAll();
break;
case 4://查看总分与平均分
sd.FindStudentAvgScoreAndSumScore(ls);
FindStudentAll();
break;
case 5://返回上级菜单
FindSingleStudent();
break;
default:
Console.WriteLine("超出选择范围,请重新输入!!!");
FindStudentAll();
break;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
FindStudentAll();
throw;
}
}
#endregion
#region //增加学员信息
public void AddStudent()
{
Console.WriteLine();
Console.WriteLine("\t\t\t\t********** **********");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\t\t\t\t 1.录入学员信息 2.返回上级菜单");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t\t\t\t********** **********");
try
{
int i4 = int.Parse(Console.ReadLine());
switch (i4)
{
case 1://录入学员信息
Console.WriteLine("请输入你要增加的学号:");
int id = int.Parse(Console.ReadLine());
e.Id = id;
Console.WriteLine("请输入你要增加的姓名:");
string name = Console.ReadLine();
e.Name = name;
Console.WriteLine("请输入你要增加的性别:");
string sex = Console.ReadLine();
e.Sex = sex;
Console.WriteLine("请输入你要增加的年龄:");
int age = int.Parse(Console.ReadLine());
e.Age = age;
Console.WriteLine("请输入你要增加的分数:");
int score = int.Parse(Console.ReadLine());
e.Score = score;
Console.WriteLine();
Console.WriteLine("信息已成功录入!");
Console.WriteLine();
ls.Add(e);
sd.add(e, ls);
// Console.WriteLine("123");
AddStudent();
break;
case 2://返回上级菜单
showMain();
break;
default:
Console.WriteLine("超出选择范围,请重新输入!");
AddStudent();
break;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
AddStudent();
throw;
}
}
#endregion
#region //修改学员信息
public void EditStudent()
{
Console.WriteLine();
Console.WriteLine("\t\t\t\t********** **********");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\t\t\t\t 1.输入要修改的学号 2.返回上级菜单 ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t\t\t\t********** **********");
try
{
int i5 = int.Parse(Console.ReadLine());
switch (i5)
{
case 1://输入要修改的学号
Console.WriteLine("请输入要修改学生的学号:");
int id = int.Parse(Console.ReadLine());
//Console.WriteLine("请输入修改后学生的学号:");
//int id1 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入修改后学生的姓名:");
string name = Console.ReadLine();
Console.WriteLine("请输入修改后学生的性别:");
string sex = Console.ReadLine();
Console.WriteLine("请输入修改后学生的年龄:");
int age = int.Parse(Console.ReadLine());
Console.WriteLine("请输入修改后学生的分数:");
int score = int.Parse(Console.ReadLine());
//删除了id1
sd.EditStudentByID(id, name, sex, age, score, ls);
EditStudent();
break;
case 2://返回上级菜单
showMain();
break;
default:
Console.WriteLine("超出选择范围,请重新输入!!!");
EditStudent();
break;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
EditStudent();
throw;
}
}
#endregion
#region //删除学员信息
public void RemoveStudent()
{
Console.WriteLine();
Console.WriteLine("\t\t\t\t********** **********");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\t\t\t\t 1.输入要删除的学号 2.返回上级菜单");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\t\t\t\t********** **********");
try
{
int i6 = int.Parse(Console.ReadLine());
switch (i6)
{
case 1://输入要删除的学号
Console.WriteLine("请输入要删除的学号:");
int it = int.Parse(Console.ReadLine());
sd.RemoveStudent(it, ls);
RemoveStudent();
break;
case 2:
//返回
showMain();
break;
default:
Console.WriteLine("超出选择范围,请重新输入!");
RemoveStudent();
break;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
RemoveStudent();
throw;
}
}
#endregion
#region //退出学生管理系统
public void Exit()
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\t\t\t\t********** **********");
Console.WriteLine("\t\t\t\t------—————————————感谢使用此产品!————————---------------------");
Console.WriteLine("\t\t\t\t********** **********");
Console.ForegroundColor = ConsoleColor.Red;
}
#endregion
程序类(program)
Manage bd = new Manage();
//初始化五条数据
bd.Init();
dao类写增删改查的
//快速连续产生相同随机数的解决方案
Random r = new Random();
#region //根据ID查询单个学生
public void findEntityByID(List<Entity> ls)
{
Console.WriteLine("请输入学号:");
int id = int.Parse(Console.ReadLine());
foreach (Entity s in ls)
{
if (s.Id == id)
{
Console.WriteLine("学生信息:" + ls[id].ToString());
break;
}
}
}
#endregion
#region //查看最高分
public void FindMaxScoreStudent(List<Entity> ls)
{
double max = 0;
max = ls[0].Score;
for (int i = 0; i < ls.Count; i++)
{
if (max <= ls[i].Score)
{
max = ls[i].Score;
}
}
for (int i = 0; i < ls.Count; i++)
{
if (ls[i] != null)
{
if (ls[i].Score == max)
{
Console.WriteLine("最高分数是:" + ls[i].ToString());
}
}
}
}
#endregion
#region //查看最低分
public void FindMinScoreStudent(List<Entity> ls)
{
double min = 0;
min = ls[0].Score;
for (int i = 0; i < ls.Count; i++)
{
if (min > ls[i].Score)
{
min = ls[i].Score;
}
}
for (int i = 0; i < ls.Count; i++)
{
if (ls[i].Score == min)
{
Console.WriteLine("最低分数是:" + ls[i].ToString());
}
}
}
#endregion
#region //根据年龄排序
public void FindStudentOrderAge(List<Entity> ls)
{
ls = ls.OrderByDescending(x => x.Age).ToList();//ToList()会新开辟一个内存空间
foreach (Entity item in ls)
{
Console.WriteLine(item);
}
}
#endregion
#region //根据分数排序
public void FindStudentOrderScore(List<Entity> ls)
{
ls = ls.OrderByDescending(x => x.Score).ToList();
foreach (Entity item in ls)
{
Console.WriteLine(item);
}
}
#endregion
#region //根据学号排序
public void FindStudentOrderNo(List<Entity> ls) {
ls = ls.OrderByDescending(x => x.Id).ToList();
foreach (Entity item in ls)
{
Console.WriteLine(item);
}
}
#endregion
#region //总分和平均分
public void FindStudentAvgScoreAndSumScore(List<Entity> ls)
{
int count = 0;
double sum = 0;
for (int i = 0; i < ls.Count; i++)
{
if (ls[i] == null)
{
break;
}
else if (ls[i] != null)
{
sum += ls[i].Score;
count++;
}
}
double avg = sum / count;
Console.WriteLine("总分:" + sum);
Console.WriteLine("平均分:" + avg);
}
#endregion
#region //增加学员信息
public void add(Entity et, List<Entity> ls) {
for (int i = 0; i <ls.Count; i++)
{
if (ls[i]!=null) {
ls[i] = et;
break;
}
}
foreach (Entity item in ls)
{
Console.WriteLine(item.ToString());
}
}
#endregion
#region //修改学员的信息
//删除了int id
public void EditStudentByID(int sId, string name, string sex, int age, int score, List<Entity> ls)
{
bool falg = false;
for (int i = 0; i < ls.Count; i++)
{
if (ls[i] != null)
{
if (ls[i].Id == sId)
{
falg = false;
ls[i].Name = name;
ls[i].Sex = sex;
ls[i].Age = age;
ls[i].Score = score;
break;
}
else
{
falg = true;
}
}
}
if (falg)
{
Console.WriteLine("系统无法修改此学号!");
}
foreach (Entity item in ls)
{
Console.WriteLine();
Console.WriteLine("修改后的为:" + item.ToString());
}
}
#endregion
#region //删除
public void RemoveStudent(int sid, List<Entity> ls)
{
for (int i = 0; i < ls.Count; i++)
{
if (ls[i] == null)
{
continue;
}
if (ls[i] != null)
{
if (ls[i].Id == sid)
{
ls[i] = ls[i + 1];
ls[i + 1] = null;
Console.WriteLine();
Console.WriteLine("成功删除!");
Console.WriteLine();
}
}
}
foreach (Entity item in ls)
{
if (item != null)
{//参考资料在QQ群:683782676
Console.WriteLine(item.ToString());
}
}
}
#endregion