Guest User

Untitled

a guest
Jun 6th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. <?php
  2.  
  3. class Human
  4. {
  5. /**
  6. * @var string
  7. */
  8. private $firstName;
  9. /**
  10. * @var string
  11. */
  12. private $lastName;
  13.  
  14. /**
  15. * Human constructor.
  16. * @param string $firstName
  17. * @param string $lastName
  18. * @throws Exception
  19. */
  20. public function __construct(string $firstName, string $lastName)
  21. {
  22. $this->setFirstName($firstName);
  23. $this->setLastName($lastName);
  24. }
  25.  
  26. /**
  27. * @return string
  28. */
  29. public function getFirstName(): string
  30. {
  31. return $this->firstName;
  32. }
  33.  
  34. /**
  35. * @param string $firstName
  36. * @throws Exception
  37. */
  38. private function setFirstName(string $firstName): void
  39. {
  40. if ($this->validCapital($firstName)) {
  41. throw new Exception("Expected upper case letter!Argument: firstName\n");
  42. }
  43. if (strlen($firstName) < 4) {
  44. throw new Exception("Expected length at least 4 symbols!Argument: firstName\n");
  45. }
  46. $this->firstName = $firstName;
  47. }
  48.  
  49. /**
  50. * @return string
  51. */
  52. public function getLastName(): string
  53. {
  54. return $this->lastName;
  55. }
  56.  
  57. /**
  58. * @param string $lastName
  59. * @throws Exception
  60. */
  61. private function setLastName(string $lastName): void
  62. {
  63. if ($this->validCapital($lastName)) {
  64. throw new Exception("Expected upper case letter!Argument: lastName\n");
  65. }
  66. if (strlen($lastName) < 3) {
  67. throw new Exception("Expected length at least 3 symbols!Argument: lastName\n");
  68. }
  69. $this->lastName = $lastName;
  70. }
  71.  
  72. /**
  73. * @param string $lastFirst
  74. * @return bool
  75. */
  76. public function validCapital(string $lastFirst): bool
  77. {
  78. return ctype_lower($lastFirst[0]);
  79. }
  80. }
  81.  
  82. class Student extends Human
  83. {
  84. /**
  85. * @var string
  86. */
  87. private $faculty;
  88.  
  89. /**
  90. * Student constructor.
  91. * @param string $firstName
  92. * @param string $lastName
  93. * @param int $faculty
  94. * @throws Exception
  95. */
  96. public function __construct(string $firstName, string $lastName, string $faculty)
  97. {
  98. parent::__construct($firstName, $lastName);
  99. $this->setFaculty($faculty);
  100. }
  101.  
  102. /**
  103. * @return int
  104. */
  105. public function getFaculty(): string
  106. {
  107. return $this->faculty;
  108. }
  109.  
  110. /**
  111. * @param int $faculty
  112. * @throws Exception
  113. */
  114. public function setFaculty(string $faculty): void
  115. {
  116. if ($this->isNumber($faculty)) {
  117. throw new Exception("Invalid faculty number!\n");
  118. }
  119. $this->faculty = $faculty;
  120. }
  121.  
  122. /**
  123. * @param $faculty
  124. * @return bool
  125. */
  126. public function isNumber($faculty): bool
  127. {
  128. return strlen($faculty) < 5 || strlen($faculty) > 10;
  129.  
  130. }
  131.  
  132. /**
  133. * @return string
  134. */
  135. public function __toString()
  136. {
  137. return "First Name: {$this->getFirstName()}\nLast Name: {$this->getLastName()}\nFaculty number: {$this->getFaculty()}\n\n";
  138. }
  139. }
  140.  
  141. class Worker extends Human
  142. {
  143. /**
  144. * @var float
  145. */
  146. private $weekSalary;
  147. /**
  148. * @var float
  149. */
  150. private $workHoursDay;
  151.  
  152. /**
  153. * Worker constructor.
  154. * @param string $firstName
  155. * @param string $lastName
  156. * @param float $weekSalary
  157. * @param float $workHoursDay
  158. * @throws Exception
  159. */
  160. public function __construct(string $firstName, string $lastName, float $weekSalary, float $workHoursDay)
  161. {
  162. parent::__construct($firstName, $lastName);
  163. $this->setWeekSalary($weekSalary);
  164. $this->setWorkHoursDay($workHoursDay);
  165. }
  166.  
  167. /**
  168. * @return float
  169. */
  170. public function getWeekSalary(): float
  171. {
  172. return $this->weekSalary;
  173. }
  174.  
  175. /**
  176. * @param float $weekSalary
  177. * @throws Exception
  178. */
  179. public function setWeekSalary(float $weekSalary): void
  180. {
  181. if ($weekSalary < 10) {
  182. throw new Exception("Expected value mismatch!Argument: weekSalary\n");
  183. }
  184. $this->weekSalary = $weekSalary;
  185. }
  186.  
  187. /**
  188. * @return float
  189. */
  190. public function getWorkHoursDay(): float
  191. {
  192. return $this->workHoursDay;
  193. }
  194.  
  195. /**
  196. * @param float $workHoursDay
  197. * @throws Exception
  198. */
  199. public function setWorkHoursDay(float $workHoursDay): void
  200. {
  201. if ($workHoursDay < 1 || $workHoursDay > 12) {
  202. throw new Exception("Expected value mismatch!Argument: workHoursPerDay\n");
  203. }
  204. $this->workHoursDay = $workHoursDay;
  205. }
  206.  
  207. /**
  208. * @return float
  209. */
  210. public function calculateSalary(): float
  211. {
  212. return $this->weekSalary / ($this->workHoursDay * 7);
  213. }
  214.  
  215. public function __toString()
  216. {
  217. 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";
  218. }
  219. }
  220.  
  221. $students = [];
  222. $workers = [];
  223. try {
  224. $student = explode(" ", readline());
  225. $firstName = $student[0];
  226. $lastName = $student[1];
  227. $faculty = $student[2];
  228. $students[] = new Student($firstName, $lastName, $faculty);
  229. foreach ($students as $item) {
  230. echo $item;
  231. }
  232. $worker = explode(" ", readline());
  233. $firstName = $worker[0];
  234. $lastName = $worker[1];
  235. $weekSalary = floatval($worker[2]);
  236. $hoursPerDay = floatval($worker[3]);
  237. $workers[] = new Worker($firstName, $lastName, $weekSalary, $hoursPerDay);
  238. foreach ($workers as $items) {
  239. echo $items;
  240. }
  241. } catch (Exception $e) {
  242. echo $e->getMessage();
  243. }
Add Comment
Please, Sign In to add comment