Skip to content

Instantly share code, notes, and snippets.

@EDDYMENS
Created May 19, 2022 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EDDYMENS/56c934ba4b5fbe31a4b6196f60a145b8 to your computer and use it in GitHub Desktop.
Save EDDYMENS/56c934ba4b5fbe31a4b6196f60a145b8 to your computer and use it in GitHub Desktop.
<?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