@ 2018-03-05T21:36:05Z <?php
error_reporting(-1);
class Room
{
private $number;
private $countOfGuests;
private $price;
private $arrivalDate;
private $dateOfDeparture;
private $guests = array("firstName" => array(), "lastName" => array());
private $isFree = TRUE;
public function __construct(int $number, int $countOfGuests, int $price, DateTime $arrivalDate, DateTime $dateOfDeparture)
{
$this->number = $number;
$this->price = $price;
$this->countOfGuests = $countOfGuests;
$this->arrivalDate = $arrivalDate;
$this->dateOfDeparture = $dateOfDeparture;
}
public function getIsFree(): bool
{
return $this->isFree;
}
public function changeIsFtee():void
{
if ($this->isFree == TRUE){
$this->isFree = FALSE;
} else {
$this->isFree = TRUE;
}
}
public function getArrivalDate(): DateTime
{
return $this->arrivalDate;
}
public function getDateOfDeparture(): DateTime
{
return $this->dateOfDeparture;
}
public function addGuestToRoom($firstName, $lastName): void
{
$this->guests["firstName"][] = $firstName;
$this->guests["lastName"][] = $lastName;
}
public function getPrice(): int
{
return $this->price;
}
public function getCountOfGuests(): int
{
return $this->countOfGuests;
}
public function getGuests(): array
{
return $this->guests;
}
public function getNumber(): int
{
return $this->number;
}
public function setDate(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture): void
{
$this->arrivalDate = $arrivalDate;
$this->dateOfDeparture = $dateOfDeparture;
}
}
class Hotel
{
private $rooms = array();
private $guests = array();
private $receipts = array("date" => array(), "money" => array());
public function addRoomForHotel(int $countOfGuests, int $price): void
{
$this->rooms[] = new Room(count($this->rooms) + 1, $countOfGuests, $price, new DateTime(), new DateTime());
}
public function addGuest(string $firstName, string $lastName): void
{
$this->guests[] = new Guest($firstName, $lastName);
}
public function getRooms(): array
{
return $this->rooms;
}
public function getRoom(int $numberOfRoom): Room
{
return $this->rooms[$numberOfRoom - 1];
}
public function getGuest(string $firstName, string $lastName): Guest
{
foreach ($this->guests as $guest) {
if ($guest->getFirstName() == $firstName && $guest->getLastName() == $lastName){
return $guest;
break;
}
}
if (1){
throw new Exception("Неправильно введены данные клиента");
}
}
public function getReceipts(): array
{
return $this->receipts;
}
public function addDateOfReceipts(DateTime $date): void
{
$this->receipts["date"][] = $date;
}
public function addMoneyOfReceipts(int $money): void
{
$this->receipts["money"][] = $money;
}
public function replenishMoney(int $money, int $numberInArray): void
{
$this->receipts["money"][$numberInArray] += $money;
}
}
class Guest
{
private $firstName;
private $lastName;
private $visitedRooms = array();
public function __construct(string $firstName, string $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function addVisitedRoom(int $numberOfRoom): void
{
$this->visitedRooms[] = $numberOfRoom;
}
public function getFirstName(): string
{
return $this->firstName;
}
public function getLastName(): string
{
return $this->lastName;
}
public function getVisitedRooms(): array
{
return $this->visitedRooms;
}
}
class Registration
{
public function getStatisticOfReceipts(Hotel $hotel, DateTime $startDate, DateTime $endDate): void
{
echo "Дата Сумма\n";
foreach ($hotel->getReceipts()["date"] as $key => $date) {
if($date == $startDate || $date > $startDate){
for($i = $key; $hotel->getReceipts()["date"][$i] != $endDate; $i++){
echo $hotel->getReceipts()["date"][$i]->format('Y-m-d'), " ", $hotel->getReceipts()["money"][$i], "\n";
if($hotel->getReceipts()["date"][$i] == end($hotel->getReceipts()["date"])){
break;
}
}
}
break;
}
}
public function registerGuests(Hotel $hotel, int $numberOfRoom, string $firstName, string $lastName, DateTime $arrivalDate, DateTime $dateOfDeparture): void
{
$hotel->getRoom($numberOfRoom)->setDate($hotel, $arrivalDate, $dateOfDeparture);
$hotel->getRoom($numberOfRoom)->addGuestToRoom($firstName, $lastName);
$hotel->getRoom($numberOfRoom)->changeIsFtee();
$hotel->getGuest($firstName, $lastName)->addVisitedRoom($numberOfRoom);
$this->addReceiptsForHotel($hotel, $numberOfRoom, $arrivalDate);
}
public function selectRoom(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture, int $countOfGuests): void
{
$availableRoom = $this->searchRooms($hotel, $dateOfDeparture, $arrivalDate);
$roomsForList = array();
while ($countOfGuests > 0) {
$maxCountOfGuestInRoom = $availableRoom[0]->getCountOfGuests();
foreach ($availableRoom as $room) {
if ($room->getCountOfGuests() > $maxCountOfGuestInRoom && $room->getCountOfGuests() <= $countOfGuests){
$maxCountOfGuestInRoom = $room->getCountOfGuests();
}
}
$roomsForList[] = $this->SearchSuitableRoom($hotel, $arrivalDate, $dateOfDeparture, $maxCountOfGuestInRoom);
end($roomsForList)->changeIsFtee();
$countOfGuests -= $maxCountOfGuestInRoom;
}
$this->printInformationOfRooms($roomsForList);
foreach ($roomsForList as $room) {
$room->changeIsFtee();
}
}
public function getFreeRoomsInRange(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture):void
{
$this->printInformationOfRooms($this->searchRooms($hotel, $dateOfDeparture, $arrivalDate));
}
public function getFreeRooms(Hotel $hotel, DateTime $date): void
{
$this->printInformationOfRooms($this->searchRooms($hotel, $date, $date));
}
private function searchRooms(Hotel $hotel, DateTime $date1, DateTime $date2)
{
$freeRooms = array();
foreach ($hotel->getRooms() as $room) {
if ($room->getIsFree() && ($room->getArrivalDate() > $date1 || $room->getDateOfDeparture() < $date2)){
$freeRooms[] = $room;
}
}
return $freeRooms;
}
private function SearchSuitableRoom(Hotel $hotel, DateTime $arrivalDate, DateTime $dateOfDeparture, int $countOfGuests): Room
{
$availableRoom = $this->searchRooms($hotel, $dateOfDeparture, $arrivalDate);
foreach ($availableRoom as $number => $room) {
if ($room->getCountOfGuests() < $countOfGuests){
unset($availableRoom[$number]);
}
}
sort($availableRoom);
if (count($availableRoom) == 0){
throw new Exception("Нет подходящих номеров");
}
$minPrice = $availableRoom[0]->getPrice();
foreach ($availableRoom as $number => $room) {
if ($room->getPrice() < $minPrice){
$minPrice = $room->getPrice();
for ($i = 0; $i < $number; $i++){
unset($availableRoom[$i]);
}
} elseif ($room->getPrice() > $minPrice){
unset($availableRoom[$number]);
}
}
sort($availableRoom);
$minGuests = $availableRoom[0]->getCountOfGuests();
foreach ($availableRoom as $number => $room) {
if ($room->getCountOfGuests() < $minGuests){
$minGuests = $room->getCountOfGuests();
for ($i = 0; $i < $number; $i++){
unset($availableRoom[$i]);
}
} elseif ($room->getCountOfGuests() > $minGuests){
unset($availableRoom[$number]);
}
}
sort($availableRoom);
return $availableRoom[0];
}
public function printHistoryOfGuest(Hotel $hotel, string $firstName, string $lastName): void
{
echo "\n$firstName $lastName История\n";
$rooms = $hotel->getGuest($firstName, $lastName)->getVisitedRooms();
foreach ($rooms as &$room) {
$room = $hotel->getRoom($room);
}
$this->printInformationOfRooms($rooms);
}
public function printHistoryOfRoom(Hotel $hotel, int $numberOfRoom): void
{
echo "\nИстория комнаты № $numberOfRoom\n\n";
echo "Имя Фамилия\n";
$guests = $hotel->getRoom($numberOfRoom)->getGuests();
for($i = 0; $i < count($guests["firstName"]); $i++){
echo $this->padLeft($guests["firstName"][$i], 10) . $this->padLeft($guests["lastName"][$i], 10) . "\n";
}
}
private function printInformationOfRooms(array $rooms): void
{
echo "\nНомер вместимость стоимость\n";
foreach($rooms as $room){
$this->printRoomsRow($room->getNumber(), $room->getCountOfGuests(), $room->getPrice());
}
}
private function padLeft($value, int $columnLength): void
{
echo $value;
echo str_repeat(" ", $columnLength - mb_strlen($value));
}
private function printRoomsRow(int $numberOfRoom, int $countOfGuests, int $price): void
{
$col1 = 6;
$col2 = 12;
$col3 = 20;
echo $this->padLeft($numberOfRoom, $col1) . $this->padLeft($countOfGuests, $col2) . $this->padLeft($price, $col3) . "\n";
}
private function addReceiptsForHotel(Hotel $hotel, int $numberOfRoom, DateTime $arrivalDate): void
{
foreach ($hotel->getReceipts()["date"] as $key => $date) {
if ($arrivalDate == $date){
$hotel->replenishMoney($hotel->getRoom($numberOfRoom)->getPrice(), $key);
return;
}
}
$hotel->addDateOfReceipts($arrivalDate);
$hotel->addMoneyOfReceipts($hotel->getRoom($numberOfRoom)->getPrice());
}
}
$hotel = new Hotel;
$hotel->addRoomForHotel(1, 500);
$hotel->addRoomForHotel(3, 700);
$hotel->addRoomForHotel(2, 1500);
$hotel->addRoomForHotel(2, 1500);
$hotel->addRoomForHotel(3, 1500);
$hotel->addGuest("Ivan", "Ivanov");
$hotel->addGuest("Petr", "Petrov");
$registration = new Registration;
$registration->registerGuests($hotel, 3, "Ivan", "Ivanov", new DateTime('2015-05-10'), new DateTime('2015-05-22'));
$registration->registerGuests($hotel, 1, "Petr", "Petrov", new DateTime('2015-05-10'), new DateTime('2015-05-12'));
$registration->registerGuests($hotel, 4, "Ivan", "Ivanov", new DateTime('2015-05-27'), new DateTime('2015-06-03'));
$registration->registerGuests($hotel, 3, "Petr", "Petrov", new DateTime('2015-07-13'), new DateTime('2015-07-15'));
// $registration->getFreeRooms($hotel, new DateTime('2015-05-12'));
// $registration->getFreeRoomsInRange($hotel, new DateTime('2015-05-13'), new DateTime('2015-05-25'));
// $registration->selectRoom($hotel, new DateTime('2015-05-12'), new DateTime('2015-05-25'), 4);
// $registration->printHistoryOfGuest($hotel, "Ivan", "Ivanov");
// $registration->printHistoryOfRoom($hotel, 3);
// $registration->getStatisticOfReceipts($hotel, new DateTime('2015-05-05'), new DateTime('2015-07-25'));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.7 0.012 0.003 17.00 8.3.6 0.011 0.011 18.63 8.3.5 0.011 0.007 16.79 8.3.4 0.011 0.004 19.21 8.3.3 0.011 0.004 19.04 8.3.2 0.005 0.003 22.07 8.3.1 0.004 0.004 23.54 8.3.0 0.004 0.004 22.10 8.2.19 0.006 0.013 18.41 8.2.18 0.013 0.003 16.50 8.2.17 0.010 0.007 22.96 8.2.16 0.012 0.003 22.25 8.2.15 0.000 0.008 24.18 8.2.14 0.000 0.009 24.66 8.2.13 0.003 0.005 21.25 8.2.12 0.005 0.003 26.35 8.2.11 0.006 0.003 22.11 8.2.10 0.006 0.006 20.57 8.2.9 0.004 0.004 19.89 8.2.8 0.004 0.004 18.29 8.2.7 0.003 0.006 18.18 8.2.6 0.003 0.006 18.34 8.2.5 0.003 0.006 18.10 8.2.4 0.005 0.005 21.20 8.2.3 0.004 0.004 18.42 8.2.2 0.005 0.003 18.32 8.2.1 0.004 0.004 18.32 8.2.0 0.000 0.008 18.41 8.1.28 0.014 0.007 25.92 8.1.27 0.000 0.008 23.90 8.1.26 0.004 0.004 26.35 8.1.25 0.008 0.000 28.09 8.1.24 0.003 0.006 23.96 8.1.23 0.009 0.003 19.10 8.1.22 0.000 0.009 17.91 8.1.21 0.008 0.000 19.29 8.1.20 0.006 0.003 17.73 8.1.19 0.004 0.007 17.76 8.1.18 0.003 0.008 18.10 8.1.17 0.004 0.004 20.91 8.1.16 0.003 0.005 19.11 8.1.15 0.003 0.006 22.05 8.1.14 0.003 0.005 17.98 8.1.13 0.004 0.004 19.25 8.1.12 0.005 0.006 17.81 8.1.11 0.003 0.005 17.73 8.1.10 0.004 0.004 17.88 8.1.9 0.005 0.003 17.86 8.1.8 0.004 0.004 17.69 8.1.7 0.005 0.002 17.84 8.1.6 0.003 0.006 17.89 8.1.5 0.004 0.004 17.77 8.1.4 0.000 0.008 17.86 8.1.3 0.000 0.009 18.02 8.1.2 0.004 0.004 17.96 8.1.1 0.008 0.000 17.81 8.1.0 0.003 0.006 17.70 8.0.30 0.000 0.009 21.68 8.0.29 0.004 0.004 17.25 8.0.28 0.000 0.008 18.77 8.0.27 0.004 0.004 17.43 8.0.26 0.000 0.007 17.17 8.0.25 0.003 0.005 17.22 8.0.24 0.002 0.005 17.43 8.0.23 0.000 0.009 17.38 8.0.22 0.000 0.007 17.36 8.0.21 0.007 0.000 17.32 8.0.20 0.004 0.004 17.42 8.0.19 0.004 0.004 17.43 8.0.18 0.000 0.008 17.23 8.0.17 0.000 0.009 17.21 8.0.16 0.004 0.004 17.29 8.0.15 0.000 0.008 17.23 8.0.14 0.004 0.004 17.29 8.0.13 0.006 0.000 13.59 8.0.12 0.004 0.004 17.18 8.0.11 0.005 0.003 17.20 8.0.10 0.000 0.008 17.34 8.0.9 0.008 0.000 17.39 8.0.8 0.013 0.010 17.31 8.0.7 0.004 0.004 17.34 8.0.6 0.003 0.006 17.34 8.0.5 0.003 0.005 17.15 8.0.3 0.008 0.010 17.44 8.0.2 0.012 0.008 17.62 8.0.1 0.000 0.008 17.44 8.0.0 0.012 0.006 17.18 7.4.33 0.000 0.005 15.55 7.4.32 0.000 0.007 16.91 7.4.30 0.005 0.003 16.79 7.4.29 0.000 0.007 16.79 7.4.28 0.000 0.008 16.70 7.4.27 0.004 0.004 16.84 7.4.26 0.000 0.007 16.77 7.4.25 0.005 0.002 16.77 7.4.24 0.000 0.007 16.88 7.4.23 0.007 0.000 16.80 7.4.22 0.008 0.000 16.73 7.4.21 0.013 0.001 16.85 7.4.20 0.007 0.000 16.97 7.4.16 0.008 0.010 16.88 7.4.14 0.010 0.012 17.86 7.4.13 0.007 0.011 16.77 7.4.12 0.014 0.006 16.86 7.4.11 0.003 0.015 16.88 7.4.10 0.003 0.016 16.86 7.4.9 0.004 0.014 16.84 7.4.8 0.017 0.005 19.39 7.4.7 0.010 0.007 16.71 7.4.6 0.013 0.010 16.89 7.4.5 0.006 0.010 16.72 7.4.4 0.007 0.011 16.86 7.4.0 0.008 0.008 15.49 7.3.33 0.006 0.000 13.50 7.3.32 0.000 0.006 13.64 7.3.31 0.004 0.004 16.70 7.3.30 0.004 0.004 16.62 7.3.29 0.007 0.000 16.61 7.3.28 0.009 0.008 16.64 7.3.26 0.012 0.010 16.76 7.3.24 0.009 0.008 16.77 7.3.23 0.012 0.009 17.00 7.3.21 0.014 0.004 16.87 7.3.20 0.010 0.006 16.59 7.3.19 0.007 0.017 16.89 7.3.18 0.015 0.009 17.00 7.3.17 0.008 0.011 16.79 7.3.16 0.020 0.003 16.88 7.3.1 0.003 0.013 16.45 7.3.0 0.003 0.014 16.46 7.2.33 0.011 0.007 17.03 7.2.32 0.017 0.007 17.13 7.2.31 0.010 0.007 16.77 7.2.30 0.007 0.010 16.84 7.2.29 0.015 0.006 16.87 7.2.13 0.003 0.013 17.11 7.2.12 0.004 0.008 17.27 7.2.11 0.010 0.003 17.18 7.2.10 0.000 0.009 17.21 7.2.9 0.003 0.012 16.91 7.2.8 0.007 0.007 17.21 7.2.7 0.000 0.012 17.20 7.2.6 0.003 0.009 17.04 7.2.5 0.006 0.006 17.14 7.2.4 0.008 0.008 16.99 7.2.3 0.014 0.012 18.42 7.2.2 0.011 0.011 18.50 7.2.1 0.019 0.010 18.99 7.2.0 0.011 0.010 19.19 7.1.25 0.003 0.007 15.82 7.1.20 0.011 0.003 15.73 7.1.15 0.016 0.017 17.99 7.1.14 0.015 0.012 17.87 7.1.13 0.010 0.012 18.90 7.1.12 0.010 0.014 18.93 7.1.11 0.015 0.011 18.10 7.1.10 0.009 0.012 18.09 7.1.9 0.012 0.011 18.05 7.1.8 0.009 0.014 18.13 7.1.7 0.008 0.014 17.26 7.1.6 0.028 0.021 35.28 7.1.5 0.025 0.015 34.84 7.1.4 0.026 0.019 34.54 7.1.3 0.032 0.019 34.66 7.1.2 0.029 0.018 34.73 7.1.1 0.009 0.012 16.79 7.1.0 0.008 0.013 16.85
preferences:dark mode live preview
59.74 ms | 401 KiB | 5 Q