Advertisement
alexx876

Untitled

May 16th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.48 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.         return ($this->vodoizm < $a->vodoizm) ? 0 : 1;
  72.     }
  73. }
  74.  
  75. class Parus extends Ship {
  76.     private $ekipazh;
  77.  
  78.     public function __construct($nazv, $vodoizm, $date, $ekipazh)
  79.     {
  80.         parent::__construct($nazv, $vodoizm, $date);
  81.         $this->ekipazh = $ekipazh;
  82.     }
  83.  
  84.     public function setEkipazh($ekipazh) {
  85.         $this->ekipazh = $ekipazh;
  86.     }
  87.  
  88.     public function getEkipazh() {
  89.         echo 'Ekipazh '.$this->ekipazh;
  90.     }
  91.  
  92.     public function __toString() {
  93.         return 'Nazvanie '.$this->nazv.' Vodoizmeshchenie '.$this->vodoizm.' Data '.$this->date.' Ekipazh '.$this->ekipazh; //экипаж
  94.     }
  95.     public function compare($a) {
  96.         return ($this->vodoizm < $a->vodoizm) ? 0 : 1;
  97.     }
  98. }
  99.  
  100. class Korvet extends Ship {
  101.     private $engine;
  102.  
  103.     public function __construct($nazv, $vodoizm, $date, $engine)
  104.     {
  105.         parent::__construct($nazv, $vodoizm, $date);
  106.         $this->engine = $engine;
  107.     }
  108.  
  109.     public function setEngine($engine) {
  110.         $this->engine = $engine;
  111.     }
  112.  
  113.     public function getEngine() {
  114.         echo 'Dvigatel '.$this->engine;
  115.     }
  116.  
  117.     public function __toString() {
  118.         return 'Nazvanie '.$this->nazv.' Vodoizmeshchenie '.$this->vodoizm.' Data '.$this->date.' Dvigatel '.$this->engine; //двигатель
  119.     }
  120.     public function compare($a) {
  121.         return ($this->vodoizm < $a->vodoizm) ? 0 : 1;
  122.     }
  123. }
  124. class ShipList {
  125.     protected $array = [];
  126.  
  127.     public function addToEnd($item) {
  128.         $this->array[] = $item;
  129.     }
  130.  
  131.     public function addToPosition($item, $index) {
  132.         array_splice( $this->array, $index, 0, $item );
  133.     }
  134.  
  135.     public function deleteByIndex($index) {
  136.         array_splice($this->array, $index, 1);
  137.     }
  138.  
  139.     public function deleteAllElements() {
  140.         $this->array = [];
  141.     }
  142.  
  143.     public function printByIndex($index) {
  144.         echo $this->array[$index];
  145.     }
  146.  
  147.     private function sortByClass() {
  148.         for($i=0; $i< count($this->array); $i++){
  149.             for($j=$i+1; $j<count($this->array); $j++){
  150.                 if(get_class($this->array[$i]) > get_class($this->array[$j])){
  151.                     $temp = $this->array[$j];
  152.                     $this->array[$j] = $this->array[$i];
  153.                     $this->array[$i] = $temp;
  154.                 }
  155.             }
  156.         }
  157.     }
  158.  
  159.     private function sortByField() {
  160.         for($i=0; $i< count($this->array); $i++){
  161.             for($j=$i+1; $j<count($this->array); $j++){
  162.                 if(get_class($this->array[$i]) == get_class($this->array[$j]) && $this->array[$i]->compare($this->array[$j]) != 0){
  163.                     $temp = $this->array[$j];
  164.                     $this->array[$j] = $this->array[$i];
  165.                     $this->array[$i] = $temp;
  166.                 }
  167.             }
  168.         }
  169.     }
  170.  
  171.     public function sort() {
  172.         $this->sortByClass();
  173.         $this->sortByField();
  174.     }
  175.  
  176.     public function printList() {
  177.         foreach ($this->array as $item) {
  178.             echo $item."\n";
  179.         }
  180.     }
  181. }
  182. $list = new ShipList();
  183.  
  184. $list->addToEnd(new Parohod('Parohod', 45000, '25.12.1999', 'Great Britain'));
  185. $list->addToEnd(new Parus('Parus', 14000, '17.06.1985',90));
  186. $list->addToEnd(new Parohod('Parohod', 44000, '25.12.1999', 'Great Britain'));
  187. $list->addToEnd(new Parohod('Parohod', 47000, '25.12.1999', 'Great Britain'));
  188. $list->addToEnd(new Korvet('Korvet', 22000, '14.08.1996','4 dizelya'));
  189. $list->addToEnd(new Parus('Parus', 15000, '17.06.1985',90));
  190. $list->addToEnd(new Parus('Parus', 16000, '17.06.1985',90));
  191. $list->addToEnd(new Korvet('Korvet', 23000, '14.08.1996','4 dizelya'));
  192.  
  193. $list->printList();
  194. echo "SORTED: \n\n";
  195. $list->sort();
  196. $list->printList();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement