Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.27 KB | None | 0 0
  1. package buildings;
  2.  
  3. /**
  4.  * Created with IntelliJ IDEA.
  5.  * User: LongMan
  6.  * Date: 17.11.14
  7.  * Time: 14:54
  8.  * To change this template use File | Settings | File Templates.
  9.  */
  10.  
  11.  /*
  12. #TODO
  13.     +Создайте приватный метод получения узла по его номеру.
  14.     +Создайте приватный метод добавления узла в список по номеру.
  15.     +Создайте приватный метод удаления узла из списка по его номеру.
  16.     +Конструктор может принимать количество офисов на этаже.
  17.     +Конструктор может принимать массив офисов этажа.
  18.     +Создайте метод получения количества офисов на этаже.
  19.     +Создайте метод получения общей площади помещений этажа.
  20.     Создайте метод получения общего количества комнат этажа.
  21.     Создайте метод получения массива офисов этажа.
  22.     Создайте метод получения офиса по его номеру на этаже.
  23.     Создайте метод изменения офиса по его номеру на этаже и ссылке на обновленный офис.
  24.     Создайте метод добавления нового офиса на этаже по будущему номеру офиса.
  25.     Создайте метод удаления офиса по его номеру на этаже.
  26.     Создайте метод getBestSpace() получения самого большого по площади офиса этажа.
  27.    */
  28. class Node {
  29.  
  30.     private Office office;
  31.     private Node next;
  32.  
  33.     public Node() {
  34.         office = new Office();
  35.     }
  36.  
  37.     public Node(Node next) {
  38.         this.next = next;
  39.     }
  40.  
  41.     public Node(Office office) {
  42.         this.office = office;
  43.     }
  44.  
  45.     public Office getElement() {
  46.         return office;
  47.     }
  48.  
  49.     public void setOffice(Office newOffice) {
  50.         office = newOffice;
  51.     }
  52.  
  53.     public Node getNext() {
  54.         return next;
  55.     }
  56.  
  57.     public void setNext(Node n) {
  58.         next = n;
  59.     }
  60. }
  61.  
  62. public class OfficeFloor {
  63.  
  64.     int officeCount;
  65.     private Node head;
  66.  
  67.     public OfficeFloor(int officeNumber) {
  68.         head = new Node();
  69.         Node lastOffice = head;
  70.         for (int i = 1; i < officeNumber; i++) {
  71.             Node office = new Node();
  72.             lastOffice.setNext(office);
  73.             lastOffice = office;
  74.             if (i == officeNumber - 1) {
  75.                 office.setNext(head);
  76.             }
  77.             officeCount++;
  78.         }
  79.  
  80.  
  81.     }
  82.  
  83.     public OfficeFloor(Office[] officeMas) {
  84.         head = new Node();
  85.         head.setOffice(officeMas[0]);
  86.         officeCount = officeMas.length;
  87.         if (officeMas.length != 1) {
  88.             Node lastOffice = head;
  89.  
  90.             for (int i = 1; i < officeMas.length; i++) {
  91.                 Node node = new Node(officeMas[i]);
  92.                 lastOffice.setNext(node);
  93.                 lastOffice = node;
  94.                 if (i == officeMas.length - 1) {
  95.  
  96.                     lastOffice.setNext(head);
  97.                 }
  98.             }
  99.         }
  100.         else {
  101.             head.setNext(head);
  102.         }
  103.     }
  104.  
  105.     public Node getOffice(int number) {
  106.         Node office = new Node();
  107.         if (number != 0) {
  108.             office = head;
  109.             for (int i = 0; i < number; i++) {
  110.                 office = office.getNext();
  111.             }
  112.             return office;
  113.         }
  114.         else {
  115.             return head;
  116.         }
  117.     }
  118.  
  119.     private void addOffice(int number) {
  120.         Node node = new Node();
  121.         if (number != 0) {
  122.             node.setNext(this.getOffice(number - 1).getNext());
  123.             this.getOffice(number - 1).setNext(node);
  124.         }
  125.         else {
  126.             node.setNext(head);
  127.             head = node;
  128.             this.getOffice(this.officeCount - 1).setNext(node);
  129.         }
  130.         officeCount++;
  131.     }
  132.  
  133.     private void delOffice(int number) {
  134.  
  135.         if (number != 0) {
  136.             this.getOffice(number - 1).setNext(this.getOffice(number).getNext());
  137.         }
  138.         else {
  139.  
  140.             this.getOffice(this.officeCount - 1).setNext(head.getNext());
  141.             head = head.getNext();
  142.         }
  143.         officeCount--;
  144.     }
  145.  
  146.     public int getOfficeCount() {
  147.         if (head != null) {
  148.             Node node = head;
  149.             int number = 1;
  150.             while (node.getNext() != head) {
  151.                 node = node.getNext();
  152.                 number++;
  153.             }
  154.             return number;
  155.         }
  156.         else {
  157.             return 0;
  158.         }
  159.         /*return officeCount;*/
  160.     }
  161.  
  162.     public double getArea() {
  163.         if (head != null) {
  164.             int area = 0;
  165.             Node node = head;
  166.             for (int i = 0; i < this.getOfficeCount(); i++) {
  167.                 area += node.getElement().getArea();
  168.                 node = node.getNext();
  169.             }
  170.             return area;
  171.         }
  172.         else {
  173.             return 0;
  174.         }
  175.     }
  176.  
  177.     public int getRoomsCount() {
  178.         if (head != null) {
  179.             int number = 0;
  180.             Node node = head;
  181.             for (int i = 0; i < this.getOfficeCount(); i++) {
  182.                 number += node.getElement().getRoomsCount();
  183.                 node = node.getNext();
  184.             }
  185.             return number;
  186.         }
  187.         else {
  188.             return 0;
  189.         }
  190.     }
  191.  
  192.     public Office[] getOffices() {
  193.         Office[] officeMassive = new Office[this.getOfficeCount()];
  194.         if (head != null) {
  195.             officeMassive[0] = head.getElement();
  196.             Node node = head;
  197.             for (int i = 1; i < this.getOfficeCount(); i++) {
  198.                 node = node.getNext();
  199.                 officeMassive[i] = node.getElement();
  200.             }
  201.             return officeMassive;
  202.         }
  203.         else {
  204.             return officeMassive;
  205.         }
  206.     }
  207.  
  208.     public Office getOfficeByNumber(int number) {
  209.  
  210.         return this.getOffice(number).getElement();
  211.     }
  212.  
  213.     public void changeOffice(int number, Office newOffice) {
  214.  
  215.         Node node = new Node(newOffice);
  216.         node.setNext(this.getOffice(number).getNext());
  217.         this.getOffice(number - 1).setNext(node);
  218.         // return newOffice;
  219.     }
  220.  
  221.     public void insertNewOffice(int number) {
  222.         this.addOffice(number);
  223.     }
  224.  
  225.     public void deleteOffice(int number) {
  226.         this.delOffice(number);
  227.     }
  228.  
  229.     public Office getBestSpace() {
  230.         if (head != null) {
  231.             double maxArea = head.getElement().getArea();
  232.             Office maxAreaOffice = new Office();
  233.             Node node = head;
  234.             for (int i = 0; i < this.getOfficeCount(); i++) {
  235.                 if (node.getElement().getArea() > maxArea) {
  236.                     maxArea = node.getElement().getArea();
  237.                     maxAreaOffice = node.getElement();
  238.                 }
  239.                 node = node.getNext();
  240.             }
  241.             return maxAreaOffice;
  242.         }
  243.         else {
  244.             return null;
  245.         }
  246.     }
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement