<?php

/* 
Некто кладет в банк 10000 р.
Банк начисляет 10% годовых 
(то есть, каждый год на счету становится на 10% больше, чем в прошлом году). 
Напиши программу, считающую, через сколько лет в банке будет миллион? 
Сколько лет будет этому некто? 
Доживет ли некто до этого дня, если сегодня ему 16 лет? */

define('TARGET', 1000000);

class Man
{
    private $years;
    private $averageDurationLife = 72;

    public function __construct(int $years)
    {
        $this->years = $years;
    }

    public function isAlive(): bool
    {
        if($this->years < $this->averageDurationLife) {
            return true;
        } else {
            return false;
        }
    }
    
    public function addYear(): self
    {
    	$this->years++;
    	return $this;
    }
    
    public function getYears(): int
    {
    	return $this->years;
    }
}

class Bill
{
    private $amount;
    private $annual;

    public function __construct(int $amount, int $annual)
    {
        $this->amount = $amount;
        $this->annual = $annual;
    }

    public function getAmount(): float
    {
        return $this->amount;
    }

    public function totalizeAnnual(): self
    {
        $this->amount += $this->amount * $this->annual / 100;
        return $this;
    }
}

class Calculator
{
    private $man;
    private $bill;

    public function __construct(Man $man, Bill $bill)
    {
        $this->man = $man;
        $this->bill = $bill;
    }

    public function getResult(): array
    {
        while($this->man->isAlive() and TARGET > $this->bill->getAmount()) {
            $this->bill->totalizeAnnual();
            $this->man->addYear();
        }
        return [
            $this->man->isAlive(),
            $this->bill->getAmount(),
            $this->man->getYears()
        ];
    }
}

$calculator = new Calculator(
    new Man(16),
    new Bill(10000, 10)
);

$result = $calculator->getResult();
echo $result[0] === true ? "Парень жив\n" : "Земля парню пухом\n";
echo "Он успел накопить: ".(int)$result[1]." рублей\n";
echo "И ему сейчас ".$result[2]." лет";