public static function withPriceandMaxspeed($model, $isSportsCar, $color, $price, $maxSpeed) { $car = new Car(); $car->model = $model; $car->isSportscar = $isSportsCar; $car->color = $color; $car->setPrice($price); $car->setMaxspeed($maxSpeed); return $car; }
<?php require_once __DIR__ . "/../vendor/autoload.php"; require_once __DIR__ . "/../src/car.php"; $app = new Silex\Application(); $app->get("/", function () { return "\n <!DOCTYPE html>\n <html>\n <head>\n <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css'>\n <title>Search for a car!</title>\n </head>\n\n <body>\n <div class='container'>\n <h1>Car Search</h1>\n <p>Enter the price and mileage in a car that you are looking for.</p>\n <form action='/search_results'>\n <div class='form-group'>\n <label for='user_price'>Enter a price:</label>\n <input id='user_price' name='user_price' class='form-control' type='number'>\n </div>\n <div class='form-group'>\n <label for='user_miles'>Enter a mileage:</label>\n <input id='user_miles' name='user_miles' class='form-control' type='number'>\n </div>\n <button type='submit' class='btn-success'>Search</button>\n </form>\n </body>\n </html>\n "; }); $app->get("/search_results", function () { $porsche = new Car("2014 Porsche 911", 114991, 7864, "/img/porsche.jpg"); $ford = new Car("2011 Ford F450", 55995, 14241, "/img/ford.jpg"); $lexus = new Car("2013 Lexus RX 350", 44700, 20000, "/img/lexus.jpg"); $mercedes = new Car("Mercedes Benz CLS550", 39900, 37979, "/img/cls550.jpg"); $mercedes->setPrice("35000.125"); $porsche->setPrice("hotdog"); $user_price = $_GET["user_price"]; $user_miles = $_GET["user_miles"]; $cars = array($porsche, $ford, $lexus, $mercedes); $arrayOfCars = searchCars($cars, $user_price, $user_miles); $whatToReturn = ""; if (empty($arrayOfCars)) { $whatToReturn = '<p>No cars match your search</p>'; } else { foreach ($arrayOfCars as $specific_car) { $car_price = $specific_car->getPrice(); $car_make = $specific_car->getMake(); $car_miles = $specific_car->getMiles(); $car_picture = $specific_car->getPicture(); $whatToReturn .= "<div><img src='{$car_picture}'</div>\n <p>{$car_make}</p>\n <p>{$car_miles} miles</p>\n <p>\${$car_price}</p>\n "; } }
$porsche->setPrice(112344); $porsche->setMiles(1245); $porsche->setImage("images/porsche.jpg"); $ford = new Car(); $ford->setMake_Model("2014 Ford f450"); $ford->setPrice(56892); $ford->setMiles(12465); $ford->setImage("images/ford.jpg"); $lexus = new Car(); $lexus->setMake_Model("2013 Lexus RX 350"); $lexus->setPrice(44700); $lexus->setMiles(20000); $lexus->setImage("images/lexus.jpg"); $mercedes = new Car(); $mercedes->setMake_Model("Mercedes Benz CLS550"); $mercedes->setPrice(39900); $mercedes->setMiles(37979); $mercedes->setImage("images/mercedes.jpg"); $cars = array($porsche, $ford, $lexus, $mercedes); $cars_matching_search = array(); foreach ($cars as $car) { if ($car->worthBuying($_GET["price"]) && $car->maxMileage($_GET["mileage"])) { array_push($cars_matching_search, $car); } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8">
class Car { public $brand; public $model; public $year; public $driver; private $price; public function showBrand() { echo $this->brand; } public function showModel() { echo $this->model; } public function setPrice($price) { $this->price = round($price, 2); } public function getPrice($bool) { if ($bool === true) { return number_format($this->price, 2, '.', ','); } else { return $this->price; } } } $bmw = new Car(); $bmw->setPrice(25700.358); echo $bmw->getPrice(true);
<?php require_once __DIR__ . "/../vendor/autoload.php"; require_once __DIR__ . "/../src/Car.php"; $app = new Silex\Application(); $app->get("/", function () { return "\n <!DOCTYPE html>\n <html>\n <head>\n <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css' integrity='sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7' crossorigin='anonymous'>\n <title>Find a Car</title>\n </head>\n <body>\n <div class='container'>\n <h1>Find a Car!</h1>\n <form action='/view_cars'>\n <div class='form-group'>\n <label for='price'>Enter Maximum Price:</label>\n <input id='price' name='price' class='form-control' type='number'>\n </div>\n <div class='form-group'>\n <label for='mileage'>Enter Maximum Mileage:</label>\n <input id='mileage' name='mileage' class='form-control' type='number'>\n </div>\n <button type='submit' class='btn-success'>Submit</button>\n </form>\n </div>\n </body>\n </html>\n "; }); $app->get("/view_cars", function () { $honda = new Car("1999 Honda CRV", 90000, 6000, "img/honda.jpg"); $tesla = new Car("2014 Tesla Model S", 5000, 35000, "img/tesla.jpg"); $nissan = new Car("2013 Nissan Leaf", 8000, 20000, "img/leaf.jpg"); $toyota = new Car("2009 Toyota Prius", 20000, 15000, "img/toyota.jpg"); $toyota->setPrice("\$12000"); $cars = array($honda, $tesla, $nissan, $toyota); $cars_matching_search = array(); foreach ($cars as $car) { if ($car->worthBuying($_GET["price"], $_GET["mileage"])) { array_push($cars_matching_search, $car); } } $output = ""; if (empty($cars_matching_search)) { echo "your search parameters did not return any results"; } foreach ($cars_matching_search as $car) { $output = $output . "<div class='row'>\n <div class='col-md-6'>\n <p>Price: " . $car->getPrice() . "</p>\n <p>Model: " . $car->getModel() . "</p>\n <p>Miles: " . $car->getMiles() . "</p>\n </div>\n <div class='col-md-6'>\n <img src=" . $car->getPhoto() . ">\n </div>\n "; } return $output; }); return $app;
//Gear Up echo "<p> GearUp:" . $bmw->changeGearUp() . "</p>"; //Gear Down echo "<p> GearDown:" . $bmw->changeGearDown() . "</p>"; //Change Color $bmw->changeColor("green"); echo "<p> Changed color :" . $bmw->getColor() . "</p>"; //Set owner $bmw->setOwner("Harry Potter"); echo "<p> And the happy owner of the BMW is : " . $bmw->getOwner() . " </p>"; //Set owner's age $bmw->setOwnerage(19); echo "<p> The owner's age is: " . $bmw->getOwnerage() . " </p>"; $priceForscrap = $bmw->calculateCarPriceForScrap(200); echo "<p> The price for scrap is : " . $priceForscrap . " </p>"; $bmw->setPrice(70000); echo "<hr>"; //----------------------------Harry Potter ---------------------- //Initialize Harry Potter Obejct $harryPotter = new Person("Harry Potter", "1234", true, 90000); //Label echo "<h3>" . $harryPotter->getName() . "</h3>"; // Can buy car ? echo "<p>" . $harryPotter->getName() . " can buy the car?", PHP_EOL; $bmw2 = clone $bmw; //Buy car //$harryPotter->buyCar($bmw); //Show owned car : echo "<p>Person's money after the transaction: " . $harryPotter->buyCar($bmw2) . "</p>"; var_dump($bmw2); echo "buy car:";
function setImage($new_image) { $this->image = $new_image; } function getImage() { return $this->image; } } $first_car = new Car("Honda CRV", 12000, 150000, "img/jetta.jpg"); $second_car = new Car("Tesla S", 53000, 20000, "img/tesla.jpg"); $third_car = new Car("Deron's Highschool Volvo", 5000, 170000, "img/volvo.jpg"); $fourth_car = new Car("Jeep Cherokee", null, 68000, "img/cherokee.jpg"); $first_car->setMake("VW Jetta"); $second_car->setMiles(35000); $third_car->setPrice(5800); $cars = array($first_car, $second_car, $third_car, $fourth_car); $cars_matching_search = array(); foreach ($cars as $car) { $car_price = $car->getPrice(); $car_miles = $car->getMiles(); if (empty($_GET["price"])) { if ($car_miles < $_GET["mileage"]) { array_push($cars_matching_search, $car); } } elseif (empty($_GET["mileage"])) { if ($car_price < $_GET["price"]) { array_push($cars_matching_search, $car); } } elseif ($car_price < $_GET["price"] && $car_miles < $_GET["mileage"]) { array_push($cars_matching_search, $car);
{ $image_change = (string) $new_image; $this->image = $image_change; } function getImage() { return $this->image; } } $porsche = new Car("2014 Porsche 911", "images/911.jpeg", 7864, 114991); $ford = new Car("2011 Ford F450", "images/ford.jpg", 14241); $lexus = new Car("2013 Lexus RX 350", "images/lexus.jpg", 20000); $mercedes = new Car("Mercedes Benz CLS550", "images/mercedes.jpg", 37979, 39900); $porsche->setName("2013 Porsche 911"); $porsche->setMileage(55555); $lexus->setPrice(0); $porsche->setName("2012 Porsche 911"); $mercedes->setName("2009 Mercedes Benz CLS550"); $ford->setImage("images/pinto.jpeg"); $cars = array($porsche, $ford, $lexus, $mercedes); $cars_matching_search = array(); foreach ($cars as $car) { if ($car->getPrice() <= $_GET["price"] && $car->getMiles() <= $_GET["mileage"]) { array_push($cars_matching_search, $car); } } ?> <!DOCTYPE html> <html> <head>
return true; } } } $porsche = new Car("2014 Porshe 911", 110000, 7864); $subaru = new Car("2002 Subaru Outback", 3800, 128000); $toyota = new Car("1994 Toyota Tacoma", 5000, 195000); $ford = new Car("1997 Ford Wrangler", 1000, 300000); $porsche->setMakeModel("2014 Porsche 911"); $subaru->setMakeModel("2002 Subaru Forester"); $toyota->setMakeModel("1994 Toyota Tacoma"); $ford->setMakeModel("1997 Ford Wrangler"); $porsche->setPrice(110000.07); $subaru->setPrice(3400.34); $toyota->setPrice(5000.99); $ford->setPrice(1001.19); $cars = array($porsche, $subaru, $toyota, $ford); $cars_matching_array = array(); foreach ($cars as $car) { if ($car->worthBuying($_GET["price"])) { array_push($cars_matching_array, $car); } } //for each car in our cars array, we will run the "worthBuying" function on it. //if the function returns true, then it will push that car into the cars_matching_array. ?> <!DOCTYPE html> <html> <head> <title>Your Car Dealership's Homepage</title>