Advertisement
alexx876

Untitled

May 31st, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. <?php
  2. interface ShipCompareable {
  3. public function compare($a);
  4. }
  5. //15. корабль, пароход, парусник, корвет.
  6. abstract class Ship implements ShipCompareable{
  7. protected $vodoizm;//Водоизмещение
  8. protected $nazv;//назв коробля
  9. protected $date;//дата спуска на воду
  10.  
  11. public function __construct($nazv, $vodoizm, $date)
  12. {
  13. $this->nazv = $nazv;
  14. $this->vodoizm = $vodoizm;
  15. $this->date = $date;
  16. }
  17.  
  18. public function setNazv($nazv) {
  19. $this->nazv = $nazv;
  20. }
  21.  
  22. public function setVodoizm($vodoizm) {
  23. $this->vodoizm = $vodoizm;
  24. }
  25.  
  26. public function setDate($date) {
  27. $this->date = $date;
  28. }
  29.  
  30. public function getNazv() {
  31. echo 'Nazvanie '.$this->nazv;
  32. }
  33.  
  34. public function getVodoizm() {
  35. echo 'Vodoizmeshchenie '.$this->vodoizm;
  36. }
  37.  
  38. public function getDate() {
  39. echo 'Data '.$this->date;
  40. }
  41.  
  42. public function __toString() {
  43. return 'Nazvanie '.$this->nazv.' Vodoizmeshchenie '.$this->vodoizm.' Data '.$this->date;
  44. }
  45. public function compare($a) {
  46. return get_class($a)::compare($a);
  47. }
  48. }
  49.  
  50. class Parohod extends Ship {
  51. private $country;
  52.  
  53. public function __construct($nazv, $vodoizm, $date, $country)
  54. {
  55. parent::__construct($nazv, $vodoizm, $date);
  56. $this->country = $country;
  57. }
  58.  
  59. public function setCountry($country) {
  60. $this->country = $country;
  61. }
  62.  
  63. public function getCountry() {
  64. echo 'Strana '.$this->country;
  65. }
  66.  
  67. public function __toString() {
  68. return 'Nazvanie '.$this->nazv.' Vodoizmeshchenie '.$this->vodoizm.' Data '.$this->date.' Strana '.$this->country;
  69. }
  70. public function compare($a) {
  71. if (get_class($this) > get_class($a)) return 1;
  72. return ($this->vodoizm > $a->vodoizm) ? 0 : 1;
  73. }
  74. }
  75.  
  76. class Parus extends Ship {
  77. private $ekipazh;
  78.  
  79. public function __construct($nazv, $vodoizm, $date, $ekipazh)
  80. {
  81. parent::__construct($nazv, $vodoizm, $date);
  82. $this->ekipazh = $ekipazh;
  83. }
  84.  
  85. public function setEkipazh($ekipazh) {
  86. $this->ekipazh = $ekipazh;
  87. }
  88.  
  89. public function getEkipazh() {
  90. echo 'Ekipazh '.$this->ekipazh;
  91. }
  92.  
  93. public function __toString() {
  94. return 'Nazvanie '.$this->nazv.' Vodoizmeshchenie '.$this->vodoizm.' Data '.$this->date.' Ekipazh '.$this->ekipazh; //экипаж
  95. }
  96. public function compare($a) {
  97. if (get_class($this) > get_class($a)) return 1;
  98. return ($this->vodoizm > $a->vodoizm) ? 0 : 1;
  99. }
  100. }
  101.  
  102. class Korvet extends Ship {
  103. private $engine;
  104.  
  105. public function __construct($nazv, $vodoizm, $date, $engine)
  106. {
  107. parent::__construct($nazv, $vodoizm, $date);
  108. $this->engine = $engine;
  109. }
  110.  
  111. public function setEngine($engine) {
  112. $this->engine = $engine;
  113. }
  114.  
  115. public function getEngine() {
  116. echo 'Dvigatel '.$this->engine;
  117. }
  118.  
  119. public function __toString() {
  120. return 'Nazvanie '.$this->nazv.' Vodoizmeshchenie '.$this->vodoizm.' Data '.$this->date.' Dvigatel '.$this->engine; //двигатель
  121. }
  122. public function compare($a) {
  123. if (get_class($this) > get_class($a)) return 1;
  124. return ($this->vodoizm > $a->vodoizm) ? 0 : 1;
  125. }
  126. }
  127. class ShipList {
  128. protected $array = [];
  129.  
  130. public function addToEnd($item) {
  131. $this->array[] = $item;
  132. }
  133.  
  134. public function addToPosition($item, $index) {
  135. array_splice( $this->array, $index, 0, $item );
  136. }
  137.  
  138. public function deleteByIndex($index) {
  139. array_splice($this->array, $index, 1);
  140. }
  141.  
  142. public function deleteAllElements() {
  143. $this->array = [];
  144. }
  145.  
  146. public function printByIndex($index) {
  147. echo $this->array[$index];
  148. }
  149.  
  150. private function sortByField() {
  151. for($i=0; $i< count($this->array); $i++){
  152. for($j=$i+1; $j<count($this->array); $j++){
  153. if( $this->array[$i]->compare($this->array[$j]) > 0){
  154. $temp = $this->array[$j];
  155. $this->array[$j] = $this->array[$i];
  156. $this->array[$i] = $temp;
  157. }
  158. }
  159. }
  160. }
  161.  
  162. public function sort() {
  163. $this->sortByField();
  164. }
  165.  
  166. public function printList() {
  167. foreach ($this->array as $item) {
  168. echo $item."\n";
  169. }
  170. }
  171. }
  172. $list = new ShipList();
  173.  
  174. $list->addToEnd(new Parohod('Parohod', 45000, '25.12.1999', 'Great Britain'));
  175. $list->addToEnd(new Parus('Parus', 14000, '17.06.1985',90));
  176. $list->addToEnd(new Parohod('Parohod', 44000, '25.12.1999', 'Great Britain'));
  177. $list->addToEnd(new Parohod('Parohod', 47000, '25.12.1999', 'Great Britain'));
  178. $list->addToEnd(new Korvet('Korvet', 22000, '14.08.1996','4 dizelya'));
  179. $list->addToEnd(new Parus('Parus', 16000, '17.06.1985',90));
  180. $list->addToEnd(new Parus('Parus', 15000, '17.06.1985',90));
  181. $list->addToEnd(new Korvet('Korvet', 23000, '14.08.1996','4 dizelya'));
  182.  
  183. $list->printList();
  184. echo "SORTED: \n\n";
  185. $list->sort();
  186. $list->printList();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement