Primaxm

Untitled

Oct 21st, 2018
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. <?php
  2.  
  3. list($studentFirstName, $studentLastName, $fNumber) = explode(" ", readline());
  4. list($workerFirstName, $workerLastName, $salary, $workingHours) = explode(" ", readline());
  5.  
  6. try {
  7. $student = new Student($studentFirstName, $studentLastName, $fNumber);
  8. $worker = new Worker($workerFirstName, $workerLastName, floatval($salary), floatval($workingHours));
  9.  
  10. echo $student . PHP_EOL;
  11. echo $worker;
  12. } catch (Exception $ex) {
  13. echo $ex->getMessage();
  14. }
  15.  
  16. class Human
  17. {
  18. /**
  19. * @var string
  20. */
  21. private $fisrtName;
  22.  
  23. /**
  24. * @var string
  25. */
  26. private $lastName;
  27.  
  28. /**
  29. * Human constructor.
  30. * @param string $fisrtName
  31. * @param string $lastName
  32. * @throws Exception
  33. */
  34. public function __construct(string $fisrtName, string $lastName)
  35. {
  36. $this->setFisrtName($fisrtName);
  37. $this->setLastName($lastName);
  38. }
  39.  
  40. /**
  41. * @return string
  42. */
  43. public function getFisrtName(): string
  44. {
  45. return $this->fisrtName;
  46. }
  47.  
  48. /**
  49. * @param string $fisrtName
  50. * @throws Exception
  51. */
  52. public function setFisrtName(string $fisrtName): void
  53. {
  54. if (!ctype_upper($fisrtName[0])) {
  55. throw new Exception("Expected upper case letter!Argument: " . $fisrtName);
  56. }
  57. if (strlen($fisrtName) < 4) {
  58. throw new Exception("Expected length at least 4 symbols!Argument: " . $fisrtName);
  59. }
  60. $this->fisrtName = $fisrtName;
  61. }
  62.  
  63. /**
  64. * @return string
  65. */
  66. public function getLastName(): string
  67. {
  68. return $this->lastName;
  69. }
  70.  
  71. /**
  72. * @param string $lastName
  73. * @throws Exception
  74. */
  75. public function setLastName(string $lastName): void
  76. {
  77. if (!ctype_upper($lastName[0])) {
  78. throw new Exception("Expected upper case letter!Argument: " . $lastName);
  79. }
  80. if (strlen($lastName) < 3) {
  81. throw new Exception("Expected length at least 3 symbols!Argument: " . $lastName);
  82. }
  83. $this->lastName = $lastName;
  84. }
  85.  
  86. }
  87.  
  88. class Student extends Human
  89. {
  90. /**
  91. * @var string
  92. */
  93. private $facultyNumber;
  94.  
  95. public function __construct(string $fisrtName, string $lastName, string $facultyNumber)
  96. {
  97. parent::__construct($fisrtName, $lastName);
  98. $this->setFacultyNumber($facultyNumber);
  99. }
  100.  
  101. /**
  102. * @return int
  103. */
  104. public function getFacultyNumber(): int
  105. {
  106. return $this->facultyNumber;
  107. }
  108.  
  109. /**
  110. * @param string $facultyNumber
  111. * @throws Exception
  112. */
  113. public function setFacultyNumber(string $facultyNumber): void
  114. {
  115. if (!preg_match("/^[\d]{5,10}$/", $facultyNumber)) {
  116. throw new Exception("Invalid faculty number!");
  117. }
  118. $this->facultyNumber = $facultyNumber;
  119. }
  120.  
  121.  
  122. public function __toString()
  123. {
  124. $output = "First Name: " . parent::getfisrtName() . PHP_EOL . "Last Name: " . parent::getLastName() . PHP_EOL . "Faculty number: " . $this->facultyNumber . PHP_EOL;
  125. return $output;
  126. }
  127.  
  128. }
  129.  
  130.  
  131. class Worker extends Human
  132. {
  133. /**
  134. * @var float
  135. */
  136. private $weekSalary;
  137.  
  138. /**
  139. * @var float
  140. */
  141. private $workHoursPerDay;
  142.  
  143. /**
  144. * Worker constructor.
  145. * @param string $fisrtName
  146. * @param string $lastName
  147. * @param float $weekSalary
  148. * @param float $workHoursPerDay
  149. * @throws Exception
  150. */
  151. public function __construct(string $fisrtName, string $lastName, float $weekSalary, float $workHoursPerDay)
  152. {
  153. parent::__construct($fisrtName, $lastName);
  154. $this->setWeekSalary($weekSalary);
  155. $this->setWorkHoursPerDay($workHoursPerDay);
  156. }
  157.  
  158. /**
  159. * @return float
  160. */
  161. public function getWeekSalary(): float
  162. {
  163. return $this->weekSalary;
  164. }
  165.  
  166. /**
  167. * @param float $weekSalary
  168. * @throws Exception
  169. */
  170. public function setWeekSalary(float $weekSalary): void
  171. {
  172. if ($weekSalary < 11) {
  173. throw new Exception("Expected value mismatch!Argument: " . $weekSalary);
  174. }
  175. $this->weekSalary = $weekSalary;
  176. }
  177.  
  178. /**
  179. * @return float
  180. */
  181. public function getWorkHoursPerDay(): float
  182. {
  183. return $this->workHoursPerDay;
  184. }
  185.  
  186. /**
  187. * @param float $workHoursPerDay
  188. * @throws Exception
  189. */
  190. public function setWorkHoursPerDay(float $workHoursPerDay): void
  191. {
  192.  
  193. if ($workHoursPerDay < 1 || $workHoursPerDay > 12) {
  194. throw new Exception("Expected value mismatch!Argument: " . $workHoursPerDay);
  195. }
  196. $this->workHoursPerDay = $workHoursPerDay;
  197. }
  198.  
  199. public function __toString()
  200. {
  201. $salaryPerHour = number_format($this->weekSalary/($this->workHoursPerDay*7), 2, ".", "");
  202. $ws = number_format($this->weekSalary, 2, ".", "");
  203. $hpd = number_format($this->workHoursPerDay, 2, ".", "");
  204.  
  205. $output = "First Name: " . parent::getfisrtName() . PHP_EOL .
  206. "Last Name: " . parent::getLastName() . PHP_EOL .
  207. "Week Salary: " . $ws . PHP_EOL .
  208. "Hours per day: ". $hpd . PHP_EOL .
  209. "Salary per hour: " . $salaryPerHour;
  210.  
  211. return $output;
  212. }
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment