//学生管理系统
public class StudentSystem {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
loop:
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("请输入您的选择:");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose) {
case "1" -> addStudent(list);
case "2" -> deletesStudent(list);
case "3" -> updateStudent(list);
case "4" -> queryStudent(list);
case "5" -> {
System.out.println("5:退出");
//System.exit(0);
break loop;
}
default -> System.out.println("没有这个选项");
}
}
}
/**
* 添加学生
*/
public static void addStudent(ArrayList<Student> list) {
Student stu = new Student();
System.out.println("添加学生");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入学生的id:");
String id = sc.next();
boolean falg = contians(list, id);
if (falg) {
//id存在,需要重新录入
System.out.println("id存在,需要重新录入");
} else {
stu.setId(id);
break;
}
}
System.out.println("请输入学生的姓名:");
String name = sc.next();
stu.setName(name);
System.out.println("请输入学生的年龄:");
int age = sc.nextInt();
stu.setAge(age);
System.out.println("请输入学生的家庭住址:");
String address = sc.next();
stu.setAddress(address);
list.add(stu);
System.out.println("学生信息添加成功!!!");
}
/**
* 删除学生
*/
public static void deletesStudent(ArrayList<Student> list) {
System.out.println("删除学生");
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除学生的id:");
String id = sc.next();
int index = getIndex(list, id);
if (index == -1) {
System.out.println("id为:" + id + "不存在");
}
//id存在,删除学生信息
list.remove(index);
System.out.println("id为:" + id + "学生信息删除成功!!!");
}
/**
* 修改学生
*/
public static void updateStudent(ArrayList<Student> list) {
System.out.println("修改学生");
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改学生的id:");
String id = sc.next();
int index = getIndex(list, id);
if (index == -1) {
//id不存在,提示不存在id
System.out.println("id为:" + id + "不存在");
return;
}
Student stu = list.get(index);
//id存在,继续录入其他信息
System.out.println("请输入要修的学生的姓名:");
String newname = sc.next();
stu.setName(newname);
System.out.println("请输入要修的学生的年龄:");
int newage = sc.nextInt();
stu.setAge(newage);
System.out.println("请输入要修的学生的家庭住址:");
String newaddrss = sc.next();
stu.setAddress(newaddrss);
System.out.println("id为:" + id + "学生信息修改成功!!!");
}
/**
* 查询学生
*/
public static void queryStudent(ArrayList<Student> list) {
System.out.println("查询学生");
if (list.size() == 0) {
System.out.println("当前无学生信息,请添加后再查询");
return;
}
//代码执行到这里表示有学生信息
System.out.println("id\t" + "姓名\t" + "年龄\t" + "家庭住址\t");
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
}
}
/**
* 判断id是否唯一
*
* @param list
* @param id
* @return
*/
public static boolean contians(ArrayList<Student> list, String id) {
return getIndex(list, id) >= 0;
}
/**
* 通过id获取索引
*
* @param list
* @param id
* @return
*/
public static int getIndex(ArrayList<Student> list, String id) {
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
if (stu.getId().equals(id)) {
//id已经存在
return i;
}
}
return -1;
}
}
//学生类
public class Student {
private String id;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
/**
* 获取
* @return id
*/
public String getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return address
*/
public String getAddress() {
return address;
}
/**
* 设置
* @param address
*/
public void setAddress(String address) {
this.address = address;
}
}
学生管理系统
最新推荐文章于 2024-08-20 00:24:53 发布