I need to print all the contents from the HashMap<String, StudentSchedule>
where StudentSchedule is a class that store other objects. In the StudentSchedule, there is a function that prints out some data which I want it to print when the HashMap is printing. I have used the enhanced for loop to print the HashMap, but the values are like object Number. When the values are printing, i want it to print the function that is in StudentSchedule.printSchedule(). I am not sure if that is possible. I think I have made it more complicated that it should be but here is what I have so far:
public class StudentSchedule {
private Student student;
private Course course;
private String[] courseDays;
private String[] courseTimes;
public StudentSchedule(Student student, Course course, String[] courseDays, String[] courseTimes) {
this.student = student;
this.course = course;
this.courseDays = courseDays;
this.courseTimes = courseTimes;
}
public Student getStudent() {
return student;
}
public Course getCourse() {
return course;
}
public String[] getCourseDays() {
return courseDays;
}
public String[] getCourseTimes() {
return courseTimes;
}
public void printSchedule() {
System.out.println("\nClass Schedule");
String studentInfo = getStudent().toString();
String courseInfo = getCourse().toString();
String[] courseDays = getCourseDays();
String[] courseTimes = getCourseTimes();
if(courseDays[2] != null && courseTimes[2] != null) {
System.out.println(studentInfo + "\n" + courseInfo + "\n" +
courseDays[0] + " -> " + courseTimes[0] + "\n" +
courseDays[1] + " -> " + courseTimes[1] + "\n" +
courseDays[2] + " -> " + courseTimes[2]);
}
else {
System.out.println(studentInfo + "\n" + courseInfo + "\n" +
courseDays[0] + " -> " + courseTimes[0] + "\n" +
courseDays[1] + " -> " + courseTimes[1]);
}
}
}
ScheduleManager
public class ScheduleManager
{
private HashMap<String, StudentSchedule> allStudentSchedule = new HashMap<>();
public void createSchedule() {
Student newStudent = getStudentInfo();
Course newCourse = getCourseInfo();
courseDays = getCourseDays();
courseTimes = getCourseTimes(courseDays);
studentSchedule = new StudentSchedule(newStudent, newCourse, courseDays, courseTimes);
allStudentSchedule.put(newStudent.getStudentId(), studentSchedule);
// testing print schedule
studentSchedule.printSchedule();
}
private void displaySchedule() {
Set<Entry<String,StudentSchedule>> hashSet = allStudentSchedule.entrySet();
for(Entry entry : hashSet ) {
System.out.println("Key="+entry.getKey()+", Value="+entry.getValue());
}
}
....