<?php
class CreditCalculator {
private $credit;
private $servicePayment;
private $percent;
private $monthlyPayment;
private $totalPayment = 0;
private $totalMonths = 0;
public function __construct
(array $data) {
foreach ($data as $k => $v)
{
$this->{$k} = $v;
}
}
public function getValue($value)
{
return isset($this->$value) ?
$this->$value : 'No such element here'; }
private function updateDebt()
{
$this->credit *= 1 + $this->percent / 100;
$this->credit += $this->servicePayment;
}
private function payOnDebt()
{
if ($this->credit >= $this->monthlyPayment) {
$this->credit -= $this->monthlyPayment;
$this->totalPayment += $this->monthlyPayment;
} else {
$this->totalPayment += $this->credit;
$this->credit -= $this->credit;
}
}
private function calculateMonth()
{
$this->updateDebt();
$this->payOnDebt();
}
public function calculateTotal()
{
while ($this->credit > 0) {
if ($this->totalMonths > 1000) {
break;
}
$this->calculateMonth();
$this->totalMonths++;
}
}
public function render()
{
return $this->totalMonths < 1000 ? 'Ваш кредит будет выплачен через ' . $this->getValue('totalMonths') . " месяцев. \n
Вы переплатите: " . round($this->getValue('totalPayment'), 2) : 'К сожалению вы никогда не выплатите Ваш кредит'; }
}
$calculator = new CreditCalculator(['credit' => 40000,
'servicePayment' => 1000,
'percent' => 3,
'monthlyPayment' => 5000]);
$calculator->calculateTotal();
echo $calculator->render();