Last active
May 23, 2019 23:22
-
-
Save juniorb2ss/eff1c1f6b0abb3c4edf52cdf4c22480a to your computer and use it in GitHub Desktop.
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 | |
$studentFirstPoints = $_GET['a'] ?? null; | |
$studentSecondPoints = $_GET['b'] ?? null; | |
try { | |
$studentPointsCalculator = new StudentPointsCalculator($studentFirstPoints, $studentSecondPoints); | |
$studentPointsCalculator->studentWasApproved(); | |
echo 'Você foi aprovado!'; | |
} catch (StudentCalculatorException $exception) { | |
echo $exception->getMessage(); | |
} | |
class StudentCalculatorException extends Exception {} | |
class StudentPointWasNotAvailable extends StudentCalculatorException | |
{ | |
public function __construct() | |
{ | |
parent::__construct(StudentPointsCalculator::STUDENT_POINTS_WAS_NOT_AVALIABLE); | |
} | |
} | |
class StudentWasReproved extends StudentCalculatorException | |
{ | |
public function __construct() | |
{ | |
parent::__construct(StudentPointsCalculator::STUDENT_WAS_REPROVED); | |
} | |
} | |
class StudentPointsCalculator | |
{ | |
const STUDENT_WAS_REPROVED = 'Voce foi reprovado!'; | |
const STUDENT_POINTS_WAS_NOT_AVALIABLE = 'Nota indisponível.'; | |
const MIN_FINAL_POINTS_TO_BE_APPROVED = 10; | |
const MAX_FINAL_POINTS_TO_BE_APPROVED = 20; | |
/** | |
* @var int | |
*/ | |
private $studentFirstPoints; | |
/** | |
* @var int | |
*/ | |
private $studentSecondPoints; | |
/** | |
* @var int | |
*/ | |
private $finalPoints; | |
/** | |
* Student constructor. | |
* @param int|null $studentFirstPoints | |
* @param int|null $studentSecondPoints | |
*/ | |
function __construct(?int $studentFirstPoints, ?int $studentSecondPoints) | |
{ | |
$this->studentFirstPoints = $studentFirstPoints; | |
$this->studentSecondPoints = $studentSecondPoints; | |
$this->finalPoints = ($this->studentFirstPoints + $this->studentSecondPoints); | |
} | |
/** | |
* @return bool | |
* @throws StudentPointWasNotAvailable | |
*/ | |
private function studentFinalPointsWasAvailable(): bool | |
{ | |
if(is_null($this->studentFirstPoints) || is_null($this->studentSecondPoints)) { | |
throw new StudentPointWasNotAvailable(); | |
} | |
return true; | |
} | |
/** | |
* @return bool | |
* @throws StudentWasReproved | |
* @throws StudentPointWasNotAvailable | |
*/ | |
public function studentWasApproved(): bool | |
{ | |
$this->studentFinalPointsWasAvailable(); | |
if($this->finalPoints <= self::MIN_FINAL_POINTS_TO_BE_APPROVED && $this->finalPoints >= self::MAX_FINAL_POINTS_TO_BE_APPROVED) { | |
throw new StudentWasReproved(); | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment