Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Human
- {
- /**
- * @var string
- */
- private $firstName;
- /**
- * @var string
- */
- private $lastName;
- /**
- * Human constructor.
- * @param string $firstName
- * @param string $lastName
- * @throws Exception
- */
- public function __construct(string $firstName, string $lastName)
- {
- $this->setFirstName($firstName);
- $this->setLastName($lastName);
- }
- /**
- * @return string
- */
- public function getFirstName(): string
- {
- return $this->firstName;
- }
- /**
- * @param string $firstName
- * @throws Exception
- */
- private function setFirstName(string $firstName): void
- {
- if ($this->validCapital($firstName)) {
- throw new Exception("Expected upper case letter!Argument: firstName\n");
- }
- if (strlen($firstName) < 4) {
- throw new Exception("Expected length at least 4 symbols!Argument: firstName\n");
- }
- $this->firstName = $firstName;
- }
- /**
- * @return string
- */
- public function getLastName(): string
- {
- return $this->lastName;
- }
- /**
- * @param string $lastName
- * @throws Exception
- */
- private function setLastName(string $lastName): void
- {
- if ($this->validCapital($lastName)) {
- throw new Exception("Expected upper case letter!Argument: lastName\n");
- }
- if (strlen($lastName) < 3) {
- throw new Exception("Expected length at least 3 symbols!Argument: lastName\n");
- }
- $this->lastName = $lastName;
- }
- /**
- * @param string $lastFirst
- * @return bool
- */
- public function validCapital(string $lastFirst): bool
- {
- return ctype_lower($lastFirst[0]);
- }
- }
- class Student extends Human
- {
- /**
- * @var string
- */
- private $faculty;
- /**
- * Student constructor.
- * @param string $firstName
- * @param string $lastName
- * @param int $faculty
- * @throws Exception
- */
- public function __construct(string $firstName, string $lastName, string $faculty)
- {
- parent::__construct($firstName, $lastName);
- $this->setFaculty($faculty);
- }
- /**
- * @return int
- */
- public function getFaculty(): string
- {
- return $this->faculty;
- }
- /**
- * @param int $faculty
- * @throws Exception
- */
- public function setFaculty(string $faculty): void
- {
- if ($this->isNumber($faculty)) {
- throw new Exception("Invalid faculty number!\n");
- }
- $this->faculty = $faculty;
- }
- /**
- * @param $faculty
- * @return bool
- */
- public function isNumber($faculty): bool
- {
- return strlen($faculty) < 5 || strlen($faculty) > 10;
- }
- /**
- * @return string
- */
- public function __toString()
- {
- return "First Name: {$this->getFirstName()}\nLast Name: {$this->getLastName()}\nFaculty number: {$this->getFaculty()}\n\n";
- }
- }
- class Worker extends Human
- {
- /**
- * @var float
- */
- private $weekSalary;
- /**
- * @var float
- */
- private $workHoursDay;
- /**
- * Worker constructor.
- * @param string $firstName
- * @param string $lastName
- * @param float $weekSalary
- * @param float $workHoursDay
- * @throws Exception
- */
- public function __construct(string $firstName, string $lastName, float $weekSalary, float $workHoursDay)
- {
- parent::__construct($firstName, $lastName);
- $this->setWeekSalary($weekSalary);
- $this->setWorkHoursDay($workHoursDay);
- }
- /**
- * @return float
- */
- public function getWeekSalary(): float
- {
- return $this->weekSalary;
- }
- /**
- * @param float $weekSalary
- * @throws Exception
- */
- public function setWeekSalary(float $weekSalary): void
- {
- if ($weekSalary < 10) {
- throw new Exception("Expected value mismatch!Argument: weekSalary\n");
- }
- $this->weekSalary = $weekSalary;
- }
- /**
- * @return float
- */
- public function getWorkHoursDay(): float
- {
- return $this->workHoursDay;
- }
- /**
- * @param float $workHoursDay
- * @throws Exception
- */
- public function setWorkHoursDay(float $workHoursDay): void
- {
- if ($workHoursDay < 1 || $workHoursDay > 12) {
- throw new Exception("Expected value mismatch!Argument: workHoursPerDay\n");
- }
- $this->workHoursDay = $workHoursDay;
- }
- /**
- * @return float
- */
- public function calculateSalary(): float
- {
- return $this->weekSalary / ($this->workHoursDay * 7);
- }
- public function __toString()
- {
- return "First Name: {$this->getFirstName()}\nLast Name: {$this->getLastName()}\nWeek Salary: " . number_format($this->weekSalary, 2, ".", "") . "\nHours per day: " . number_format($this->getWorkHoursDay(), 2, ".", "") . "\nSalary per hour: " . number_format($this->calculateSalary(), 2, ".", "") . "\n";
- }
- }
- $students = [];
- $workers = [];
- try {
- $student = explode(" ", readline());
- $firstName = $student[0];
- $lastName = $student[1];
- $faculty = $student[2];
- $students[] = new Student($firstName, $lastName, $faculty);
- foreach ($students as $item) {
- echo $item;
- }
- $worker = explode(" ", readline());
- $firstName = $worker[0];
- $lastName = $worker[1];
- $weekSalary = floatval($worker[2]);
- $hoursPerDay = floatval($worker[3]);
- $workers[] = new Worker($firstName, $lastName, $weekSalary, $hoursPerDay);
- foreach ($workers as $items) {
- echo $items;
- }
- } catch (Exception $e) {
- echo $e->getMessage();
- }
Add Comment
Please, Sign In to add comment