Untitled
Guest 99 4th Dec, 2024
<?php
require_once '../vendor/autoload.php';
$numbersInput = [7,3];
$battery = new Battery(BatteryType::AA, $numbersOfBattery);
$calculator = new Calculator();
$calculator->setNumbers([3,4]);
$result = $calculator->multiple();
echo ' ';
echo $result;
echo $calculator->checkBattery();
echo ' ';
$calculator->setNumbers([7,2]);
$result = $calculator->divide();
echo ' ';
echo $result;
echo $calculator->checkBattery();
$calculator->setNumbers([7,2]);
$result = $calculator->divide();
echo ' ';
echo $result;
echo $calculator->checkBattery();
$calculator->setNumbers([7,2]);
$result = $calculator->divide();
echo ' ';
echo $result;
echo $calculator->checkBattery();
$calculator->setNumbers([7,2]);
$result = $calculator->divide();
echo ' ';
echo $result;
echo $calculator->checkBattery();
$calculator->setNumbers([7,2]);
$result = $calculator->divide();
echo ' ';
echo $result;
echo $calculator->checkBattery();
class Calculator {
private array $numbers;
private int $battery = 100;
public function __construct(private Battery $battery) {
}
public function setNumbers(array $numbers): void {
$this->numbers = $numbers;
}
private function batteryComsumption(): void {
$this->battery -= $this->battery->batteryComsumption();
if($this->battery < 0) {
throw new Exception('No battery');
}
}
public function checkBattery(): string {
return 'Battery Level: '.$this->battery."\n";
}
/**
* @return int
*/
public function multiple(): int {
$this->batteryComsumption();
$multiple = $this->numbers[0];
unset($this->numbers[0]);
foreach($this->numbers as $number) {
$multiple *= $number;
}
return $multiple;
}
public function divide(): float {
$this->batteryComsumption();
$divide = $this->numbers[0];
unset($this->numbers[0]);
foreach($this->numbers as $number) {
$divide /= $number;
}
return $divide;
}
}
class Battery {
public function __construct(private BatteryType $batteryType, private int $numbersOfBattery): void
{}
public function batteryComsumption(): int
{
$comsuption = match($this->batteryType) {
BatteryType::AA => 25,
BatteryType::AAA => 50,
};
return $comsuption/$this->numbersOfBattery;
}
}
Enum BatteryType{
case AA;
case AAA;
}
To share this paste please copy this url and send to your friends
RAW Paste Data