Skip to content

Commit 19384a5

Browse files
committed
added the Car oOP Challenge .js
1 parent 6fc8159 commit 19384a5

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

carOOP-challenge.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Vehicle {
2+
constructor (a,b,c) {
3+
this.make = a;
4+
this.model = b;
5+
this.year = c;
6+
}
7+
honk() {
8+
console.log('beep');
9+
}
10+
toString() {
11+
console.log(`This bad boy is a ${this.year} ${this.make} ${this.model}`);
12+
}
13+
}
14+
15+
class Car extends Vehicle {
16+
constructor(a,b,c) {
17+
super(a,b,c)
18+
this.numWheels = 4;
19+
}
20+
}
21+
22+
class Motorcycle extends Vehicle {
23+
constructor(a,b,c) {
24+
super(a,b,c);
25+
this.numWheels = 2;
26+
}
27+
revEngine() {
28+
console.log("VROOM!!!")
29+
}
30+
}
31+
32+
class Garage {
33+
constructor(a) {
34+
this.vehicles = [];
35+
this.capacity = a;
36+
}
37+
add(a) {
38+
if (!(a instanceof Vehicle)) {
39+
throw new Error ('Only vehicles are allowed in here!');
40+
}
41+
if (this.vehicles.length >= this.capacity) {
42+
throw new Error ('Sorry, the garage is full.');
43+
}
44+
this.vehicles.push(a);
45+
console.log('Vehicle added to the garage!');
46+
}
47+
}

0 commit comments

Comments
 (0)