Created
March 6, 2026 02:51
-
-
Save keinermendoza/7ca2461c5aa8395008382bcc9ae4f7d9 to your computer and use it in GitHub Desktop.
Script PHP para CLI. Pede para inserir 3 músicas com o número de reproduções. Mostra a lista de músicas na ordem em que foram inseridas, ordenada por menos reproduzidas e por mais reproduzidas.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Song { | |
| protected string $name; | |
| protected int $reproductions; | |
| public function getName(): string { | |
| return $this->name; | |
| } | |
| public function getReproductions(): int { | |
| return $this->reproductions; | |
| } | |
| public function setName(string $name) { | |
| $name_lenght = mb_strlen($name); | |
| if($name_lenght > 30) { | |
| throw new Exception("Nomes das Músicas podem ter até 30 caractéres. nome atual possue $name_lenght caractéres"); | |
| } | |
| $this->name = $name; | |
| } | |
| public function setReproductions($reproductions) { | |
| if (!is_numeric($reproductions)) { | |
| throw new Exception("As reproduções debem ser números. Você digitou '$reproductions'"); | |
| } | |
| if($reproductions < 0) { | |
| throw new Exception("Não é possivel que uma música tenha reporduções negativas. Você digitou '$reproductions'"); | |
| } | |
| $this->reproductions = $reproductions; | |
| } | |
| } | |
| class SongList { | |
| protected array $songs = []; | |
| public function addSong(Song $song): void { | |
| $this->songs[] = $song; | |
| } | |
| public function getCount(): int { | |
| return count($this->songs); | |
| } | |
| public function __clone() { | |
| foreach($this->songs as $key => $song) { | |
| $this->songs[$key] = clone $song; | |
| } | |
| } | |
| public function __toString(): string { | |
| $final_text = ""; | |
| foreach($this->songs as $song) { | |
| $final_text .= "A música " . $song->getName() . " tem " . $song->getReproductions() . " reproduções \n"; | |
| } | |
| $final_text .= "\n"; | |
| return $final_text; | |
| } | |
| static public function generateSortedSongList(SongList $songList) { | |
| $newSongList = clone $songList; | |
| usort($newSongList->songs, fn($song1, $song2) => $song1->getReproductions() <=> $song2->getReproductions()); | |
| return $newSongList; | |
| } | |
| static public function generateReversedSongList(SongList $songList) { | |
| $newSongList = clone $songList; | |
| usort($newSongList->songs, fn($song2, $song1) => $song1->getReproductions() <=> $song2->getReproductions()); | |
| return $newSongList; | |
| } | |
| } | |
| // main program | |
| const MAX_SONGS = 3; | |
| $songList = new SongList(); | |
| while($songList->getCount() < MAX_SONGS) { | |
| echo "Música ". $songList->getCount() + 1 . "/" . MAX_SONGS . "\n"; | |
| $song = new Song(); | |
| try { | |
| $song->setName(readline("Digite o nome da música: ")); | |
| $song->setReproductions(readline("Digite o número de reproduções: ")); | |
| } catch(Exception $e) { | |
| echo $e->getMessage(); | |
| echo "\n\n"; | |
| continue; | |
| } | |
| $songList->addSong($song); | |
| } | |
| $sortedSongList = SongList::generateSortedSongList($songList); | |
| $reversedSongList = SongList::generateReversedSongList($songList); | |
| echo "\nLista de músicas original\n"; | |
| echo $songList; | |
| echo "Lista de músicas da menos para a mais reproduzida\n"; | |
| echo $sortedSongList; | |
| echo "Lista de músicas da mais para a menos reproduzida\n"; | |
| echo $reversedSongList; | |
| // end main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment