fork download
  1. <?php
  2.  
  3. /*
  4. Некто кладет в банк 10000 р.
  5. Банк начисляет 10% годовых
  6. (то есть, каждый год на счету становится на 10% больше, чем в прошлом году).
  7. Напиши программу, считающую, через сколько лет в банке будет миллион?
  8. Сколько лет будет этому некто?
  9. Доживет ли некто до этого дня, если сегодня ему 16 лет? */
  10.  
  11. define('TARGET', 1000000);
  12.  
  13. class Man
  14. {
  15. private $years;
  16. private $averageDurationLife = 72;
  17.  
  18. public function __construct(int $years)
  19. {
  20. $this->years = $years;
  21. }
  22.  
  23. public function isAlive(): bool
  24. {
  25. if($this->years < $this->averageDurationLife) {
  26. return true;
  27. } else {
  28. return false;
  29. }
  30. }
  31.  
  32. public function addYear(): self
  33. {
  34. $this->years++;
  35. return $this;
  36. }
  37.  
  38. public function getYears(): int
  39. {
  40. return $this->years;
  41. }
  42. }
  43.  
  44. class Bill
  45. {
  46. private $amount;
  47. private $annual;
  48.  
  49. public function __construct(int $amount, int $annual)
  50. {
  51. $this->amount = $amount;
  52. $this->annual = $annual;
  53. }
  54.  
  55. public function getAmount(): float
  56. {
  57. return $this->amount;
  58. }
  59.  
  60. public function totalizeAnnual(): self
  61. {
  62. $this->amount += $this->amount * $this->annual / 100;
  63. return $this;
  64. }
  65. }
  66.  
  67. class Calculator
  68. {
  69. private $man;
  70. private $bill;
  71.  
  72. public function __construct(Man $man, Bill $bill)
  73. {
  74. $this->man = $man;
  75. $this->bill = $bill;
  76. }
  77.  
  78. public function getResult(): array
  79. {
  80. while($this->man->isAlive() and TARGET > $this->bill->getAmount()) {
  81. $this->bill->totalizeAnnual();
  82. $this->man->addYear();
  83. }
  84. return [
  85. $this->man->isAlive(),
  86. $this->bill->getAmount(),
  87. $this->man->getYears()
  88. ];
  89. }
  90. }
  91.  
  92. $calculator = new Calculator(
  93. new Man(16),
  94. new Bill(10000, 10)
  95. );
  96.  
  97. $result = $calculator->getResult();
  98. echo $result[0] === true ? "Парень жив\n" : "Земля парню пухом\n";
  99. echo "Он успел накопить: ".(int)$result[1]." рублей\n";
  100. echo "И ему сейчас ".$result[2]." лет";
Success #stdin #stdout 0.02s 24312KB
stdin
Standard input is empty
stdout
Парень жив
Он успел накопить: 1067189 рублей
И ему сейчас 65 лет