|
| 1 | +class User { |
| 2 | + constructor(id, username, password) { |
| 3 | + this.id = id; |
| 4 | + this.username = username; |
| 5 | + this.password = password; |
| 6 | + this.enrolledCourses = []; |
| 7 | + this.courseProgress = {}; |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +class Course { |
| 12 | + constructor(id, title, description) { |
| 13 | + this.id = id; |
| 14 | + this.title = title; |
| 15 | + this.description = description; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class LearningPlatform { |
| 20 | + constructor() { |
| 21 | + this.users = {}; |
| 22 | + this.courses = []; |
| 23 | + } |
| 24 | + |
| 25 | + createUser(username, password) { |
| 26 | + const id = Object.keys(this.users).length + 1; |
| 27 | + const user = new User(id, username, password); |
| 28 | + this.users[id] = user; |
| 29 | + return user; |
| 30 | + } |
| 31 | + |
| 32 | + getUserById(id) { |
| 33 | + return this.users[id]; |
| 34 | + } |
| 35 | + |
| 36 | + createCourse(title, description) { |
| 37 | + const id = this.courses.length + 1; |
| 38 | + const course = new Course(id, title, description); |
| 39 | + this.courses.push(course); |
| 40 | + return course; |
| 41 | + } |
| 42 | + |
| 43 | + enrollUserInCourse(userId, courseId) { |
| 44 | + const user = this.getUserById(userId); |
| 45 | + if (!user || user.enrolledCourses.includes(courseId)) { |
| 46 | + return false; // User not found or already enrolled |
| 47 | + } |
| 48 | + user.enrolledCourses.push(courseId); |
| 49 | + user.courseProgress[courseId] = 0; // Initialize progress to 0% |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + updateCourseProgress(userId, courseId, progress) { |
| 54 | + const user = this.getUserById(userId); |
| 55 | + if (!user || !user.enrolledCourses.includes(courseId)) { |
| 56 | + return false; // User not found or not enrolled in the course |
| 57 | + } |
| 58 | + user.courseProgress[courseId] = progress; |
| 59 | + return true; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Example usage |
| 64 | +const platform = new LearningPlatform(); |
| 65 | + |
| 66 | +const user1 = platform.createUser("user123", "password123"); |
| 67 | +const user2 = platform.createUser("user456", "password456"); |
| 68 | + |
| 69 | +const course1 = platform.createCourse("JavaScript Fundamentals", "Learn the basics of JavaScript."); |
| 70 | +const course2 = platform.createCourse("Web Development 101", "Explore web technologies and development."); |
| 71 | + |
| 72 | +platform.enrollUserInCourse(user1.id, course1.id); |
| 73 | +platform.enrollUserInCourse(user1.id, course2.id); |
| 74 | + |
| 75 | +platform.updateCourseProgress(user1.id, course1.id, 25); |
| 76 | +platform.updateCourseProgress(user1.id, course2.id, 50); |
| 77 | + |
| 78 | +console.log("User:", user1); |
| 79 | +console.log("Course:", course1); |
0 commit comments