Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Create an Animal Choir simulator
- *
- * The task constraints are:
- *
- * There must be 3 different choir member animals
- * (i.e. dogs, cats, mice)
- *
- * Every animal must have a sing method that returns a string representation of a voice
- * (i.e. 'bark', 'meow', 'squeak')
- *
- * Every animal must have a loudness property which can have 3 settings,
- * depending on which the sing method result will be rendered as
- * lowercase, first letter uppercase and uppercase.
- *
- * Singer groups are groups of animals with the same loudness property value.
- * Singer group song is represented with a CSV of the group singer sing result in random order.
- *
- * The choir simulator must have implement the following methods:
- * crescendo - the choir start singing from the least loud singer group, and then are being joined
- * by more and more loud singer groups until they are singing all together.
- * The joining is represented with a new line.
- * Example:
- * meow, squeak, bark
- * Meow, bark, squeak, Bark, meow
- * bark, Meow, MEOW, squeak, BARK, meow, Bark
- *
- * arpeggio - the choir singer groups of the same loudness start singing one by one from
- * the least loud to the loudest
- * Example:
- * meow, squeak, bark
- * Meow, Bark
- * MEOW, BARK
- *
- */
- //TODO: Describe the class hierarchy
- //Choir class
- class Choir{
- public $line_ending = '';
- public $line_separator = '';
- public $voices = '';
- public function crescendo(){
- $crescendo_song = '';
- $animals = new Animal();
- //We define which animal voices will be included in song
- $animals->voices = $this->voices;
- //First we start with the silent
- $animals->loudness = 'silent';
- //Call the function
- $silent = $animals->sing();
- //echo the result
- $crescendo_song .= $this->stringForm($silent);
- //Then we continue with the normal, but also merge with silent
- $animals->loudness = 'normal';
- $normal = array_merge($animals->sing(),$silent);
- $crescendo_song .= $this->stringForm($normal);
- //And in the end we merge loud with silent and normal
- $animals->loudness = 'loud';
- $loud = array_merge($animals->sing(),$silent,$normal);
- $crescendo_song .= $this->stringForm($loud);
- return $crescendo_song;
- }
- public function arpeggio(){
- $arpeggio_song = '';
- $animals = new Animal();
- //We define which animal voices will be included in song
- $animals->voices = $this->voices;
- //First we start with the silent
- $animals->loudness = 'silent';
- $arpeggio_song .= $this->stringForm($animals->sing());
- //Then normal
- $animals->loudness = 'normal';
- $arpeggio_song .= $this->stringForm($animals->sing());
- //And then the loud
- $animals->loudness = 'loud';
- $arpeggio_song .= $this->stringForm($animals->sing());
- return $arpeggio_song;
- }
- private function stringForm($array){
- //Randomize the array
- shuffle($array);
- //Predefine a song and a separator
- $song = '';
- $comma = '';
- //Form song string
- foreach ($array as $slog) {
- $song .= $comma.$slog;
- $comma = $this->line_separator.' ';
- };
- return $song.$this->line_ending;
- }
- }
- //Animal class
- class Animal{
- public $loudness = '';
- public function sing(){
- $song = array();
- foreach ($this->voices as $voices) {
- switch ($this->loudness) {
- case 'silent':
- array_push($song,strtolower($voices));
- break;
- case 'normal':
- array_push($song,ucfirst($voices));
- break;
- case 'loud':
- array_push($song,strtoupper($voices));
- break;
- default:
- array_push($song,$voices);
- break;
- }
- }
- return $song;
- }
- }
- $choir = new Choir();
- $choir->line_ending = PHP_EOL;
- //$choir->line_ending = '<br>'; //For cleared viewing in browser use <br> tag
- //Define the line separator for CSV
- $choir->line_separator = ',';
- //Define the voices of animals
- $choir->voices = array('bark','meow','squeak');
- //Call and echo the functions
- echo $choir->crescendo();
- echo $choir->arpeggio();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement