public static void searchStudentByCollege(Map<String,College>colleges) { Scanner scanner= new Scanner(System.in); System.out.print("请输入要查询的学院名称:"); String collegeName = scanner.next(); if (colleges.containsKey(collegeName)) { College college = colleges.get(collegeName); List<ClassInfo> classInCollege = college.getAllClasses(); System.out.println("------ 学生信息 ------"); for (ClassInfo classInfo : classInCollege) { System.out.println(classInfo); List<Student> studentsInCollege = college.getStudentsByCollege(); for (Student student : studentsInCollege) { System.out.println(student); } } System.out.println("----------------------"); } else { System.out.println("未找到该学院的信息!"); } 帮我写出相关的类和构造方法 }
时间: 2024-02-29 09:55:50 浏览: 120
以下是相关的类和构造方法:
1. College类:
public class College {
private String name;
private List<ClassInfo> classes;
private Map<String, Student> students;
public College(String name, List<ClassInfo> classes, Map<String, Student> students) {
this.name = name;
this.classes = classes;
this.students = students;
}
public String getName() {
return name;
}
public List<ClassInfo> getAllClasses() {
return classes;
}
public List<Student> getStudentsByCollege() {
return new ArrayList<>(students.values());
}
public void addStudent(Student student) {
this.students.put(student.getId(), student);
}
}
2. ClassInfo类:
public class ClassInfo {
private String name;
private List<Student> students;
public ClassInfo(String name, List<Student> students) {
this.name = name;
this.students = students;
}
public String getName() {
return name;
}
public List<Student> getStudents() {
return students;
}
}
3. Student类:
public class Student {
private String id;
private String name;
private int age;
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
在调用该方法之前,需要先创建相应的College、ClassInfo和Student对象,并将它们组织成相应的数据结构,然后将这些对象存储在一个Map中。调用该方法时,将这个Map作为参数传入即可。
阅读全文