Last active
November 17, 2020 22:11
-
-
Save StandardNerd/fd6cf363329eb88395395ef8470dd4b5 to your computer and use it in GitHub Desktop.
PHP Exercise
This file contains 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 | |
require_once 'participant.php'; | |
$bob = new Participant('Bob'); | |
$bill = new Participant('Bill'); | |
$bob->surviveOktoberfest(); | |
$bill->surviveOktoberfest(); | |
echo "\n"; | |
echo 'Bierspesen Bob: ' . sprintf("%.2f", $bob->getTotalCost()/100.0) . " CHF\n"; | |
echo 'Bierspesen Bill: ' . sprintf("%.2f", $bill->getTotalCost()/100.0) . " CHF\n"; | |
echo 'Literverbrauch Bob: ' . $bob->getTotalConsumption() . "\n"; | |
echo 'Literverbrauch Bill: ' . $bill->getTotalConsumption() . "\n"; |
This file contains 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 | |
/** "Oktober-Fest" Example: | |
* During 3.5h Bill & Bob are ordering every hour 1l of beer. | |
* 1l of beer costs 14.95CHF. | |
* They toast themselves to each new round: "Prost!" | |
*/ | |
class Participant { | |
public $duration; | |
public $total_cost = 0; | |
public $total_consumption = 0; | |
function __construct($name, $duration=3.5, $beer_price=1495, $beer_per_hour=1) { | |
$this->name = $name; | |
$this->duration = $duration; | |
$this->beer_price = $beer_price; | |
$this->beer_per_hour = $beer_per_hour; | |
} | |
public function toast() : string { | |
return "Prost!"; | |
} | |
public function order() : void { | |
$this->total_cost += $this->beer_price; | |
$this->total_consumption += $this->beer_per_hour; | |
} | |
public function getTotalCost() : int { | |
return $this->total_cost; | |
} | |
public function getTotalConsumption() : int { | |
return $this->total_consumption; | |
} | |
public function surviveOctoberFest() : void { | |
for ($i = 0; $i < ceil($this->duration); $i++) { | |
$this->order(); | |
echo $this->toast(); | |
} | |
} | |
} |
This file contains 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 ParticipantTest extends PHPUnit\Framework\TestCase | |
{ | |
public static function setUpBeforeClass() : void | |
{ | |
require_once 'participant.php'; | |
} | |
public function setUp() : void { | |
$this->bob = new Participant('Bob'); | |
$this->beer_price = 1495; | |
} | |
public function testGetDuration() : void { | |
$this->assertEquals(3.5, $this->bob->duration); | |
} | |
public function testGreeting() : void { | |
$this->assertEquals("Prost!", $this->bob->toast()); | |
} | |
public function testGetTotalCost() : void { | |
$this->assertEquals(0, $this->bob->getTotalCost()); | |
$this->bob->order(); | |
$this->assertEquals($this->beer_price, $this->bob->getTotalCost()); | |
$this->bob->order(); | |
$this->assertEquals(($this->beer_price * 2), $this->bob->getTotalCost()); | |
} | |
public function testGetTotalConsumption() : void { | |
$this->assertEquals(0, $this->bob->getTotalConsumption()); | |
$this->bob->order(); | |
$this->assertEquals(1, $this->bob->getTotalConsumption()); | |
$this->bob->order(); | |
$this->assertEquals(2, $this->bob->getTotalConsumption()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment