实现功能:
1.添加功能和查看功能:
2.删除功能
3.修改功能
Student.java 文件
Student.java
package ArrayList集合.学生管理系统;
//复制粘贴请把上面两个删了
public class Student {
String stuNumber;
String name;
int age;
String birthDay;
public Student() {
}
public Student(String stuNumber, String name, int age, String birthDay) {
this.stuNumber = stuNumber;
this.name = name;
this.age = age;
this.birthDay = birthDay;
}
public String getStuNumber() {
return stuNumber;
}
public void setStuNumber(String stuNumber) {
this.stuNumber = stuNumber;
}
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;
}
}
StuManage.java文件
package ArrayList集合.学生管理系统;
import java.util.ArrayList;
import java.util.Scanner;
public class StuManage {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Student> list = new ArrayList<>();//主集合
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.退出");
int choice = in.nextInt();
if (choice == 1) {
addStudent(list);
} else if (choice == 2) {
RemoveStu(list);
} else if (choice == 3) {
UpdateStu(list);
} else if (choice == 4) {
ShowAllStu(list);
} else if (choice == 5) {
System.out.println("结束系统");
break;
} else {
System.out.println("请输入正确的命令");
}
}//while循环括号
}//主方法括号
public static void UpdateStu(ArrayList<Student>list){
Scanner Sc = new Scanner(System.in);
System.out.println("请输入要修改的学号");
String number = Sc.next();
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
if (number.equals(stu.getStuNumber())) {
System.out.println("请输入要修改的学生学号");
stu.setStuNumber(Sc.next());
System.out.println("请呼入要修改的学生姓名");
stu.setName(Sc.next());
System.out.println("请呼入要修改的学生年龄");
stu.setAge(Integer.parseInt(Sc.next()));
System.out.println("请呼入要修改的学生生日");
stu.setBirthDay(Sc.next());
}
else {
System.out.println("无此学号");
}
}
}
public static void RemoveStu(ArrayList<Student> list ) {
Scanner Sc = new Scanner(System.in);
System.out.println("请输入要删除的学号");
String number = Sc.next();
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
if (number.equals(stu.getStuNumber())) {
list.remove(stu);
System.out.println("删除成功");
break;
} else {
System.out.println("删除失败,找不到对应的学号");
break;
}
}
}//RemoveStu方法结束括号
private static void addStudent(ArrayList<Student> list) {
Scanner in = new Scanner(System.in);
ArrayList<Student> arr = new ArrayList<>();
while (true) {
System.out.println("请输入学号");
String StuNumber = in.nextLine();
if (judgeRepeat(list, StuNumber)) {
System.out.println("出现重复学号,请重新输入");
continue;
}
System.out.println("请输入姓名");
String name = in.nextLine();
System.out.println("请输入学生年龄");
int age = in.nextInt();
System.out.println("请输入学生生日");
String birthday = in.next();
list.add(new Student(StuNumber, name, age, birthday));
System.out.println("添加成功!");
break;
}
}//adddStudent 方法括号
//@判断是否重复学号 为真即为重复,为假即为不重复
public static boolean judgeRepeat(ArrayList<Student> arr , String StuNumber) {
for (int i = 0; i < arr.size(); i++) {
Student stu = arr.get(i);
if (StuNumber.equals(stu.getStuNumber())) {
return true;
}
}
return false;
}
//展示所有学生方法
public static void ShowAllStu(ArrayList<Student> arr) {
System.out.println("学号 姓名 年龄 生日");
for (int i = 0; i < arr.size(); i++) {
Student stu = arr.get(i);
String StuNumber1 = stu.getStuNumber();
String name = stu.getName();
int age = stu.getAge();
String day = stu.getBirthDay();
System.out.printf("%s",StuNumber1);
System.out.printf(" %s",name);
System.out.printf(" %d",age);
System.out.printf(" %s\n",day);
}
}
}//StuManage类括号