2016-10-20 4 views
0

PHPでテストに合格する プログラムを作成しましたが、実際の結果が返されず、エラーを修正できませんでした。PHP関数のソートエラー

私は搭乗カードの配列を持っていると私は配列を取得するために都市 のためにそれを並べ替える必要があります。搭乗券の

各エンド市は、次の要素

のスタート都市であります
<?php 


class Application 
{ 
    /** @var int */ 
    public $nbCards; 
    /** @var BoardingCard[] */ 
    protected $boardingCards; 
    /** @var City[] */ 
    public $cities; 
    /** @var City */ 
    private $startPoint; 
    /** @var City */ 
    private $endPoint; 

    public function getCitiesAsString() 
    { 
     $names = []; 
     foreach (self::getCities() as $index => $city) { 
      $names[] = $index.'-'.$city->getName(); 
     } 

     return implode(" - ", $names); 
    } 

    /** 
    * Application constructor. 
    */ 
    public function __construct() 
    { 
     $this->boardingCards = []; 
     $this->cities = []; 
    } 

    /** 
    * @return BoardingCard[] 
    */ 
    public function getBoardingCards() 
    { 
     return $this->boardingCards; 
    } 

    /** 
    * @param BoardingCard[] $boardingCards 
    */ 
    public function setBoardingCards($boardingCards) 
    { 
     $this->boardingCards = $boardingCards; 
    } 

    /** 
    * @return City[] 
    */ 
    public function getCities() 
    { 
     return $this->cities; 
    } 

    public function addCity($city) 
    { 
     $this->cities[] = $city; 
    } 

    public function addBoardingCards($boardingCard) 
    { 
     $this->boardingCards[] = $boardingCard; 
    } 

    /** 
    * initiate the application 
    */ 
    public function run() 
    { 
     echo "***********Welcome******* \n"; 
     do { 
      echo "how many cities do you want to add? \n"; 
      $handle = fopen("php://stdin", "r"); 
      $nbCities = trim(fgets($handle)); 
     } while (empty($nbCities) || $nbCities <= 0); 
     for ($i = 1; $i <= $nbCities; $i++) { 
      $city = new City(); 
      do { 
       echo "Enter the name of the city number $i: \n"; 
       $name = trim(fgets($handle)); 
      } while ($name == ''); 
      $city->setName($name); 
      $this->addCity($city); 
     } 
     $this->nbCards = count($this->getCities()) - 1; 

     for ($i = 0; $i < $this->nbCards; $i++) { 
      $boardingCard = new BoardingCard(); 

      //*********** get boarding card Type *********** 
      do { 
       echo "Enter the boarding card (".($i + 1).") type [".BoardingCard::getTypesAsString()."]: "; 
       $type = strtolower(trim(fgets($handle))); 
      } while (array_search($type, BoardingCard::getTypes()) === false); 
      $boardingCard->setType($type); 
      // *********** get start city *********** 
      do { 
       echo "Enter the boarding card (".($i + 1).") start city from the list: ".$this->getCitiesAsString(); 
       $index = trim(fgets($handle)); 
      } while (!isset($this->cities[$index])); 
      $boardingCard->setStartCity($this->cities[$index]); 
      //*********** get end city *********** 
      do { 
       echo "Enter the boarding card (".($i + 1).") end city from the list: ".$this->getCitiesAsString(); 
       $index = trim(fgets($handle)); 
      } while (!isset($this->cities[$index]) || $this->cities[$index]->getName() == 
       $boardingCard->getStartCity()->getName() 
      ); 
      $boardingCard->setEndCity($this->cities[$index]); 

      // *********** get seat number *********** 
      do { 
       echo "Enter the seat number: "; 
       $seatNumber = trim(fgets($handle)); 
      } while (empty($seatNumber)); 
      $boardingCard->setSeatNb($seatNumber); 

      if ($boardingCard->getType() == BoardingCard::TYPE_TRAIN) { 
       do { 
        echo "Enter the train number: "; 
        $trainNbr = trim(fgets($handle)); 
       } while (empty($trainNbr)); 
       $boardingCard->setTrainNb($trainNbr); 
      } elseif ($boardingCard->getType() == BoardingCard::TYPE_BUS) { 
       do { 
        echo "Enter the bus name: "; 
        $busName = trim(fgets($handle)); 
       } while (empty($busName)); 
       $boardingCard->setBusName($busName); 
      } else { 
       do { 
        echo "Enter the flight number: "; 
        $flightNbr = trim(fgets($handle)); 
       } while (empty($flightNbr)); 
       $boardingCard->setFlightNb($flightNbr); 

       do { 
        echo "Enter the gate number: "; 
        $gateNbr = trim(fgets($handle)); 
       } while (empty($gateNbr)); 
       $boardingCard->setGateNb($gateNbr); 

       do { 
        echo "Enter the counter number: "; 
        $counterNbr = trim(fgets($handle)); 
       } while (empty($counterNbr)); 
       $boardingCard->setCounterNb($counterNbr); 

      } 
      $this->addBoardingCards($boardingCard); 
     } 

     echo " \n"; 

     do { 
      echo "Enter your journey start City from the list: ".self::getCitiesAsString(); 
      $index = trim(fgets($handle)); 
     } while (!isset($this->cities[$index])); 
     $this->setStartPoint($this->cities[$index]); 
     do { 
      echo "Enter your journey end City from the list: ".self::getCitiesAsString(); 
      $index = trim(fgets($handle)); 
     } while (!isset($this->cities[$index]) || $this->cities[$index]->getName() == $this->getStartPoint()->getName(
      )); 
     $this->setEndPoint($this->cities[$index]); 
    } 

