typescript
Copy
class Person {
protected name: string;
protected age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
public getName(): string {
return this.name;
}
public getAge(): number {
return this.age;
}
}
class Course {
private courseId: string;
private name: string;
private instructor: Person;
constructor(courseId: string, name: string, instructor: Person) {
this.courseId = courseId;
this.name = name;
this.instructor = instructor;
}
public getCourseId(): string {
return this.courseId;
}
public getName(): string {
return this.name;
}
}
class Student extends Person {
private studentId: string;
private courses: Course[];
constructor(name: string, age: number, studentId: string) {
super(name, age);
this.studentId = studentId;
this.courses = [];
}
public getStudentId(): string {
return this.studentId;
}
public enrollCourse(course: Course): void {
this.courses.push(course);
}
}
class Department {
private name: string;
private courses: Course[];
constructor(name: string) {
this.name = name;
this.courses = [];
}
public addCourse(course: Course): void {
this.courses.push(course);
}
public getCourseCount(): number {
return this.courses.length;
}
}
// Usage
const instructor = new Person("Dr. Smith", 45);
const mathCourse = new Course("MATH101", "Introduction to Calculus", instructor);
const csCourse = new Course("CS101", "Programming Fundamentals", instructor);
const student = new Student("Alice Johnson", 20, "S10001");
student.enrollCourse(mathCourse);
student.enrollCourse(csCourse);
const scienceDepartment = new Department("Science");
scienceDepartment.addCourse(mathCourse);
scienceDepartment.addCourse(csCourse);
console.log(student.getName()); // Output: Alice Johnson
console.log(student.getStudentId()); // Output: S10001
console.log(scienceDepartment.getCourseCount()); // Output: 2