-2

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());
        }

    }
....
bubbles2189
  • 149
  • 1
  • 2
  • 15

1 Answers1

0

override the toString method of your StudentSchedule class and call printSchedule in it. Something like this

@Override
public String toString() { 
    printSchedule();
    return ""; 
} 
nupadhyaya
  • 1,909
  • 1
  • 14
  • 14
  • So in the displaySchedule() method, I should add Value="+entry.getValue().toString()); so that it could printSchedule(); – bubbles2189 Sep 21 '18 at 23:01
  • No.. Just call entry.getValue(). This will return StudentSchedule object. When you try to print an object, the toString method gets called by default. When you didn't override the toString, it printed the object hashcode. Now that you have overriden the toString, the overriden toString will get executed which will call printSchedule – nupadhyaya Sep 21 '18 at 23:03
  • ok, so when I tried that, I got the value as "blank", I'll try to add the code from printSchedule() and return it as a string intstead. – bubbles2189 Sep 21 '18 at 23:05
  • Yup, it works when I add printSchedule() code inside of the toString() and return it instead. Thanks! – bubbles2189 Sep 21 '18 at 23:08