Create a gist now

Instantly share code, notes, and snippets.

[ Posted by Margaret ] Short Trip
<?php
class Vehicle{
var $Transportation_number;
var $Seat;
var $Baggage;
public function get_Transportation_number()
{
return ($this->Transportation_number);
}
public function get_Seat()
{
return ($this->Seat);
}
public function set_Transportation_number($value)
{
$this->Transportation_number= $value;
}
public function set_Seat($value)
{
$this->Seat= $value;
}
}
class Plane extends Vehicle{
var $Gate;
var $Baggage;
public function set_Gate($value)
{
$this->Gate = $value;
}
public function get_Gate()
{
return ($this->Gate);
}
public function set_Baggage($value)
{
$this->Baggage = $value;
}
public function get_Baggage()
{
if (!empty( $this->Baggage )){
return ( " Baggage drop at ticket counter" . $this->Baggage);
}else{
return "";
}
}
}
class Train extends Vehicle{
public function message ($object){
}
}
class Bus extends Vehicle{
}
class Trip{
var $departure;
var $arrival;
var $transportation;
var $vehicle;
var $isLastTrip ;
public function set_departure($name)
{
$this->departure = $name;
}
public function get_departure()
{
return ($this->departure);
}
public function set_arrival($name)
{
$this->arrival = $name;
}
public function get_arrival()
{
return ($this->arrival);
}
public function set_transportation($name)
{
$this->transportation = $name;
}
public function get_transportation()
{
return ($this->transportation);
}
public function set_vehicle(Vehicle $vehicle)
{
$this->vehicle = $vehicle;
}
public function get_vehicle()
{
return ($this->vehicle);
}
public function set_lastTrip($value)
{
echo 'ultimo ';
$this->isLastTrip = $value;
}
public function printTicket(){
switch ($this->transportation) {
case 'Plane':
echo "From ".$this->departure." take flight ".$this->vehicle->Transportation_number." to ". $this->arrival. ". Gate ". $this->vehicle->Gate. ", seat ". $this->vehicle->get_Seat().$this->vehicle->get_Baggage();
echo"<br>";
break;
case 'Train':
echo "Take train ".$this->vehicle->Transportation_number." from ".$this->departure." to ". $this->arrival. ". Sit in seat ". $this->vehicle->Seat. ".";
echo"<br>";
break;
case 'Bus':
echo "Take the airport bus from ".$this->departure." to ". $this->arrival;
echo"<br>";
break;
default:
# code...
break;
}
if ($this->isLastTrip == 'true'){
echo "You have arrived at your final destination";
}
}
}
function setTickets(array $tripCollection){
$collectionTrip = array ();
foreach ($tripCollection as $valor){
$trip = new Trip ();
$trip->set_departure($valor['Departure']);
$trip->set_arrival($valor['Arrival']);
$trip->set_transportation($valor['Transportation']);
switch ( $trip->transportation) {
case 'Plane':
$plane = new Plane();
$plane->Transportation_number = $valor['Transportation_number'];
$plane->set_Seat($valor['Seat']);
$plane->set_Gate ($valor['Gate']);
if (!empty($valor['Baggage'])){
$plane->set_Baggage ($valor['Baggage']);
}
$trip->set_vehicle($plane);
break;
case 'Train':
$train =new Train ();
$train->Transportation_number = $valor['Transportation_number'];
$train->Seat = $valor['Seat'];
$trip->set_vehicle($train);
break;
case 'Bus':
$bus = new Bus();
$bus->Transportation_number = $valor['Transportation_number'];
$bus->Seat = $valor['Seat'];
$trip->set_vehicle($bus);
break;
}
//$trip->showTrip();
//$collectionTrip[] = $trip;
array_push($collectionTrip, $trip);
}
//Seleccione el primero elemento del array e indico que es mi origen
for ($i = 0, $max = count($collectionTrip); $i < $max; $i++) {
$hasPreviousTrip = false;
$isLastTrip = true;
foreach ($collectionTrip as $index => $trip) {
if ($collectionTrip[$i]->get_departure() == $trip->get_arrival()) {
$hasPreviousTrip = true;
}
elseif ($collectionTrip[$i]->get_arrival()== $trip->get_departure()) {
$isLastTrip = false;
}
}
if (!$hasPreviousTrip) {
array_unshift($collectionTrip, $collectionTrip[$i]);
unset($collectionTrip[$i]);
}
elseif ($isLastTrip) {
array_push($collectionTrip, $collectionTrip[$i]);
unset($collectionTrip[$i]);
}
}
$ultimo = count($collectionTrip);
$collectionTrip[$ultimo]->isLastTrip = 'true';
foreach ($collectionTrip as $trip){
$trip->printTicket();
}
//print_r($collectionTrip);
}
$objTicketCollection = json_decode('[
{
"Departure": "Stockholm",
"Arrival": "New York",
"Transportation": "Plane",
"Transportation_number": "SK22",
"Seat": "7B",
"Gate": "22"
},
{
"Departure": "Madrid",
"Arrival": "Barcelona",
"Transportation": "Train",
"Transportation_number": "78A",
"Seat": "45B"
},
{
"Departure": "Gerona Airport",
"Arrival": "Stockholm",
"Transportation": "Plane",
"Transportation_number": "SK455",
"Seat": "3A",
"Gate": "45B",
"Baggage": "334"
},
{
"Departure": "Barcelona",
"Arrival": "Gerona Airport",
"Transportation_number": "RR1",
"Transportation": "Bus",
"Seat": "3A"
}
]', true);
setTickets($objTicketCollection);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment