-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHonda.php
More file actions
86 lines (76 loc) · 2.42 KB
/
Honda.php
File metadata and controls
86 lines (76 loc) · 2.42 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace ClassyTransport;
use Exception;
require_once __DIR__.'/Vehicle.php';
class Honda extends Vehicle{
private $color;
private $colors = array('Black','Silver','Blue','White','Red');
public function __construct(
string $color,
int $width,
int $height,
int $length,
int $horsepower,
int $numOfCylinders,
string $fuelType
) {
parent::__construct(
$make = 'Honda',
$model = 'Civic',
$width,
$height,
$length,
$horsepower,
$numOfCylinders,
$fuelType,
$driveType = 'Front Wheel',
$doors = 'standard',
$windows = 'standard',
$tires = 'standard'
);
$this->color = $color;
try {
if(!$this->checkColor($this->color)) {//checks if color is okay
throw new Exception('Not a valid color. Choose Black, Silver, Blue, White, or Red');
}
}
catch(Exception $e) {
echo $e->getMessage(); //catch exception and display message
}
}
//to string method to display properties of Honda Class
public function __toString():string {
$str = "Make: " . $this->getMake() . "<br>" .
"Model: " . $this->getModel() . "<br>" .
"Width: " . $this->getWidth() . "in<br>" .
"Height: " . $this->getHeight() . "in<br>" .
"Length: " . $this->getLength() . "in<br>";
$str .= "Engine: " . "<br>";
$keys = array_keys($this->engine);
foreach($this->getEngine() as $key => $value) {
$str .= $key . ": " . $value . "<br>";
}
foreach($this->getDriveType() as $key => $value) {
if($value) {
$str .= "Drive Type: " . $key . "<br>";
}
}
$str .= "Doors: " . $this->getDoors() . "<br>" .
"Windows: " . $this->getWindows() . "<br>" .
"Tires: " . $this->getTires();
return $str;
}
public function checkColor($color):bool {
foreach($this->colors as $value) {
if ($value == $color) {
return true;
}
}
$this->color = ''; //if color is not valid reset to no color
return false;
}
public function getColor(){
return $this->color;
}
}
?>