cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace _3.List学生管理系统
{
internal class Program
{
static List<Student> list = new List<Student>
{
new Student(){Id = 1,Name ="凡凡",Age =24,Gender ="男" ,Height=1.80},
new Student(){Id = 3,Name ="爽爽",Age =18,Gender ="女" ,Height=1.73},
new Student(){Id = 2,Name ="坤坤",Age =21,Gender ="男" ,Height=1.70}
};
static MyComparer<Student> comparer = new MyComparer<Student>();
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("输入你需要的功能\n1.添加 2.删除 3.显示所有 4.查找单个 5.修改 6.排序 7.退出");
string a=Console.ReadLine();
switch (a)
{
case "1":
AddStudent();
Console.WriteLine("添加操作完成");
break;
case "2":
Console.WriteLine("输入你需要删除的学生学号");
string id1 =Console.ReadLine() ;
Cunzai(id1);
DeleteStudent(id1);
Console.WriteLine("删除操作完成");
break;
case "3":
DisplayStudent();
Console.WriteLine("显示所有学生操作完成");
break;
case "4":
Console.WriteLine("输入单个查询的学生学号");
string id=Console.ReadLine();
Cunzai(id);
SearchStudent(id);
Console.WriteLine("查询单个学生操作完成");
break;
case "5":
Console.WriteLine("输入修改信息的学生学号");
string id2 = Console.ReadLine();
Cunzai(id2);
ModifyStudent(id2);
Console.WriteLine("修改单个学生操作完成");
break;
case "6":
Console.WriteLine("输入你需要的选项:1.根据学号排序 2.根据年龄排序 3.根据身高排序");
string b=Console.ReadLine();
Paixu(b);
break;
case "7":
Console.WriteLine("退出成功");
return;
default:
Console.WriteLine("你输入的不符合规范");
break;
}
}
}
/// <summary>
/// 根据输入的选项对学生排序
/// </summary>
/// <param name="pai"></param>
static void Paixu(string pai)
{
switch (pai)
{
case "1":
comparer.Sort(list);
DisplayStudent1(list);
Console.WriteLine("根据学号排序操作完成");
break;
case "2":
comparer.Sort1(list);
DisplayStudent1(list);
Console.WriteLine("根据年龄排序操作完成");
break;
case "3":
comparer.Sort2(list);
DisplayStudent1(list);
Console.WriteLine("根据身高排序操作完成");
break;
default:
Console.WriteLine("输入的选项不存在");
break;
}
}
/// <summary>
/// 显示排序后的学生
/// </summary>
/// <param name="list"></param>
static void DisplayStudent1(List<Student> list)
{
foreach (var item in list)
{
Console.WriteLine(item);
}
}
/// <summary>
/// 确保学号唯一性
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
static string Weiyi(string id)
{
if (id != "")
{
foreach (var item in list)
{
if (item.Id == int.Parse(id))
{
return "false";
}
}
}
else
{
return "-1";
}
return "true";
}
/// <summary>
/// 检查该学号学生是否存在
/// </summary>
/// <param name="id">输入的学号</param>
static void Cunzai(string id)
{
if(Weiyi(id)=="true")
{
Console.WriteLine("该学号不存在");
return;
}
else if(Weiyi(id)=="-1")
{
throw new Exception("请不要输入空字符串");
}
}
/// <summary>
/// 添加学生
/// </summary>
static void AddStudent()
{
Console.WriteLine("输入学号");
string id=Console.ReadLine();
Console.WriteLine("输入姓名");
string name = Console.ReadLine();
Console.WriteLine("输入年龄");
string age = Console.ReadLine();
Console.WriteLine("输入性别(女:0\t男:1)");
string gender = Console.ReadLine();
Console.WriteLine("输入身高(m)");
string height=Console.ReadLine();
try
{
if(Weiyi(id)=="true")
{
if(double.Parse(height)>2.50)
{
Console.WriteLine("身高输入不符合规范");
return;
}
Student student = new Student()
{
Id = int.Parse(id),
Name = name,
Age = int.Parse(age),
Gender = gender == "0" ? "女" : "男",
Height=double.Parse(height)
};
list.Add(student);
Console.WriteLine("添加成功");
}
else
{
Console.WriteLine("此学号已被使用,添加失败");
}
}
catch
{
Console.WriteLine("输入格式有误");
}
}
/// <summary>
/// 显示所有学生
/// </summary>
static void DisplayStudent()
{
var student =from x in list
select x.ToString();
foreach (var item in student)
{
Console.WriteLine(item.ToString());
}
}
/// <summary>
/// 删除单个学生
/// </summary>
static void DeleteStudent(string id)
{
for (int i = 0; i <list.Count; i++)
{
if (list[i].Id==int.Parse(id))
{
list.RemoveAt(i);
Console.WriteLine("删除成功");
}
}
}
/// <summary>
/// 显示单个学生信息
/// </summary>
/// <param name="id"></param>
static void SearchStudent(string id)
{
var student = from x in list
where x.Id == int.Parse(id)
select x;
foreach (var item in student)
{
Console.WriteLine(item.ToString());
}
}
/// <summary>
/// 修改单个学生信息
/// </summary>
/// <param name="id"></param>
static void ModifyStudent(string id)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].Id == int.Parse(id))
{
Console.WriteLine("不修改的信息直接跳过");
Console.WriteLine("输入修改的姓名");
string name = Console.ReadLine();
Console.WriteLine("输入修改的年龄");
string age = Console.ReadLine();
Console.WriteLine("输入修改的性别(女:0\t男:1)");
string gender = Console.ReadLine();
Console.WriteLine("输入身高(m)");
string height = Console.ReadLine();
try
{
if (double.Parse(height) > 2.50)
{
Console.WriteLine("身高输入不符合规范");
return;
}
list[i].Name = name == "" ? list[i].Name : name;
list[i].Age = age == "" ? list[i].Age : int.Parse(age);
list[i].Gender = gender == "" ? list[i].Gender : gender == "0" ? "女" : "男";
list[i].Height = height == "" ? list[i].Height : double.Parse(height);
Console.WriteLine("修改成功");
}
catch (Exception)
{
Console.WriteLine("修改输入格式有误");
}
}
}
}
}
}
创建的学生类
cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _3.List学生管理系统
{
public interface IComParable<T> //接口
{
int ComPareTo(T other); //在list的数组中,想对几种数据进行排列,就要写几个方法
int ComPareTo1(T other); //这里以三种数据举例:分别是学生的ID,学生的年龄,学生的身高
int ComPareTo2(T other);
}
public class Student:IComParable<Student> //继承接口的学生类
{
public int Id { get; set; } //学生的学号
public string Name { get; set; }
public int Age { get; set; } //学生的年龄
public string Gender { get; set; }
public double Height { get; set; } //学生的身高
//public Student(int id, string name, int age, string gender) 这里作者不需要构造函数所以就注释掉了
//{
// Id = id;
// Name = name;
// Age = age;
// Gender = gender;
//}
/// <summary>
/// 根据ID排序
/// </summary>
/// <param name="other">传入学生类</param>
/// <returns>返回排序结果</returns>
public int ComPareTo(Student other) //根据ID排列
{
if (other == null) return 1;
return this.Id.CompareTo(other.Id);
}
/// <summary>
/// 根据年龄排序
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int ComPareTo1(Student other) //根据年龄排列
{
if (other == null) return 1;
return this.Age.CompareTo(other.Age);
}
/// <summary>
/// 根据身高排序
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int ComPareTo2(Student other) //根据身高排列
{
if (other == null) return 1;
return this.Height.CompareTo(other.Height);
}
public override string ToString() //对ToString进行重写
{
return $"学号:{Id}\t姓名:{Name}\t年龄:{Age}\t性别:{Gender}\t身高{String.Format("{0:F2}",Height)}米";
}
}
/// <summary>
/// 使用接口的约束
/// </summary>
/// <typeparam name="T"></typeparam>
internal class MyComparer<T> where T : IComParable<T>
{
//对列表进行排序
public void Sort(List<T> list) //根据ID排列
{
//Sort可以立即为只能对值类型的数据进行比较,没办法比较复杂类型
list.Sort((x, y) => x.ComPareTo(y));
}
public void Sort1(List<T> list) //根据年龄排列
{
//Sort可以立即为只能对值类型的数据进行比较,没办法比较复杂类型
list.Sort((x, y) => x.ComPareTo1(y));
}
public void Sort2(List<T> list) //根据身高排列
{
//Sort可以立即为只能对值类型的数据进行比较,没办法比较复杂类型
list.Sort((x, y) => x.ComPareTo2(y));
}
}
}