    public function showCards() 
    { 
     foreach ($this->boardingCards as $key => $boardingCard) { 
      echo $key."- ".$boardingCard->getDescription()."\n"; 
     } 
    } 

    public function sort() 
    { 
     $i = 0; 
     $j = 0; 
     $tempStartPoint = $this->getStartPoint(); 
     $sorted = false; 

     while ($sorted == false) { 
      echo "temp start point : ".$tempStartPoint->getName(); 
      if (strcmp($tempStartPoint->getName(), $this->boardingCards[$i]->getStartCity()->getName())) { 
       $temp = $this->boardingCards[$j]; 
       $this->boardingCards[$j] = $this->boardingCards[$i]; 
       $this->boardingCards[$i] = $temp; 
       echo "on a permute : ".$this->boardingCards[$j]->getStartCity()->getName()." par : ".$this->boardingCards[$i]->getStartCity()->getName()."\n"; 
       echo "temp start point : ".$tempStartPoint->getName(); 

       $tempStartPoint = $this->boardingCards[$j]->getEndCity(); 
       echo "temp start point : ".$tempStartPoint->getName(); 
       $j++; 
       $i = $j; 
      } 
      else { 
       $i++; 
      } 
      if ($j == ($this->nbCards - 1)) { 
       $sorted = true; 
      } 

     } 


    } 

    /** 
    * @return City 
    */ 
    public function getStartPoint() 
    { 
     return $this->startPoint; 
    } 

    /** 
    * @param City $startPoint 
    */ 
    public function setStartPoint($startPoint) 
    { 
     $this->startPoint = $startPoint; 
    } 

    /** 
    * @return City 
    */ 
    public function getEndPoint() 
    { 
     return $this->endPoint; 
    } 

    /** 
    * @param City $endPoint 
    */ 
    public function setEndPoint($endPoint) 
    { 
     $this->endPoint = $endPoint; 
    } 
} 
+0

どのようなエラーが表示されますか? – ravisachaniya

+0

それは動作しますが、偽の結果を返します 配列をソートしません –

答えて

0

あなたは、配列の無効な要素にアクセスしようとしているので、これで試してくださいエラーが表示されることがある:

while ($sorted == false) { 
    echo "temp start point : ".$tempStartPoint->getName(); 
    if ($j >= ($this->nbCards - 1)) { 
     $sorted = true; 
     break; 
    } 

    if (strcmp($tempStartPoint->getName(), $this->boardingCards[$i]->getStartCity()->getName())) { 
     $temp = $this->boardingCards[$j]; 
     $this->boardingCards[$j] = $this->boardingCards[$i]; 
     $this->boardingCards[$i] = $temp; 
     echo "on a permute : ".$this->boardingCards[$j]->getStartCity()->getName()." par : ".$this->boardingCards[$i]->getStartCity()->getName()."\n"; 
     echo "temp start point : ".$tempStartPoint->getName(); 

     $tempStartPoint = $this->boardingCards[$j]->getEndCity(); 
     echo "temp start point : ".$tempStartPoint->getName(); 
     $j++; 
     $i = $j; 
    } 
    else { 
     $i++; 
    } 


} 

しかし、エラーを追加すると、正確で迅速な回答が得やすくなります