Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface ShipCompareable {
- public function compare($a);
- }
- //15. корабль, пароход, парусник, корвет.
- abstract class Ship implements ShipCompareable{
- protected $vodoizm;//Водоизмещение
- protected $nazv;//назв коробля
- protected $date;//дата спуска на воду
- public function __construct($nazv, $vodoizm, $date)
- {
- $this->nazv = $nazv;
- $this->vodoizm = $vodoizm;
- $this->date = $date;
- }
- public function setNazv($nazv) {
- $this->nazv = $nazv;
- }
- public function setVodoizm($vodoizm) {
- $this->vodoizm = $vodoizm;
- }
- public function setDate($date) {
- $this->date = $date;
- }
- public function getNazv() {
- echo 'Nazvanie '.$this->nazv;
- }
- public function getVodoizm() {
- echo 'Vodoizmeshchenie '.$this->vodoizm;
- }
- public function getDate() {
- echo 'Data '.$this->date;
- }
- public function __toString() {
- return 'Nazvanie '.$this->nazv.' Vodoizmeshchenie '.$this->vodoizm.' Data '.$this->date;
- }
- public function compare($a) {
- return get_class($a)::compare($a);
- }
- }
- class Parohod extends Ship {
- private $country;
- public function __construct($nazv, $vodoizm, $date, $country)
- {
- parent::__construct($nazv, $vodoizm, $date);
- $this->country = $country;
- }
- public function setCountry($country) {
- $this->country = $country;
- }
- public function getCountry() {
- echo 'Strana '.$this->country;
- }
- public function __toString() {
- return 'Nazvanie '.$this->nazv.' Vodoizmeshchenie '.$this->vodoizm.' Data '.$this->date.' Strana '.$this->country;
- }
- public function compare($a) {
- return ($this->vodoizm < $a->vodoizm) ? 0 : 1;
- }
- }
- class Parus extends Ship {
- private $ekipazh;
- public function __construct($nazv, $vodoizm, $date, $ekipazh)
- {
- parent::__construct($nazv, $vodoizm, $date);
- $this->ekipazh = $ekipazh;
- }
- public function setEkipazh($ekipazh) {
- $this->ekipazh = $ekipazh;
- }
- public function getEkipazh() {
- echo 'Ekipazh '.$this->ekipazh;
- }
- public function __toString() {
- return 'Nazvanie '.$this->nazv.' Vodoizmeshchenie '.$this->vodoizm.' Data '.$this->date.' Ekipazh '.$this->ekipazh; //экипаж
- }
- public function compare($a) {
- return ($this->vodoizm < $a->vodoizm) ? 0 : 1;
- }
- }
- class Korvet extends Ship {
- private $engine;
- public function __construct($nazv, $vodoizm, $date, $engine)
- {
- parent::__construct($nazv, $vodoizm, $date);
- $this->engine = $engine;
- }
- public function setEngine($engine) {
- $this->engine = $engine;
- }
- public function getEngine() {
- echo 'Dvigatel '.$this->engine;
- }
- public function __toString() {
- return 'Nazvanie '.$this->nazv.' Vodoizmeshchenie '.$this->vodoizm.' Data '.$this->date.' Dvigatel '.$this->engine; //двигатель
- }
- public function compare($a) {
- return ($this->vodoizm < $a->vodoizm) ? 0 : 1;
- }
- }
- class ShipList {
- protected $array = [];
- public function addToEnd($item) {
- $this->array[] = $item;
- }
- public function addToPosition($item, $index) {
- array_splice( $this->array, $index, 0, $item );
- }
- public function deleteByIndex($index) {
- array_splice($this->array, $index, 1);
- }
- public function deleteAllElements() {
- $this->array = [];
- }
- public function printByIndex($index) {
- echo $this->array[$index];
- }
- private function sortByClass() {
- for($i=0; $i< count($this->array); $i++){
- for($j=$i+1; $j<count($this->array); $j++){
- if(get_class($this->array[$i]) > get_class($this->array[$j])){
- $temp = $this->array[$j];
- $this->array[$j] = $this->array[$i];
- $this->array[$i] = $temp;
- }
- }
- }
- }
- private function sortByField() {
- for($i=0; $i< count($this->array); $i++){
- for($j=$i+1; $j<count($this->array); $j++){
- if(get_class($this->array[$i]) == get_class($this->array[$j]) && $this->array[$i]->compare($this->array[$j]) != 0){
- $temp = $this->array[$j];
- $this->array[$j] = $this->array[$i];
- $this->array[$i] = $temp;
- }
- }
- }
- }
- public function sort() {
- $this->sortByClass();
- $this->sortByField();
- }
- public function printList() {
- foreach ($this->array as $item) {
- echo $item."\n";
- }
- }
- }
- $list = new ShipList();
- $list->addToEnd(new Parohod('Parohod', 45000, '25.12.1999', 'Great Britain'));
- $list->addToEnd(new Parus('Parus', 14000, '17.06.1985',90));
- $list->addToEnd(new Parohod('Parohod', 44000, '25.12.1999', 'Great Britain'));
- $list->addToEnd(new Parohod('Parohod', 47000, '25.12.1999', 'Great Britain'));
- $list->addToEnd(new Korvet('Korvet', 22000, '14.08.1996','4 dizelya'));
- $list->addToEnd(new Parus('Parus', 15000, '17.06.1985',90));
- $list->addToEnd(new Parus('Parus', 16000, '17.06.1985',90));
- $list->addToEnd(new Korvet('Korvet', 23000, '14.08.1996','4 dizelya'));
- $list->printList();
- echo "SORTED: \n\n";
- $list->sort();
- $list->printList();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement