alexx876

Untitled

May 31st, 2019
707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.64 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 0;
  72.         return ($this->nazv[0] > $a->nazv[0]) ? 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 0;
  98.         return ($this->nazv[0] > $a->nazv[0]) ? 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 0;
  124.         return ($this->nazv[0] > $a->nazv[0]) ? 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 sortByClass() {
  151.         for($i=0; $i< count($this->array); $i++){
  152.             for($j=$i+1; $j<count($this->array); $j++){
  153.                 if(get_class($this->array[$i]) < get_class($this->array[$j])){
  154.                     $temp = $this->array[$j];
  155.                     $this->array[$j] = $this->array[$i];
  156.                     $this->array[$i] = $temp;
  157.                 }
  158.             }
  159.         }
  160.     }
  161.  
  162.     private function sortByField() {
  163.         for($i=0; $i< count($this->array); $i++){
  164.             for($j=$i+1; $j<count($this->array); $j++){
  165.                 if( $this->array[$i]->compare($this->array[$j]) > 0){
  166.                     $temp = $this->array[$j];
  167.                     $this->array[$j] = $this->array[$i];
  168.                     $this->array[$i] = $temp;
  169.                 }
  170.             }
  171.         }
  172.     }
  173.  
  174.     public function sort() {
  175.         //$this->sortByClass();
  176.         $this->sortByField();
  177.     }
  178.  
  179.     public function printList() {
  180.         foreach ($this->array as $item) {
  181.             echo $item."\n";
  182.         }
  183.     }
  184. }
  185. $list = new ShipList();
  186.  
  187. $list->addToEnd(new Parohod('Parohod', 45000, '25.12.1999', 'Great Britain'));
  188. $list->addToEnd(new Parus('Parus', 14000, '17.06.1985',90));
  189. $list->addToEnd(new Parohod('Parohod', 44000, '25.12.1999', 'Great Britain'));
  190. $list->addToEnd(new Parohod('Parohod', 47000, '25.12.1999', 'Great Britain'));
  191. $list->addToEnd(new Korvet('Korvet', 22000, '14.08.1996','4 dizelya'));
  192. $list->addToEnd(new Parus('Parus', 16000, '17.06.1985',90));
  193. $list->addToEnd(new Parus('Parus', 15000, '17.06.1985',90));
  194. $list->addToEnd(new Korvet('Korvet', 23000, '14.08.1996','4 dizelya'));
  195.  
  196. $list->printList();
  197. echo "SORTED: \n\n";
  198. $list->sort();
  199. $list->printList();
Advertisement
Add Comment
Please, Sign In to add comment