puneet Posted October 14, 2014 Share Posted October 14, 2014 Write a class for feeding a horse. The Horse object should keep track of the last time it was fed and calculate when it is hungry. Assume there is another class that checks the horse every 10 minutes to see if it is hungry and if it is, it calls another method which feeds it. There should be about 3 methods on the class, but more can be added if needed. It's was question in my exam which I skipped Can somebody hep me with that Please Quote Link to comment https://forums.phpfreaks.com/topic/291615-creating-class/ Share on other sites More sharing options...
Frank_b Posted October 14, 2014 Share Posted October 14, 2014 (edited) class Horse { private fedtime; public function __construct() { $this->fedtime = new DateTime('yesterday'); } public function getLastFedTime() { return $this->fedtime; } public function setLastFedTime($time) $this->fedtime = $time; } public function isHungry() { $now = new DateTime(); $interval = date_diff($this->fedtime, $now); return $interval->format('%a') > 1; // gives true if the fedtime is more then one day ago, otherwise false. } } And now you do the rest! Edited October 14, 2014 by Frank_b 1 Quote Link to comment https://forums.phpfreaks.com/topic/291615-creating-class/#findComment-1493509 Share on other sites More sharing options...
ignace Posted October 14, 2014 Share Posted October 14, 2014 (edited) class Horse { public function __construct(DateTime $lastFedAt) { $this->lastFedAt = $lastFedAt; } public function feed() { $this->lastFedAt = new DateTime(); } public function isHungry() { if (date('H') < 6 || date('H') > 22) { return false; // sleeping } return time() - $this->lastFedAt->getTimestamp() > 4 * 3600; // feed every 4 hours } } class HorseFeeder { public function check(Horse $horse) { $horse->isHungry() && $horse->feed(); } public function checkLoop(Horse $horse) { while (true) { $this->check($horse); sleep(600); } } Edited October 14, 2014 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/291615-creating-class/#findComment-1493515 Share on other sites More sharing options...
ginerjm Posted October 14, 2014 Share Posted October 14, 2014 You skipped a class, a test no less, and you want someone here to write your answer for you. SHAME ON YOU AND SHAME ON ANYONE HERE WHO GIVES YOU WHAT YOU ASK FOR. Quote Link to comment https://forums.phpfreaks.com/topic/291615-creating-class/#findComment-1493516 Share on other sites More sharing options...
Strider64 Posted October 14, 2014 Share Posted October 14, 2014 You skipped a class, a test no less, and you want someone here to write your answer for you. SHAME ON YOU AND SHAME ON ANYONE HERE WHO GIVES YOU WHAT YOU ASK FOR. He skipped the question on the test, probably couldn't answer it. Probably just trying to see what the answer was that was on the test. Quote Link to comment https://forums.phpfreaks.com/topic/291615-creating-class/#findComment-1493518 Share on other sites More sharing options...
ginerjm Posted October 14, 2014 Share Posted October 14, 2014 Then he should get a solution from the teacher or from the other students so that he gets something appropriate for the context of his course. Quote Link to comment https://forums.phpfreaks.com/topic/291615-creating-class/#findComment-1493521 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.