forked from su-ntu-ctp/6m-software-1.4-scrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq1-smart-home.js
52 lines (44 loc) · 1.21 KB
/
q1-smart-home.js
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
/*
Task
- Create a "BaseSignal" class to be inherited by "TvSignal", "AirconSignal" and "DoorSignal" class.
- In the "BaseSignal" class, throw an error within the constructor to block direct instantiation
(see doc-permissions.js for example).
- Implement `send` method to print `Sending ${type} signal` in the BaseSignal class.
- In the respective child classes, call `super()` with it's type.
*/
// Task: Add code here
class BaseSignal {
constructor(type) {
if (this.constructor.name === "BaseSignal") {
throw new Error("This class cannot be instantiated");
}
this.type = type;
}
send() {
console.log(`Sending ${this.type} signal`);
}
}
class TvSignal extends BaseSignal {
constructor() {
// Add code here
super("Tv");
}
}
class AirconSignal extends BaseSignal {
constructor() {
// Add code here
super("Aircon");
}
}
class DoorSignal extends BaseSignal {
constructor() {
// Add code here
super("Door");
}
}
const tv = new TvSignal();
tv.send(); // prints "Sending tv signal"
const door = new DoorSignal();
door.send(); // prints "Sending door signal"
const aircon = new AirconSignal();
aircon.send(); // prints "Sending aircon signal"