Created
May 19, 2022 08:27
-
-
Save EDDYMENS/56c934ba4b5fbe31a4b6196f60a145b8 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 | |
class MiniRetirement { | |
private $workedYears = 0; | |
private $retirementYears = 0; | |
private $totalYears = 0; | |
private $unaccountedYears = 0; | |
public function __construct($totalYears) { | |
$this->unaccountedYears = $this->totalYears = $totalYears; | |
return $this; | |
} | |
public function addWorkYear($monthlyIncome, $monthlySpend, $retirementMonthlySpend=null) { | |
$retirementMonthlySpend = $retirementMonthlySpend ?? $monthlySpend; | |
$this->workedYears++; | |
$this->unaccountedYears--; | |
$amountSavedInAYear = floatval(($monthlyIncome-$monthlySpend)*12); | |
$numberOfRetirements = floatval($amountSavedInAYear/($retirementMonthlySpend*12)); | |
$this->retirementYears += $numberOfRetirements; | |
$this->unaccountedYears -= $numberOfRetirements; | |
return $this; | |
} | |
public function print() { | |
echo "Worked Years: ".$this->workedYears.'<br>'; | |
echo "Retirement Years: ".$this->retirementYears.'<br>'; | |
echo "Unaccounted Years: ".$this->unaccountedYears.'<br>'; | |
return $this; | |
} | |
} | |
(new MiniRetirement(18)) | |
->addWorkYear(10000, 2000) | |
->addWorkYear(10000, 1500) | |
->addWorkYear(10000, 1500) | |
->addWorkYear(10000, 2000) | |
->addWorkYear(10000, 1500) | |
->addWorkYear(10000, 1500) | |
->print(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment