Cyberstonks
Cyberstonks

Reputation: 11

How to make a model-view-controller?

I need to modify my code so that it becomes a model-view-controller. Since I'm a total noob when it comes to programming my head aches if I'm honest. Any help how to do this would be appreciated.

For a better understanding I will paste the whole task so you can see what my code is for.

*In one pasture there are 200 bustling sheep. The flock consists of 95 white sheep, 60 black sheep and 45 white-black sheep. The shepherd now wants to divide them up so that the corresponding wool can be sheared by color. Help him write a small script so that he can better order his sheep. Please implement the first three points with variables, arrays, mathematical operators and functions. Tip: For a better overview, you can also define your own functions.

  1. Store the sheep in appropriate groups. Work out a concept that works with variables and arrays.
  2. Create 200 sheep with the properties name, number of legs, color and assign them to a stable!
  3. Assign the sheep randomly to a number of sheds.*

Here's my code sample, as I said the following part has to become a model-view-controller. How do i do this?

Here is my code:

<?php
$stableForEachColor = array("stableForWhite", "stableForBlack", "stableForWhiteBlack");
// Function to get the full amount of all sheep
function legs($all)
{
    $legs = 4;
    return $all * $legs;
}

$whiteSheep = 95;
$blackSheep = 60;
$whiteBlackSheep = 45;
$all = $whiteSheep + $blackSheep + $whiteBlackSheep;
echo "At the pasture there are $all sheep in total.\n\n";



$legs = legs($all);
echo "All sheep combined have $legs legs in total.\n\n";

// Names of the white sheep
for ($colorWhite = 1; $colorWhite <= $whiteSheep; $colorWhite++) {
    echo "Wooly $colorWhite is white.\n";
}
// Names of the black sheep
for ($colorBlack = 1; $colorBlack <= $blackSheep; $colorBlack++) {
    echo "Wooly $colorBlack is black.\n";
}

// Names of the white-black sheep
for ($colorWhiteBlack = 1; $colorWhiteBlack <= $whiteBlackSheep; $colorWhiteBlack++) {
    echo "Wooly $colorWhiteBlack is white-black.\n";
}



$stable = array('white_sheep' => $colorWhite, 'black_sheep' => $colorBlack, 'white-black_sheep' => $colorWhiteBlack);
echo '<pre>';
print_r($stable);

$sheep = 200;
$stablesC = 4;
for ($i = $sheep; $i > 0; $i--) {
    $stables[rand(0, ($stablesC - 1))] += 1;
}
echo '<pre>';
print_r($stables);

Upvotes: 0

Views: 90

Answers (1)

Raxi
Raxi

Reputation: 2860

Most of that task involves just restructuring your code, to separate...well, the M, V and C bits.

There is no one true answer, but based on a quick glance i suspect the model part may look roughly like:

  • abstract class Sheep { ... }
    • with private properties $name , $numberoflegs, $color
    • with public function getName() and getLegs() and getColor()
    • with public function setName($x) and setLegs($x)
    • with public __construct($color, $shed, ?$legs, ?$name) function
  • class WhiteSheep extends Sheep { ... }
    • with public __construct($shed) function
  • class BlackSheep extends Sheep { ... }
    • with public __construct($shed) function
  • class StripedSheep extends Sheep { ... }
    • with public __construct($shed) function
  • abstract class SheepCollection implements Iterator { ... }
    • public function addSheep($sheep)
    • public function removeSheep($sheep)
    • public function transferSheep($sheep, $shed)
    • public function setName($x)
    • public function getName()
    • public function getLegs()
    • public function getAmountOfSheep()
    • private property $name
    • private array $sheep
    • public __construct($name)
  • class Stable extends SheepCollection { ... }
    • with public __construct() (could be a singleton)
  • class Shed extends SheepCollection { ... }
    • with public __construct($name)

and your view layer can likely be very slim (just a couple methods in total).

anyway, maybe this gives you a little of an idea on where to start.

Upvotes: 1

Related Questions