This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathinterfaces.ts
52 lines (45 loc) · 1.46 KB
/
interfaces.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
interface Drivable {
// Starts the car's ignition so that it can drive.
start(): void;
// Attempt to drive a distance. Returns true or false based on whether or not the drive was successful.
drive(distance: number): boolean;
// Give the distance from the start.
getPosition(): number;
}
class Car implements Drivable {
private _isRunning: boolean;
private _distanceFromStart: number;
constructor() {
this._isRunning = false;
this._distanceFromStart = 0;
}
/**
* Starts the car's ignition so that it can drive.
*/
public start() {
this._isRunning = true;
}
/**
* Attempt to drive a distance. Returns true or false based on whether or not the drive was successful.
*
* @param {number} distance The distance attempting to cover
*
* @returns {boolean} Whether or not the drive was successful
*/
public drive(distance: number): boolean {
if (this._isRunning) {
this._distanceFromStart += distance;
return true;
}
return false;
}
/**
* Gives the distance from starting position
*
* @returns {number} Distance from starting position;
*/
public getPosition(): number {
return this._distanceFromStart;
}
}
// Want to experiment? Try adding a second interface: Flyable. Implement it in a Helicopter class, then write a FlyingCar class that implements both Drivable and Flyable!