Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php abstract class Car { abstract protected function calcCost($weight, $distance); } class Truck extends Car{ public function calcCost($weight, $distance){ return ($weight * 4 + $distance * 18); } } class TrailerTruck extends Car{ public function calcCost($weight, $distance){ $weightCost = ($weight <= 500) ? 5 : 3; $trackCost = ($distance <= 500) ? 20 : 15; return ($weight * $weightCost + $distance * $trackCost); } } class Cargo extends Car{ public function calcCost($weight, $distance){ $weightCost = ($weight <= 1000) ? 3 : 1; $trackCost = ($distance <= 2000) ? 50 : 10; return ($weight * $weightCost + $distance * $trackCost); } } class SeaContainer extends Car{ public function calcCost($weight, $distance){ return 80000; } } class Calculator{ public function calcCost($weight, $distance){ $result = []; switch([$weight, $distance]) { case ($weight <= 3000 and $distance <= 500): $car = new Truck; $result['options']['car'] = $car->calcCost($weight, $distance); $container = new SeaContainer; $result['options']['container'] = $container->calcCost($weight, $distance); $trailerTruck = new TrailerTruck; $result['options']['trailerTruck'] = $trailerTruck->calcCost($weight, $distance); $cargo = new Cargo; $result['options']['cargo'] = $cargo->calcCost($weight, $distance); break; case ($weight <= 9000 and $distance <= 5000): $trailerTruck = new TrailerTruck; $result['options']['trailerTruck'] = $trailerTruck->calcCost($weight, $distance); $cargo = new Cargo; $result['options']['cargo'] = $cargo->calcCost($weight, $distance); $container = new SeaContainer; $result['options']['container'] = $container->calcCost($weight, $distance); break; case ($weight <= 20000 and $weight >= 9000 and $distance <= 3000): $trailerTruck = new TrailerTruck; $result['options']['trailerTruck'] = $trailerTruck->calcCost($weight, $distance); $container = new SeaContainer; $result['options']['container'] = $container->calcCost($weight, $distance); break; case ($weight <= 25000 and $weight >= 20000 and $distance >= 5000): $container = new SeaContainer; $result['options']['container'] = $container->calcCost($weight, $distance); break; } $minCost = array_search(min($result['options']), $result['options']); $result['minCost'] = [ 'vehicleTypeName' => $minCost, 'cost' => $result['options'][$minCost] ]; uasort($result['options'], function($a, $b){ if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; }); return $result; } } $calc = new Calculator; $a = $calc->calcCost(3000, 500); var_dump($a);
Copy Clear