<?php declare(strict_types=1); class Game { public function __construct( public readonly string $name ) {} } class User { public function __construct( public readonly string $name ) {} } class GameCollection { private array $games = []; public function add(Game $game): void { $this->games[$game->name] = $game; } public function remove(Game $game): void { unset($this->games[$game->name]); } public function getAll(): array { return $this->games; } } class GameStore { # array<User->name, GameCollection> private array $loans; public function __construct( private GameCollection $games ) {} public function addGame(Game $game): void { $this->games->add($game); } public function getAllGames(): array { return $this->games->getAll(); } public function registerUser(User $user): void { $this->loans[$user->name] = new GameCollection(); } public function checkOut(Game $game, User $user): void { $this->loans[$user->name]->add($game); $this->games->remove($game); } public function checkIn(Game $game, User $user): void { $this->loans[$user->name]->remove($game); $this->games->add($game); } public function getAllOnLoan(): array { return array_map(fn($user) => $user->getAll(), $this->loans); } } $csgo = new Game('CS:GO'); $exp33 = new Game('Expedition 33'); $marioBros = new Game('Super Mario Bros'); $johnny = new User('Johnny'); $daniel = new User('Daniel'); $store = new GameStore(games: new GameCollection()); $store->addGame($csgo); $store->addGame($exp33); $store->addGame($marioBros); $store->registerUser($daniel); $store->checkOut($exp33, $daniel); var_dump( 'Daniel registers and checksOut Expedition 33', 'Store Available Games', $store->getAllGames(), 'On Loan', $store->getAllOnLoan() ); $store->registerUser($johnny); $store->checkIn($exp33, $daniel); $store->checkOut($exp33, $johnny); var_dump( 'Daniel returns Expedition 33 and Johnny checksOut the same game', 'Store Available Games', $store->getAllGames(), 'On Loan', $store->getAllOnLoan() );
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).