fork download
  1. <?php
  2. class CreditCalculator {
  3. private $credit;
  4. private $servicePayment;
  5. private $percent;
  6. private $monthlyPayment;
  7. private $totalPayment = 0;
  8. private $totalMonths = 0;
  9. public function __construct(array $data)
  10. {
  11. foreach ($data as $k => $v)
  12. {
  13. $this->{$k} = $v;
  14. }
  15. }
  16. public function getValue($value)
  17. {
  18. return isset($this->$value) ? $this->$value : 'No such element here';
  19. }
  20. private function updateDebt()
  21. {
  22. $this->credit *= 1 + $this->percent / 100;
  23. $this->credit += $this->servicePayment;
  24. }
  25. private function payOnDebt()
  26. {
  27. if ($this->credit >= $this->monthlyPayment) {
  28. $this->credit -= $this->monthlyPayment;
  29. $this->totalPayment += $this->monthlyPayment;
  30. } else {
  31. $this->totalPayment += $this->credit;
  32. $this->credit -= $this->credit;
  33. }
  34. }
  35. private function calculateMonth()
  36. {
  37. $this->updateDebt();
  38. $this->payOnDebt();
  39. }
  40. public function calculateTotal()
  41. {
  42.  
  43. while ($this->credit > 0) {
  44. if ($this->totalMonths > 1000) {
  45. break;
  46. }
  47. $this->calculateMonth();
  48. $this->totalMonths++;
  49. }
  50. }
  51. public function render()
  52. {
  53. return $this->totalMonths < 1000 ? 'Ваш кредит будет выплачен через ' . $this->getValue('totalMonths') . " месяцев. \n
  54. Вы переплатите: " . round($this->getValue('totalPayment'), 2) : 'К сожалению вы никогда не выплатите Ваш кредит';
  55. }
  56. }
  57. $calculator = new CreditCalculator(['credit' => 40000,
  58. 'servicePayment' => 1000,
  59. 'percent' => 3,
  60. 'monthlyPayment' => 5000]);
  61. $calculator->calculateTotal();
  62. echo $calculator->render();
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
Ваш кредит будет выплачен через  13 месяцев. 

		Вы переплатите: 61270.19