How to display a category with its associated courses in Java

My code uses two classes: Section and Lesson with matching IDs. I want a neat output showing each Section with its related Lesson.

class Section {
  int secId;
  String title;
  List<Lesson> lessonSet;

  Section(int secId, String title, List<Lesson> lessonSet) {
    this.secId = secId;
    this.title = title;
    this.lessonSet = lessonSet;
  }
  public String getSummary() {
    return title + " " + lessonSet;
  }
}

class Lesson {
  int lessonNum;
  int secId;
  String topic;

  Lesson(int lessonNum, int secId, String topic) {
    this.lessonNum = lessonNum;
    this.secId = secId;
    this.topic = topic;
  }
  public String showTopic() {
    return topic;
  }
}

class Runner {
  public static void main(String[] args) {
    List<Lesson> lessons = new ArrayList<>();
    lessons.add(new Lesson(101, 1, "Mathematics"));
    lessons.add(new Lesson(102, 2, "Literature"));

    List<Section> sections = new ArrayList<>();
    sections.add(new Section(1, "STEM", lessons));
    sections.add(new Section(2, "Arts", lessons));

    for (Section s : sections) {
      System.out.println(s.getSummary());
    }
  }
}

i like overriding the toString method on your objects. each section then prints its own lessons neatly. keeps main clean and is a simple fix that works well in my projects.

In my experience, achieving a neat output is easier when you design your code around the relationship between sections and lessons. One effective approach is to group lessons by their section ID before printing. This way, you can display only the relevant lessons for each section, which keeps the output clear and readable. Consider creating a helper method or using streams for filtering within the Runner class. Personal experimentation showed that separating concerns like this not only simplifies the display logic but also makes maintaining the code far more straightforward in the long run.