大家好,又见面了,我是你们的朋友全栈君。
① 创建一个学员的实体类Student,实现其构造方法和封装:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// 实体类
/// </summary>
namespace BaseXm
{
/// <summary>
/// 学员的实体类
/// </summary>
public class Student
{
/// <summary>
/// 学号
/// </summary>
private int _id;
/// <summary>
/// 姓名
/// </summary>
private string _name;
/// <summary>
/// 年龄
/// </summary>
private int _age;
/// <summary>
/// 性别
/// </summary>
private string _sex;
/// <summary>
/// 分数
/// </summary>
private int _grade;
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("学号:");
sb.Append(_id);
sb.Append(",姓名:");
sb.Append(_name);
sb.Append(",年龄:");
sb.Append(_age);
sb.Append(",性别:");
sb.Append(_sex);
sb.Append(",分数");
sb.Append(_grade);
return sb.ToString();
}
public int Id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
public string Sex
{
get
{
return _sex;
}
set
{
_sex = value;
}
}
public int Grade
{
get
{
return _grade;
}
set
{
_grade = value;
}
}
public Student(int _id, string _name, int _age, string _sex, int _grade)
{
this.Id = _id;
this.Name = _name;
this.Age = _age;
this.Sex = _sex;
this.Grade = _grade;
}
public Student()
{
}
}
}
② 写一个老师管理学员的接口ITeacher,写增删查改的方法:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BaseXM
{
/// <summary>
/// 管理的接口类 泛型
/// </summary>
public interface ITeacher<T> where T : class
{
/// <summary>
/// 增加学员
/// </summary>
/// <param name="t">学员对象</param>
void AddStudent(T t);
/// <summary>
/// 编辑修改学员
/// </summary>
/// <param name="t">学号</param>
void EditStudent(T t);
/// <summary>
/// 删除学员
/// </summary>
/// <param name="i">学号</param>
void DeleteStudent(int i);
/// <summary>
/// 查询单个学员根据id
/// </summary>
/// <param name="i"></param>
T SelectStudentById(int i);
/// <summary>
/// 查询最高分或最低分学员
/// </summary>
/// <param name="i"></param>
ArrayList SelectMaxOrMin(int i);
/// <summary>
/// 查询总分与平均分
/// </summary>
int[] SelectSumAndAvg();
/// <summary>
/// 查询全部学员根据年龄或分数或学号排序
/// </summary>
ArrayList SelectStudentAgeGradeId(string str);
}
}
③写学员管理的接口类 IStudent,实现老师的接口以及学员的实体类ITeacher:
using BaseXM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BaseXm
{
/// <summary>
/// 学员管理的接口类
/// </summary>
public interface IStudent : ITeacher<Student>
{
}
}
④实现学生管理的实现类,StudentManage,接着实现接口IStudent:
using BaseXm;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
/// 实现类
/// </summary>
namespace BaseXM
{
/// <summary>
/// 学生管理实现类
/// </summary>
public class StudentManage : IStudent
{
#region 正则表达式
/// <summary>
/// 正则表达式
/// </summary>
Regex reg = new Regex("^[0-9]+$");
#endregion
#region 学员集合List
/// <summary>
/// 学员集合 List
/// </summary>
private static ArrayList ls = new ArrayList();
#endregion
#region 登录信息 增加学员集合数据AddStudents()
/// <summary>
/// 增加学员数据
/// </summary>
public static void AddStudents()
{
//赋值
ls.Add(new Student(1, "拾亿", 18, "男", 136));
}
#endregion
#region 增加学员AddStudent(Student q)
/// <summary>
/// 增加学员
/// </summary>
/// <param name="q"></param>
public void AddStudent(Student q)
{
int i = 1;
foreach (Student s in ls)
{
if (s.Id >= i)
i = s.Id + 1;
}
q.Id = i;
ls.Add(q);
}
#endregion
#region 删除学员DeleteStudent(int i)
/// <summary>
/// 删除学员
/// </summary>
/// <param name="i">学号</param>
public void DeleteStudent(int i)
{
int p = 0;
foreach (Student s in ls)
{
if (s.Id == i)
{
ls.Remove(s);
break;
}
p++;
}
}
#endregion
#region 修改学员EditStudent(Student r)
/// <summary>
/// 修改学员
/// </summary>
/// <param name="r">学员对象</param>
public void EditStudent(Student r)
{
foreach (Student s in ls)
{
if (s.Id == r.Id)
{
s.Name = r.Name;
s.Age = r.Age;
s.Sex = r.Sex;
s.Grade = r.Grade;
break;
}
}
}
#endregion
#region 登录界面 Login()
public void Login()
{
while (true)
{
//Console.ForegroundColor = ConsoleColor.Blue; //设置字体颜色为蓝色
Console.WriteLine("《《《《《《--------------------------------------------------------------&&&&----欢迎您来到 学员 管理系统----&&&&-------------------------------------------------------------》》》》》》》");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("--------------------------------------------------------------------------- 此系统需要先登录后方可使用哦 ------------------------------------------------------------------------------");
Console.WriteLine(" ---- 请输入 学号 ---- ");
string sy = Console.ReadLine().Trim();
Console.WriteLine(" ---- 请输入 姓名 ---- ");
string wl = Console.ReadLine().Trim();
if (sy.Equals("1") && wl.Equals("拾忆"))
{
ShowMain();
}
else
{
Console.WriteLine("学号或者姓名没有输入或者有误");
Login();
}
break;
}
}
#endregion
#region 一级菜单 界面
/// <summary>
/// 一级菜单 主界面
/// </summary>
///
public void ShowMain()
{
while (true)
{
Console.ForegroundColor = ConsoleColor.Blue; //设置字体颜色为蓝色
Console.WriteLine();
Console.WriteLine("《《《《《《--------------------------------------------------------------&&&&----欢迎您来到 学员 管理系统----&&&&-------------------------------------------------------------》》》》》》》");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
Console.WriteLine("--------------------------------------------------------------------------------- 本系统有如下操作 ------------------------------------------------------------------------------------");
Console.WriteLine(" ----1.教员界面---- ");
Console.WriteLine();
Console.WriteLine(" ----2.学员界面---- ");
Console.WriteLine();
Console.WriteLine(" ----3.退出学员管理系统---- ");
Console.WriteLine();
Console.WriteLine(" ----《《《《请输入1--2的数字》》》》---- ");
Console.WriteLine();
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
string w = Console.ReadLine().Trim();
if (w.Equals("1"))
{
ShowMain11();
}
else if (w.Equals("2"))
{
ShowMain12();
}
else if (w.Equals("3"))
{
Console.WriteLine(" 退出成功!!!欢迎您再次使用哦 ");
}
else
{
Console.WriteLine(" 输入错误!!!请依据提示重新请输入1--3的数字哦 ");
continue;
}
break;
}
}
/// <summary>
/// 一级菜单第一个 教员查看信息
/// </summary>
public void ShowMain11()
{
while (true)
{
Console.WriteLine(" ----1.查看学员信息---- ");
Console.WriteLine();
Console.WriteLine(" ----2.增加学员信息---- ");
Console.WriteLine();
Console.WriteLine(" ----3.修改学员信息---- ");
Console.WriteLine();
Console.WriteLine(" ----4.删除学员信息---- ");
Console.WriteLine();
Console.WriteLine(" ----5.返回上级菜单---- ");
Console.WriteLine();
Console.WriteLine(" ----6.退出学员管理系统---- ");
Console.WriteLine();
Console.WriteLine(" ----《《《《请输入1--5的数字》》》》---- ");
Console.WriteLine();
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
string x = Console.ReadLine().Trim();
if (x.Equals("1"))
{
ShowMain123();
}
else if (x.Equals("2"))
{
ShowMain212();
}
else if (x.Equals("3"))
{
ShowMain23();
}
else if (x.Equals("4"))
{
ShowMain24();
}
else if (x.Equals("5"))
{
ShowMain();
break;
}
else if (x.Equals("6"))
{
Console.WriteLine(" 退出成功!!!欢迎您再次使用哦 ");
}
else
{
Console.WriteLine(" 输入错误!!!请依据提示重新请输入1--5的数字哦 ");
continue;
}
break;
}
}
/// <summary>
/// 一级菜单第二个 学员查看信息
/// </summary>
public void ShowMain12()
{
while (true)
{
Console.WriteLine(" ----1.查看学员信息---- ");
Console.WriteLine();
Console.WriteLine(" ----2.退出学员管理系统---- ");
Console.WriteLine();
Console.WriteLine(" ----3.返回上级菜单---- ");
Console.WriteLine();
Console.WriteLine(" ----《《《《请输入1--2的数字》》》》---- ");
Console.WriteLine();
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
string x = Console.ReadLine().Trim();
if (x.Equals("1"))
{
ShowMain124();
}
else if (x.Equals("2"))
{
Console.WriteLine(" 退出成功!!!欢迎您再次使用哦 ");
break;
}
else if (x.Equals("3"))
{
ShowMain();
}
else
{
Console.WriteLine(" 输入错误!!!请依据提示重新请输入1--2的数字哦 ");
}
break;
}
}
#endregion
#region 二级菜单第一个 查看信息
/// <summary>
/// 二级菜单第一个 教员查看学员信息
/// </summary>
///
private void ShowMain123()
{
while (true)
{
Console.WriteLine("%----------------------------%");
Console.WriteLine(" 1.查看单个学员信息 ");
Console.WriteLine(" 2.查看所有学员信息 ");
Console.WriteLine(" 3.返回上级菜单 ");
Console.WriteLine(" 请输入1--3的数字 ");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
string s = Console.ReadLine().Trim();
if (s.Equals("1"))
{
ShowMain31();
break;
}
else if (s.Equals("2"))
{
ShowMain32();
break;
}
else if (s.Equals("3"))
{
ShowMain11();
break;
}
else
{
Console.WriteLine(" 输入错误!!!请按提示重新请输入1--3的数字!!! ");
}
}
}
/// <summary>
/// 二级菜单第二个 学员查看信息
/// </summary>
private void ShowMain124()
{
while (true)
{
Console.WriteLine("%----------------------------%");
Console.WriteLine(" 1.查看单个学员信息 ");
Console.WriteLine(" 2.查看所有学员信息 ");
Console.WriteLine(" 3.返回上级菜单 ");
Console.WriteLine(" 请输入1--3的数字 ");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
string s = Console.ReadLine().Trim();
if (s.Equals("1"))
{
ShowMain311();
break;
}
else if (s.Equals("2"))
{
ShowMain322();
break;
}
else if (s.Equals("3"))
{
ShowMain12();
break;
}
else
{
Console.WriteLine(" 输入错误!!!请按提示重新请输入1--3的数字 !!! ");
}
}
}
#endregion
#region 二级菜单第二个 增加学员信息
/// <summary>
/// 二级菜单第二个
/// </summary>
private void ShowMain212()
{
while (true)
{
Console.WriteLine("%----------------------------%");
Console.WriteLine(" 1.增加学员信息 ");
Console.WriteLine(" 2.返回上级菜单 ");
Console.WriteLine(" 请输入1--2的数字 ");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
string s = Console.ReadLine().Trim();
if (s.Equals("1"))
{
while (true)
{
Console.WriteLine(" 请输入您要增加的学员的姓名: ");
string name = Console.ReadLine().Trim();
if (name.Length < 2 || name.Length > 20)
{
Console.WriteLine(" 姓名只能在2位到20位之间 ");
Console.WriteLine(" 请输入a或其他任意字符退出 ");
if (Console.ReadLine().Trim().Equals("a"))
continue;
}
else
{
while (true)
{
Console.WriteLine(" 请输入您要增加的学员的年龄 ");
string s2 = Console.ReadLine().Trim();
if (!reg.Match(s2).Success)
{
Console.WriteLine(" 输入的不是数字 ");
Console.WriteLine(" 请输入1或其他任意字符退出 ");
if (Console.ReadLine().Equals("1"))
continue;
}
else
{
int age = int.Parse(s2);
if (age < 10 || age > 50)
{
Console.WriteLine(" 年龄只能在10-50岁之间的整数 ");
Console.WriteLine(" 请输入1或其他任意字符退出 ");
if (Console.ReadLine().Equals("1"))
continue;
}
else
{
while (true)
{
Console.WriteLine(" 请输入你要增加的学员的性别: ");
string sex = Console.ReadLine();
if (!(sex.Equals("男") || sex.Equals("女")))
{
Console.WriteLine(" 性别只能为男或者女哦 ");
Console.WriteLine(" 请输入1或其他任意字符退出 ");
if (Console.ReadLine().Trim().Equals("1"))
continue;
}
else
{
while (true)
{
Console.WriteLine(" 请输入您要增加的学员的分数(0~150): ");
string s3 = Console.ReadLine().Trim();
Match ms2 = reg.Match(s3);
if (!ms2.Success)
{
Console.WriteLine(" 输入的不是数字(0~150之间) ");
Console.WriteLine(" 请输入1或其他任意字符退出 ");
if (Console.ReadLine().Trim().Equals("1"))
continue;
}
else
{
int grade = int.Parse(s3);
if (grade > -1 && grade < 151)
{
Console.WriteLine("姓名:" + name + ",年龄:" + age + ",性别:" + sex + ",分数:" + grade);
Console.WriteLine(" 您确认增加这位学员吗?请输入:1(确认)其它字符(取消) ");
if (!Console.ReadLine().Trim().Equals("1"))
{
Console.WriteLine(" 取消成功 ");
break;
}
AddStudent(new Student(0, name, age, sex, grade));
Console.WriteLine(" 增加成功 !!!! ");
break;
}
else
{
Console.WriteLine(" 学员的分数在0-150分之间 ");
Console.WriteLine(" 请输入1或其他任意字符退出 ");
if (Console.ReadLine().Trim().Equals("1"))
continue;
}
}
break;
}
}
break;
}
}
}
break;
}
}
break;
}
}
else if (s.Equals("2"))
{
ShowMain11();
return;
}
else
{
Console.WriteLine(" 请输入1--2的数字!!! ");
}
}
}
#endregion
#region 二级菜单第三个 修改学员信息
void ShowMain23()
{
while (true)
{
Console.WriteLine("%----------------------------%");
Console.WriteLine(" 1.输入要修改的学号 ");
Console.WriteLine(" 2.返回上级菜单 ");
Console.WriteLine(" 请输入1--2的数字 ");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
string s = Console.ReadLine().Trim();
if (s.Equals("1"))
{
while (true)
{
while (true)
{
Console.WriteLine(" 请输入您要修改的学员的学号(阿拉伯数字): ");
string t = Console.ReadLine().Trim();
if (!reg.Match(t).Success)
{
Console.WriteLine(" 请输入1--2的数字 ");
break;
}
else
{
int id = int.Parse(t);
Student x = SelectStudentById(id);
if (x == null)
{
Console.WriteLine(" 教员没有添加此学号哦 ");
break;
}
Console.WriteLine(" 原来的姓名:" + x.Name + " ");
Console.WriteLine(" 请输入您要修改后的学员的姓名: ");
string name = Console.ReadLine().Trim();
if (name.Length < 2 || name.Length > 20)
{
Console.WriteLine(" 姓名需要在2位到20位之间!!! ");
Console.WriteLine(" 请输入1或其他任意字符退出 ");
if (Console.ReadLine().Trim().Equals("1"))
continue;
}
else
{
while (true)
{
Console.WriteLine(" 原来年龄:" + x.Age + " ");
Console.WriteLine(" 请输入您要修改后的学员的年龄 ");
string s2 = Console.ReadLine().Trim();
if (!reg.Match(s2).Success)
{
Console.WriteLine(" 学员的年龄只能为10-50之间的数字 ");
Console.WriteLine(" 请输入1或其他任意字符退出 ");
if (Console.ReadLine().Equals("1"))
continue;
}
else
{
int age = int.Parse(s2);
if (age < 10 || age > 50)
{
Console.WriteLine(" 学员的年龄在10-50岁之间!!! ");
Console.WriteLine(" 请输入1或其他任意字符退出 ");
if (Console.ReadLine().Equals("1"))
continue;
}
else
{
while (true)
{
Console.WriteLine(" 原姓别:" + x.Sex + " ");
Console.WriteLine(" 请输入修改后的学员的性别: ");
string sex = Console.ReadLine();
if (!(sex.Equals("男") || sex.Equals("女")))
{
Console.WriteLine(" 学员的性别只能位男或者女哦 ");
Console.WriteLine(" 请输入1或其他任意字符退出 ");
if (Console.ReadLine().Trim().Equals("1"))
continue;
}
else
{
while (true)
{
Console.WriteLine(" 原分数:" + x.Grade + " ");
Console.WriteLine(" 请输入修改后的学员的分数(0~150): ");
string s3 = Console.ReadLine().Trim();
Match ms2 = reg.Match(s3);
if (!ms2.Success)
{
Console.WriteLine(" 学员的分数只能在0-150分之间的整数 ");
Console.WriteLine(" 请输入1或其他任意字符退出 ");
if (Console.ReadLine().Trim().Equals("1"))
continue;
}
else
{
int grade = int.Parse(s3);
if (grade > -1 && grade < 151)
{
Console.WriteLine(" 原信息:" + x + " ");
Student y = new Student(x.Id, name, age, sex, grade);
Console.WriteLine(" 修改后的信息:" + y + " ");
Console.WriteLine(" 您确认修改这位学员吗?请输入:1(确认)0(取消) ");
if (!Console.ReadLine().Trim().Equals("1"))
{
Console.WriteLine(" 取消成功! ");
break;
}
EditStudent(y);
Console.WriteLine(" 修改成功 !!! ");
break;
}
else
{
Console.WriteLine(" 学员的分数只能在0-150分之间的整数!!! ");
Console.WriteLine(" 请输入1或其他任意字符退出 ");
if (Console.ReadLine().Trim().Equals("1"))
continue;
}
}
break;
}
}
break;
}
}
}
break;
}
}
}
break;
}
break;
}
}
else if (s.Equals("2"))
{
ShowMain11();
return;
}
else
{
Console.WriteLine(" 请输入1--2的数字哦 !!! ");
}
}
}
#endregion
#region 二级菜单第四个 删除学员信息
void ShowMain24()
{
while (true)
{
Console.WriteLine("%----------------------------%");
Console.WriteLine(" 1.输入要删除的学号 ");
Console.WriteLine(" 2.返回上级菜单 ");
Console.WriteLine(" 请输入1--2的数字 ");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
string s = Console.ReadLine().Trim();
if (s.Equals("1"))
{
while (true)
{
Console.WriteLine(" 请输入你要删除的学员的学号(阿拉伯数字): ");
string a = Console.ReadLine().Trim();
if (!reg.Match(a).Success)
{
Console.WriteLine(" 请您输入学号数字哦!!! ");
break;
}
else
{
int id = int.Parse(a);
Student student = SelectStudentById(id);
if (student == null)
{
Console.WriteLine(" 教员没有添加此学号哦,学号为阿拉伯数字 ");
}
else
{
Console.WriteLine(student);
Console.WriteLine(" 您确定要删除此位学员吗?请输入:1(确认) 其他(取消) ");
if (Console.ReadLine().Trim().Equals("1"))
{
DeleteStudent(id);
Console.WriteLine(" 删除成功! !! ");
}
else
{
Console.WriteLine(" 取消成功! !! ");
}
}
}
break;
}
}
else if (s.Equals("2"))
{
ShowMain11();
break;
}
else
{
Console.WriteLine(" 输入错误!!!请输入1--2的数字哦 ");
}
}
}
#endregion
#region 三级菜单第一个 查看单个学员信息
/// <summary>
/// 三级菜单第一个 教员查看
/// </summary>
private void ShowMain31()
{
while (true)
{
Console.WriteLine("%----------------------------%");
Console.WriteLine(" 1.根据学号查看信息 ");
Console.WriteLine(" 2.查看最高分学员 ");
Console.WriteLine(" 3.查看最低分学员 ");
Console.WriteLine(" 4.返回上级菜单 ");
Console.WriteLine(" 请输入1--4的数字 ");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
String s = Console.ReadLine().Trim();
if (s.Equals("1"))
{
Console.WriteLine(" 请输入您要查找的学生的学号: ");
string s2 = Console.ReadLine().Trim();
if (s2.Length > 0)
{
Match ma = reg.Match(s2);
if (ma.Success)
{
Student s3 = SelectStudentById(int.Parse(s2));
if (s3 == null)
{
Console.WriteLine(" 教员没有添加此学号哦 ");
}
else
{
Console.WriteLine(s3);
}
}
else
{
Console.WriteLine(" 您输入的不是数字!!!请重新选择 ");
}
}
else
{
Console.WriteLine(" 您输入的不是数字!!!请重新选择 ");
}
}
else if (s.Equals("2"))
{
ArrayList al = SelectMaxOrMin(2);
if (al.Count == 0)
{
Console.WriteLine(" 此系统教员没有添加此学生哦 ");
}
else
{
foreach (Student student in al)
{
Console.WriteLine(student);
}
}
}
else if (s.Equals("3"))
{
ArrayList al = SelectMaxOrMin(1);
if (al.Count == 0)
{
Console.WriteLine(" 您输入的学号不存在!教员没有添加此学生哦 ");
}
else
{
foreach (Student student in al)
{
Console.WriteLine(student);
}
}
}
else if (s.Equals("4"))
{
ShowMain123();
break;
}
else
{
Console.WriteLine(" 输入错误!请输入1--4的数字 !!! ");
}
}
}
/// <summary>
/// 三级菜单第一1个 学员查看
/// </summary>
private void ShowMain311()
{
while (true)
{
Console.WriteLine("%----------------------------%");
Console.WriteLine(" 1.根据学号查看信息 ");
Console.WriteLine(" 2.查看最高分学员 ");
Console.WriteLine(" 3.查看最低分学员 ");
Console.WriteLine(" 4.返回上级菜单 ");
Console.WriteLine(" 请输入1--4的数字 ");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
String s = Console.ReadLine().Trim();
if (s.Equals("1"))
{
Console.WriteLine(" 请输入您要查找的学生的学号: ");
string s2 = Console.ReadLine().Trim();
if (s2.Length > 0)
{
Match ma = reg.Match(s2);
if (ma.Success)
{
Student s3 = SelectStudentById(int.Parse(s2));
if (s3 == null)
{
Console.WriteLine(" 您输入的学号不存在!教员没有添加此学生哦 ");
}
else
{
Console.WriteLine(s3);
}
}
else
{
Console.WriteLine(" 您输入的不是数字!!!请重新选择 ");
}
}
else
{
Console.WriteLine(" 您输入的不是数字!!!请重新选择 ");
}
}
else if (s.Equals("2"))
{
ArrayList al = SelectMaxOrMin(2);
if (al.Count == 0)
{
Console.WriteLine(" 教员没有添加此学生哦 ");
}
else
{
foreach (Student student in al)
{
Console.WriteLine(student);
}
}
}
else if (s.Equals("3"))
{
ArrayList al = SelectMaxOrMin(1);
if (al.Count == 0)
{
Console.WriteLine(" 教员没有添加此学生哦 ");
}
else
{
foreach (Student student in al)
{
Console.WriteLine(student);
}
}
}
else if (s.Equals("4"))
{
ShowMain124();
break;
}
else
{
Console.WriteLine(" 输入错误!请输入1--4的数字 !!! ");
}
}
}
#endregion
#region 查询单个学员根据学号SelectStudentById(int i)
/// <summary>
/// 查询单个学员根据学号
/// </summary>
/// <param name="i">学号</param>
public Student SelectStudentById(int i)
{
foreach (Student s in ls)
{
if (s.Id == i)
{
return s;
}
}
return null;
}
#endregion
#region 查询单个学员最高分或最低分学员SelectMaxOrMin(int i)
/// <summary>
/// 查询最高分或最低分学员
/// </summary>
/// <param name="i">1代表最低分,2代表最高分</param>
public ArrayList SelectMaxOrMin(int i)
{
ArrayList ls2 = new ArrayList();
int y = i == 1 ? 1000 : -1;
if (i == 1)
{
foreach (Student s in ls)
{
if (s.Grade < y)
{
y = s.Grade;
}
}
}
else
{
foreach (Student s in ls)
{
if (s.Grade > y)
{
y = s.Grade;
}
}
}
foreach (Student s in ls)
{
if (s.Grade == y)
{
ls2.Add(s);
}
}
return ls2;
}
#endregion
#region 三级菜单第二个 查看所有学员信息
/// <summary>
/// 三级菜单第二个 教员查看
/// </summary>
private void ShowMain32()
{
while (true)
{
Console.WriteLine("%----------------------------%");
Console.WriteLine(" 1.根据年龄排序 ");
Console.WriteLine(" 2.根据分数排序 ");
Console.WriteLine(" 3.根据学号排序 ");
Console.WriteLine(" 4.查看总分与平均分 ");
Console.WriteLine(" 5.返回上级菜单 ");
Console.WriteLine(" 请输入1--5的数字 ");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
string s = Console.ReadLine().Trim();
if (s.Equals("1"))
{
ArrayList al = SelectStudentAgeGradeId("age");
if (al.Count == 0)
{
Console.WriteLine(" 教员没有添加此学生哦 ");
}
else
{
foreach (Student student in al)
{
Console.WriteLine(student);
}
}
}
else if (s.Equals("2"))
{
ArrayList al = SelectStudentAgeGradeId("grade");
if (al.Count == 0)
{
Console.WriteLine(" 教员没有添加此学生哦 ");
}
else
{
foreach (Student student in al)
{
Console.WriteLine(student);
}
}
}
else if (s.Equals("3"))
{
ArrayList al = SelectStudentAgeGradeId("id");
if (al.Count == 0)
{
Console.WriteLine(" 教员没有添加此学生哦 ");
}
else
{
foreach (Student student in al)
{
Console.WriteLine(student);
}
}
}
else if (s.Equals("4"))
{
ArrayList al = SelectStudentAgeGradeId("id");
if (al.Count == 0)
{
Console.WriteLine(" 教员没有添加此学生哦 ");
}
else
{
int[] i = SelectSumAndAvg();
Console.WriteLine("总分:" + i[0] + ",平均分:" + i[1]);
}
}
else if (s.Equals("5"))
{
ShowMain123();
break;
}
else
{
Console.WriteLine(" 输入错误!请输入1--5的数字 ");
}
}
}
/// <summary>
/// 三级菜单第二1个 教员查看
/// </summary>
private void ShowMain322()
{
while (true)
{
Console.WriteLine("%----------------------------%");
Console.WriteLine(" 1.根据年龄排序 ");
Console.WriteLine(" 2.根据分数排序 ");
Console.WriteLine(" 3.根据学号排序 ");
Console.WriteLine(" 4.查看总分与平均分 ");
Console.WriteLine(" 5.返回上级菜单 ");
Console.WriteLine(" 请输入1--5的数字 ");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
string s = Console.ReadLine().Trim();
if (s.Equals("1"))
{
ArrayList al = SelectStudentAgeGradeId("age");
if (al.Count == 0)
{
Console.WriteLine(" 教员没有添加此学生哦 ");
}
else
{
foreach (Student student in al)
{
Console.WriteLine(student);
}
}
}
else if (s.Equals("2"))
{
ArrayList al = SelectStudentAgeGradeId("grade");
if (al.Count == 0)
{
Console.WriteLine(" 教员没有添加此学生哦 ");
}
else
{
foreach (Student student in al)
{
Console.WriteLine(student);
}
}
}
else if (s.Equals("3"))
{
ArrayList al = SelectStudentAgeGradeId("id");
if (al.Count == 0)
{
Console.WriteLine(" 教员没有添加此学生哦 ");
}
else
{
foreach (Student student in al)
{
Console.WriteLine(student);
}
}
}
else if (s.Equals("4"))
{
ArrayList al = SelectStudentAgeGradeId("id");
if (al.Count == 0)
{
Console.WriteLine(" 教员没有添加此学生哦 ");
}
else
{
int[] i = SelectSumAndAvg();
Console.WriteLine("总分:" + i[0] + ",平均分:" + i[1]);
}
}
else if (s.Equals("5"))
{
ShowMain124();
break;
}
else
{
Console.WriteLine(" 输入错误!请输入1--5的数字 ");
}
}
}
#endregion
#region 查询所有学员并根据年龄或分数或学号排序 SelectStudentAgeGradeId(string str)
/// <summary>
/// 查询所有学员并根据年龄或分数或学号排序
/// </summary>
public ArrayList SelectStudentAgeGradeId(string str)
{
//年龄排序
if (str.Equals("age"))
{
for (int i = 0; i < ls.Count - 1; i++)
{
for (int y = i + 1; y < ls.Count; y++)
{
Student s = (Student)ls[i];
Student s2 = (Student)ls[y];
if (s.Age < s2.Age)
{
Student s3 = s;
ls[i] = s2;
ls[y] = s3;
}
}
}
}
//分数排序
else if (str.Equals("grade"))
{
for (int i = 0; i < ls.Count - 1; i++)
{
for (int y = i + 1; y < ls.Count; y++)
{
Student s = (Student)ls[i];
Student s2 = (Student)ls[y];
if (s.Grade < s2.Grade)
{
Student s3 = s;
ls[i] = s2;
ls[y] = s3;
}
}
}
}
else
{
//学号排序
for (int i = 0; i < ls.Count - 1; i++)
{
for (int y = i + 1; y < ls.Count; y++)
{
Student s = (Student)ls[i];
Student s2 = (Student)ls[y];
if (s.Id > s2.Id)
{
Student s3 = s;
ls[i] = s2;
ls[y] = s3;
}
}
}
}
return ls;
}
#endregion
#region 查询总分和平均分SelectSumAndAvg()
/// <summary>
/// 查询总分和平均分,先返回总分再返回平均分
/// </summary>
public int[] SelectSumAndAvg()
{
int[] x = new int[2];
int sum = 0;
foreach (Student s in ls)
{
sum += s.Grade;
}
x[0] = sum;
x[1] = sum / ls.Count;
return x;
}
#endregion
}
}
⑤测试类Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BaseXM
{
class Program
{
static void Main(string[] args)
{
StudentManage.AddStudents();
StudentManage sm = new StudentManage();
sm.Login();
Console.ReadKey();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/191257.html原文链接:https://javaforall.cn