Let's practice TypeScript by creating classes for rocket cargo calculations.
If you have not already done so, follow the instructions given in the TypeScript exercises to fork the GitHub repository.
Use the terminal to check that you are in the master
branch, then navigate
into the studio
folder.
$ git branch
* master
solutions
$ pwd
/typescript-lc101-projects
$ ls
exercises studio
$ cd studio
$ ls
index.ts Payload.ts
From the file tree in VSCode, open the index.ts
file.
Astronaut
, Cargo
, and Rocket
. (Details
below).index.ts
file.In the starter code, you will notice that an interface named Payload
has
been declared. This interface ensures that any class that implements it will
have a massKg
property.
Create three new files---Astronaut.ts
, Cargo.ts
, and Rocket.ts
. To
do this in VSCode, click the "New File" button and enter the file name.
Another option is to run the command touch new_file_name
in the terminal.
Define each class (Astronaut
, Cargo
, Rocket
) in a separate file.
Each class should be exported using export
.
export class Astronaut {
// properties and methods
}
As needed, the classes can be imported using import
.
import { Astronaut } from './Astronaut';
Astronaut.ts
Payload
interfacemassKg
should be a number.name
should be a string.massKg
should be a number.name
should be string.this.massKg
and this.name
.Cargo.ts
Payload
interfacemassKg
should be a number.material
should be a string.massKg
should be a number.material
should be a string.this.massKg
and this.material
Rocket.ts
.name
should be a string.totalCapacityKg
should be a number.cargoItems
should be an array of Cargo
objects.[]
astronauts
should be an array of Astronaut
objects.[]
name
should be a string.totalCapacityKg
should be a number.this.name
and this.totalCapacityKg
sumMass( items: Payload[] ): number
items
using each item's massKg
propertycurrentMassKg(): number
this.sumMass
to return the combined mass of
this.astronauts
and this.cargoItems
canAdd(item: Payload): boolean
true
if this.currentMassKg() + item.massKg <= this.totalCapacityKg
addCargo(cargo: Cargo): boolean
this.canAdd()
to see if another item can be added.true
, adds cargo
to this.cargoItems
and returns
true
.false
, returns false
.addAstronaut(astronaut: Astronaut): boolean
this.canAdd()
to see if another astronaut can be added.true
, adds astronaut
to this.astronauts
and returns true
.false
, returns false
.index.ts
¶Paste the code shown below into index.ts
.
1import { Astronaut } from './Astronaut';
2import { Cargo } from './Cargo';
3import { Rocket } from './Rocket';
4
5let falcon9: Rocket = new Rocket('Falcon 9', 7500);
6
7let astronauts: Astronaut[] = [
8 new Astronaut(75, 'Mae'),
9 new Astronaut(81, 'Sally'),
10 new Astronaut(99, 'Charles')
11];
12
13for (let i = 0; i < astronauts.length; i++) {
14 let astronaut = astronauts[i];
15 let status = '';
16 if (falcon9.addAstronaut(astronaut)) {
17 status = "On board";
18 } else {
19 status = "Not on board";
20 }
21 console.log(`${astronaut.name}: ${status}`);
22}
23
24let cargo: Cargo[] = [
25 new Cargo(3107.39, "Satellite"),
26 new Cargo(1000.39, "Space Probe"),
27 new Cargo(753, "Water"),
28 new Cargo(541, "Food"),
29 new Cargo(2107.39, "Tesla Roadster"),
30];
31
32for (let i = 0; i < cargo.length; i++) {
33 let c = cargo[i];
34 let loaded = '';
35 if (falcon9.addCargo(c)) {
36 loaded = "Loaded"
37 } else {
38 loaded = "Not loaded"
39 }
40 console.log(`${c.material}: ${loaded}`);
41}
42
43console.log(`Final cargo and astronaut mass: ${falcon9.currentMassKg()} kg.`);
index.ts
¶index.ts
file. This will also
compile the modules you imported into the file (Astronaut.ts
,
Rocket.ts
, etc.).node index.js
to run the JavaScript file created during
the build process.$ ls
Astronaut.ts Cargo.ts Payload.ts Rocket.ts index.ts
$ tsc index.ts
$ ls
Astronaut.js Cargo.js Payload.js Rocket.js index.js
Astronaut.ts Cargo.ts Payload.ts Rocket.ts index.ts
$ node index.js
Mae: On board
Sally: On board
Charles: On board
Satellite: Loaded
Space Probe: Loaded
Water: Loaded
Food: Loaded
Tesla Roadster: Not loaded
Final cargo and astronaut mass: 5656.78 kg.