Forward Generating TypeScript from a Business Domain UML Description
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:
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[] = [];
// 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);
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
8. Run the compiled JavaScript:
npm start
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 (