学生类
package StuOfMan;
public class Student {
private String sname;
private String sid;
private String sgender;
public Student() {
}
public Student(String sname, String sid, String sgender) {
this.sname = sname;
this.sid = sid;
this.sgender = sgender;
}
public Student(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSgender() {
return sgender;
}
public void setSgender(String sgender) {
this.sgender = sgender;
}
@Override
public boolean equals(Object obj) {
Student student = (Student)obj;
return this.sid.equals(student.sid);
}
@Override
public String toString() {
return "[sname=" + sname + ", sid=" + sid + ", sgender=" + sgender + "]";
}
}
测试类
package StuOfMan;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
while (true) {
System.out.println("欢迎使用学生管理系统");
System.out.print("1:添加学生");
System.out.print("2:修改学生");
System.out.print("3:删除学生");
System.out.print("4:查询学生");
System.out.print("5:查询所有学生信息");
System.out.println("6:退出系统");
System.out.println("输入要操作的选项:");
Scanner sc = new Scanner(System.in);
int option = sc.nextInt();
switch (option) {
case 1:
addStu(list);
break;
case 2:
setStu(list);
break;
case 3:
delStu(list);
break;
case 4:
lookStu(list);
break;
case 5:
lookAll(list);
break;
case 6:
System.out.println("已退出系统,欢迎下次使用!");
return;
default:
System.out.println("没有该操作选项!");
break;
}
}
}
//输入的方法
public static String shuRu() {
Scanner shuRu = new Scanner(System.in);
return shuRu.nextLine();
}
//添加学生的方法
public static void addStu(List<Student> list) {
System.out.println("输入学生姓名:");
String sname = shuRu();
System.out.println("输入学生学号:");
String sid = shuRu();
System.out.println("输入学生性别:");
String sgender = shuRu();
Student stu = new Student(sname,sid,sgender);
if(list.contains(stu)) {
System.out.println("学号重复,程序终止!");
return;
}
if(list.add(stu)) {
System.out.println("添加成功");
}
return;
}
//修改学生的方法
public static void setStu(List<Student> list) {
System.out.println("输入要修改学生的学号:");
String sid = shuRu();
int index = list.indexOf(new Student(sid));
if(index == -1) {
System.out.println("学号不存在,操作终止!");
return;
}
Student stu = list.get(index);
System.out.println("请输入新姓名:");
String name = shuRu();
stu.setSname(name);
System.out.println("请输入新性别:");
String gender = shuRu();
stu.setSgender(gender);
System.out.println("修改成功!");
return;
}
//删除学生信息
public static void delStu(List<Student> list) {
System.out.println("输入要删除学生的学号:");
String sid = shuRu();
if(!list.contains(new Student(sid))) {
System.out.println("学号不存在,操作终止!");
return;
}
if(list.remove(new Student(sid))) {
System.out.println("删除成功!");
}
return;
}
//查询学生信息
public static void lookStu(List<Student> list) {
System.out.println("输入姓名:");
String sname = shuRu();
int count = 0;
for (Student stu : list) {
if(stu.getSname().equals(sname)) {
System.out.println(stu);
count++;
}
}
if(count == 0) {
System.out.println("系统中未找到姓名为" + sname +"的信息!");
}
return;
}
//查询所有学生的信息
public static void lookAll(List<Student> list) {
for (Student stu : list) {
System.out.println(stu.toString());
}
}
}