C#学员管理系统
C#学员管理系统是在控制台输出的项目,和OOP学员管理系统相似。
① 创建一个学员的实体类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();