简易版学生管理系统。能够在控制台向集合中添加,删除,修改,查看学生信息;根据学生学号判断集合中是否存在学生信息,并给出提示;
先提前创建一个学生类,包含学号,姓名,年龄,生日属性,再在测试类中实现;
学生类:
public class Student {
private String sid;
private String name;
private int age;
private String birthday;
public Student() {
}
public Student(String sid, String name, int age, String birthday) {
this.sid = sid;
this.name = name;
this.age = age;
this.birthday = birthday;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
测试类:
public static void main(String[] args) {
// 1. 搭建主界面菜单
ArrayList<Student> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
lo:
while (true) {
System.out.println("--------欢迎来到学生管理系统--------");
System.out.println("1 添加学生");
System.out.println("2 删除学生");
System.out.println("3 修改学生");
System.out.println("4 查看学生");
System.out.println("5 退出");
System.out.println("请输入您的选择:");
String choice = sc.next();
switch (choice) {
case "1":
//System.out.println("添加学生");
addStudent(list);
break;
case "2":
//System.out.println("删除学生");
deleteStudent(list);
break;
case "3":
//System.out.println("修改学生");
updateStudent(list);
break;
case "4":
//System.out.println("查看学生");
queryStudent(list);
break;
case "5":
System.out.println("感谢您的使用");
break lo;
default:
System.out.println("您的输入有误");
break;
}
}
}
//根据学号修改学生信息方法
public static void updateStudent(ArrayList<Student> list) {
//给出提示(输入要修改的学生学号)
System.out.println("请输入您要修改的学生学号");
//键盘接收要修改的学号
Scanner sc = new Scanner(System.in);
String updateSid = sc.next();
//调用gexIndex方法,判断要修改的学号是否存在
int index = getIndex(list, updateSid);
//返回为-1说明不存在
if (index == -1) {
//不存在:给出提示
System.out.println("查无信息,请重新输入");
} else {
//存在,接受新信息
System.out.println("请输入新的姓名");
String name = sc.next();
System.out.println("请输入新的年龄");
int age = sc.nextInt();
System.out.println("请输入新的生日");
String birthday = sc.next();
/*
创建新的学生对象,再替换集合中的学生对象
Student stu = new Student(updateSid, name, age, birthday);
list.set(index,stu);
*/
//直接根据索引更改学生信息
Student stu = list.get(index);
stu.setName(name);
stu.setAge(age);
stu.setBirthday(birthday);
System.out.println("修改成功");
}
}
//根据学号删除学生
public static void deleteStudent(ArrayList<Student> list) {
//给出提示(输入要删除的学生学号)
System.out.println("请输入您要删除的学生学号");
//键盘接收要删除的学号
Scanner sc = new Scanner(System.in);
String deleteSid = sc.next();
//调用gexIndex方法,判断要删除的学号是否存在
int index = getIndex(list, deleteSid);
//返回为-1说明不存在
if (index == -1) {
System.out.println("查无信息,请重新输入");
} else {
//返回其他则存在,并提示删除成功
list.remove(index);
System.out.println("删除成功!");
}
}
//根据学号查看学生信息
public static void queryStudent(ArrayList<Student> list) {
int size = list.size();
if (size == 0) {
System.out.println("无信息,请添加后重新查询");
return;
}
//展示表头数据
System.out.println("学号\t姓名\t年龄\t生日");
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday());
}
}
//往集合中添加学生对象
public static void addStudent(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
String sid;
while (true) {
// 1. 给出录入的提示信息
System.out.println("请输入学号:");
sid = sc.next();
int index = getIndex(list, sid);
if (index == -1) {
//学号不存在,可以使用
break;
}
}
System.out.println("请输入姓名:");
String name = sc.next();
System.out.println("请输入年龄:");
int age = sc.nextInt();
System.out.println("请输入生日:");
String birthday = sc.next();
//将键盘录入的数据封装为学生对象
Student stu = new Student(sid, name, age, birthday);
//把学生对象添加进集合
list.add(stu);
//添加成功提示
System.out.println("添加成功");
}
//判断集合中是否有学生对象
public static int getIndex(ArrayList<Student> list, String sid) {
//假设传入的学号不存在,初始为-1;
int index = -1;
//遍历集合获取每一个学生对象,准备进行查找
for (int i = 0; i < list.size(); i++) {
//获取学生对象
Student stu = list.get(i);
//获取学生学号
String id = stu.getSid();
//判断学生学号与传入的是否相等
if (id.equals(sid)) {
//如果相等就把正确索引让index记录
index = i;
}
}
//如果不正确就返回-1
return index;
}
运行结果: