Menu
Coddy logo textTech

E-Learning Platform

Part of the Object Oriented Programming section of Coddy's GO journey — lesson 105 of 107.

challenge icon

Challenge

Easy

Let's build an E-Learning Platform that manages courses, students, and tracks learning progress! You'll combine structs, interfaces, composition, and polymorphism to create a cohesive system where students can enroll in courses and complete lessons.

You'll organize your code across five files:

  • lesson.go: Define a Lesson struct representing individual learning units. Each lesson has an ID, Title, and Duration (in minutes). Include a NewLesson constructor that returns a pointer to a Lesson.
  • course.go: Create a Course struct that contains an ID, Title, Instructor, and a slice of *Lesson pointers. Include a NewCourse constructor and an AddLesson method. Also implement a TotalDuration() int method that returns the sum of all lesson durations in the course.
  • student.go: Define a Student struct with ID, Name, and Email fields. Include a NewStudent constructor.
  • enrollment.go: This is where the progress tracking magic happens. Create an Enrollment struct that links a student to a course and tracks which lessons they've completed. It should have:
    • A pointer to the Student
    • A pointer to the Course
    • A CompletedLessons map (lesson ID to boolean)

    Define a Progressable interface with a Progress() float64 method that returns a percentage (0.0 to 100.0).

    Implement these methods on Enrollment:

    • NewEnrollment(student *Student, course *Course) *Enrollment — constructor that initializes an empty completed lessons map
    • CompleteLesson(lessonID string) string — marks a lesson complete. Returns lesson not found if the lesson doesn't exist in the course, already completed if already done, or completed on success
    • Progress() float64 — returns the percentage of lessons completed (completed/total * 100), or 0.0 if the course has no lessons
  • main.go: Build your platform and process enrollment operations.

    Read a course's ID, title, and instructor. Then read the number of lessons and each lesson's ID, title, and duration. Read a student's ID, name, and email. Create an enrollment linking the student to the course.

    Then read the number of operations. Each operation is either complete [lessonID] or progress. For complete operations, print the result message. For progress operations, print the progress formatted to one decimal place followed by a percent sign.

The following inputs will be provided:

  • Course ID, Title, Instructor (3 lines)
  • Number of lessons, then each lesson's ID, Title, Duration (3 lines each)
  • Student ID, Name, Email (3 lines)
  • Number of operations, then each operation (1 line each)

For example, given:

C001
Go Fundamentals
Jane Smith
3
L001
Variables
30
L002
Functions
45
L003
Structs
60
S001
Alice
alice@learn.com
5
progress
complete L001
progress
complete L001
complete L002

Your output should be:

0.0%
completed
33.3%
already completed
completed

And given:

C100
Python Basics
Bob Teacher
2
L100
Intro
20
L101
Loops
40
S100
Charlie
charlie@edu.com
4
complete L999
complete L100
complete L101
progress

Your output should be:

lesson not found
completed
completed
100.0%

Notice how the Enrollment struct uses composition to link students and courses together, while the Progressable interface allows any type to report its completion status. This design makes it easy to extend—you could add progress tracking to individual lessons or entire learning paths using the same interface!

Try it yourself

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	// Helper function to read a line
	readLine := func() string {
		scanner.Scan()
		return strings.TrimSpace(scanner.Text())
	}

	// Read course information
	courseID := readLine()
	courseTitle := readLine()
	courseInstructor := readLine()

	// TODO: Create the course using NewCourse
	_ = courseID
	_ = courseTitle
	_ = courseInstructor

	// Read number of lessons
	numLessons, _ := strconv.Atoi(readLine())

	// TODO: Read each lesson and add to course
	for i := 0; i < numLessons; i++ {
		lessonID := readLine()
		lessonTitle := readLine()
		lessonDuration, _ := strconv.Atoi(readLine())

		// TODO: Create lesson and add to course
		_ = lessonID
		_ = lessonTitle
		_ = lessonDuration
	}

	// Read student information
	studentID := readLine()
	studentName := readLine()
	studentEmail := readLine()

	// TODO: Create the student using NewStudent
	_ = studentID
	_ = studentName
	_ = studentEmail

	// TODO: Create enrollment linking student to course

	// Read number of operations
	numOps, _ := strconv.Atoi(readLine())

	// Process each operation
	for i := 0; i < numOps; i++ {
		operation := readLine()

		if operation == "progress" {
			// TODO: Call Progress() and print with format "%.1f%%"
			fmt.Println("0.0%") // Replace with actual progress
		} else if strings.HasPrefix(operation, "complete ") {
			lessonID := strings.TrimPrefix(operation, "complete ")
			// TODO: Call CompleteLesson and print the result
			_ = lessonID
			fmt.Println("") // Replace with actual result
		}
	}
}

All lessons in Object Oriented Programming