我必须通过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 :(得分: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++;
}
}
但是如果你添加错误,那么正确快速的回答会更容易