目录
题目内容编辑
完整源码
题目内容
- 根据下图所示数据,定义学生类Student,设置对应的字段并进行封装
- 在Test中,定义ArrayList集合 ,将上述学生对象实例化,并放入集合,
- 定义方法t1,参数为学生类集合,功能是输出显示所有学生信息
- 定义方法t2,参数为学生类集合,班级和性别,实现按照班级和性别查询功能。主方法中调用该方法,实现查询网络技术101班级的女生信息
- 定义方法t3,实现按姓名模糊查询功能,如果存在符合条件的学生,返回相关学生信息,否则输出没有数据
- 定义方法t4, 返回类型为学生类集合,参数为学生类对象,实现功能是修改学生信息。主方法中调用该方法,实现将班级为”电子商务111”的学生修改成电子商务101
- 定义方法t5, 返回类型为学生类集合,参数也是学生类集合,实现按照出生日期降序排列的功能
完整源码
Student.java
public class Student {private String studentId; //学号private String name; //姓名private String gender; //性别private String birthdate; //出生日期private String major; //专业名字public Student() {}public Student(String studentId, String name, String gender, String birthdate, String major) {this.studentId = studentId;this.name = name;this.gender = gender;this.birthdate = birthdate;this.major = major;}public void setStudentId(String studentId) {this.studentId = studentId;}public void setName(String name) {this.name = name;}public void setGender(String gender) {this.gender = gender;}public void setBirthdate(String birthdate) {this.birthdate = birthdate;}public void setMajor(String major) {this.major = major;}public String getStudentId() {return studentId;}public String getName() {return name;}public String getGender() {return gender;}public String getBirthdate() {return birthdate;}public String getMajor() {return major;}
}
Main.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;public class Main {public static void main(String[] args) {List<Student> students = new ArrayList<>();//2, 在Test中,定义ArrayList集合 ,将上述学生对象实例化,并放入集合,students.add(new Student("10101001", "张永峰", "男", "1993-08-01", "电子商务101"));students.add(new Student("10101002", "何小丽", "女", "1992-11-03", "电子商务101"));students.add(new Student("10101003", "张宇", "男", "1992-08-21", "电子商务101"));students.add(new Student("10102001", "王斌", "男", "1991-07-14", "网络技术101"));students.add(new Student("10109002", "孙立金", "男", "1993-11-15", "网络技术101"));students.add(new Student("10102004", "翁静静", "女", "1992-02-27", "网络技术101"));students.add(new Student("11101001", "刘淑芳", "女", "1994-06-10", "电子商务111"));students.add(new Student("11101002", "王亚旭", "男", "1993-03-18", "电子商务111"));students.add(new Student("11101003", "高磊", "男", "1993-05-11", "电子商务111"));t1(students);List<Student> filteredStudents = t2(students, "网络技术101", "女");System.out.println("按照班级和性别查询结果:");t1(filteredStudents);String searchKeyword = "张";List<Student> searchResult = t3(students, searchKeyword);if (searchResult.isEmpty()) {System.out.println("没有符合条件的学生数据。");} else {System.out.println("按姓名模糊查询结果:");t1(searchResult);}List<Student> updatedStudents = t4(students, "电子商务111", "电子商务101");System.out.println("修改学生信息后的结果:");t1(updatedStudents);List<Student> sortedStudents = t5(students);System.out.println("按出生日期降序排列的结果:");t1(sortedStudents);}//3, 定义方法t1,参数为学生类集合,功能是输出显示所有学生信息public static void t1(List<Student> students) {System.out.println("学生信息:");for (Student student : students) {System.out.println("学号:" + student.getStudentId());System.out.println("姓名:" + student.getName());System.out.println("性别:" + student.getGender());System.out.println("出生日期:" + student.getBirthdate());System.out.println("专业:" + student.getMajor());System.out.println();}}//4, 定义方法t2,参数为学生类集合,班级和性别,实现按照班级和性别查询功能。//主方法中调用该方法,实现查询网络技术101班级的女生信息public static List<Student> t2(List<Student> students, String major, String gender) {List<Student> filteredStudents = new ArrayList<>();for (Student student : students) {if (student.getMajor().equals(major) && student.getGender().equals(gender)) {filteredStudents.add(student);}}return filteredStudents;}//5, 定义方法t3,实现按姓名模糊查询功能,如果存在符合条件的学生,返回相关学生信息,否则输出没有数据public static List<Student> t3(List<Student> students, String searchKeyword) {List<Student> searchResult = new ArrayList<>();for (Student student : students) {if (student.getName().contains(searchKeyword)) {searchResult.add(student);}}return searchResult;}//6, 定义方法t4, 返回类型为学生类集合,参数为学生类对象,实现功能是修改学生信息。//主方法中调用该方法,实现将班级为”电子商务111”的学生修改成电子商务101public static List<Student> t4(List<Student> students, String originalMajor, String newMajor) {List<Student> updatedStudents = new ArrayList<>();for (Student student : students) {if (student.getMajor().equals(originalMajor)) {student = new Student(student.getStudentId(), student.getName(), student.getGender(),student.getBirthdate(), newMajor);}updatedStudents.add(student);}return updatedStudents;}//7, 定义方法t5, 返回类型为学生类集合,参数也是学生类集合,实现按照出生日期降序排列的功能public static List<Student> t5(List<Student> students) {List<Student> sortedStudents = new ArrayList<>(students);Collections.sort(sortedStudents, Comparator.comparing(Student::getBirthdate).reversed());return sortedStudents;}
}