Share
Explore

Forward Generating TypeScript from a Business Domain UML Description


megaphone

Write a typescript program to model the business domain of a college with students, classes, and the need to register students into classes.

Provide lab instructions to use typescript npm command (the new tsc package) to compile and run this as a typescript program. show simple uml to assist students to visualize.


TypeScript program modeling a college domain, along with a simple UML diagram and lab instructions using the new TypeScript npm command to compile and run the program.
First, let's start with a simple UML diagram to visualize the domain:
image.png
Now, let's create the TypeScript program:
```typescript // college.ts - name of the file

class Student { constructor(public id: string, public name: string) {}
enroll(course: Class) { console.log(`${this.name} has enrolled in ${course.name}`); } }
class Class { private students: Student[] = [];
constructor(public id: string, public name: string, public capacity: number) {}
addStudent(student: Student) { if (this.students.length < this.capacity) { this.students.push(student); console.log(`${student.name} added to ${this.name}`); } else { console.log(`${this.name} is full. Cannot add ${student.name}`); } } }
class Registration { constructor(public student: Student, public class: Class) {} }
class College { private registrations: Registration[] = [];
constructor(public name: string) {}
registerStudent(student: Student, class_: Class) { const registration = new Registration(student, class_); this.registrations.push(registration); student.enroll(class_); class_.addStudent(student); } }
// Usage : use cases: const college = new College("Tech University"); const student1 = new Student("S001", "Alice"); const student2 = new Student("S002", "Bob");
const class1 = new Class("C001", "Introduction to Programming", 2);
college.registerStudent(student1, class1); college.registerStudent(student2, class1);
// Try to register a third student (should fail due to capacity)
const student3 = new Student("S003", "Charlie"); college.registerStudent(student3, class1); ```

Now, let's provide lab instructions to compile and run this TypeScript program using the new TypeScript npm command:


Lab Instructions: Compiling and Running TypeScript with npm
1. Set up the project: ``` mkdir college-registration cd college-registration npm init -y ```
2. Install TypeScript: ``` npm install typescript --save-dev ```
3. Create a TypeScript configuration file: npx tsc --init ```
4. Open `tsconfig.json` and ensure it has the following options: ```json { "compilerOptions": { "target": "es2016", "module": "commonjs", "outDir": "./dist", "strict": true, "esModuleInterop": true } } ```
5. Create a `src` folder and add the `college.ts` file inside it with the provided TypeScript code.
6. Update `package.json` to include the following scripts: ```json "scripts": { "build": "tsc", "start": "node dist/college.js" } ```
7. Compile the TypeScript code: ``` npm run build
image.png

8. Run the compiled JavaScript:

npm start
image.png

9. You should see output similar to this:

Alice has enrolled in Introduction to Programming Alice added to Introduction to Programming Bob has enrolled in Introduction to Programming Bob added to Introduction to Programming Charlie has enrolled in Introduction to Programming Introduction to Programming is full. Cannot add Charlie ```
10. Experiment with the code: - Try changing the capacity of the class - Add more students or classes - Implement additional methods or properties
11. After making changes, recompile and run the program: ``` npm run build npm start
This lab exercise demonstrates how to model a simple college registration system using TypeScript classes, compile the TypeScript code using the npm-based TypeScript compiler, and run the resulting JavaScript. It covers important object-oriented programming concepts such as classes, inheritance, and composition while providing hands-on experience with TypeScript compilation and execution.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.