Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package buildings;
- /**
- * Created with IntelliJ IDEA.
- * User: LongMan
- * Date: 17.11.14
- * Time: 14:54
- * To change this template use File | Settings | File Templates.
- */
- /*
- #TODO
- +Создайте приватный метод получения узла по его номеру.
- +Создайте приватный метод добавления узла в список по номеру.
- +Создайте приватный метод удаления узла из списка по его номеру.
- +Конструктор может принимать количество офисов на этаже.
- +Конструктор может принимать массив офисов этажа.
- +Создайте метод получения количества офисов на этаже.
- +Создайте метод получения общей площади помещений этажа.
- Создайте метод получения общего количества комнат этажа.
- Создайте метод получения массива офисов этажа.
- Создайте метод получения офиса по его номеру на этаже.
- Создайте метод изменения офиса по его номеру на этаже и ссылке на обновленный офис.
- Создайте метод добавления нового офиса на этаже по будущему номеру офиса.
- Создайте метод удаления офиса по его номеру на этаже.
- Создайте метод getBestSpace() получения самого большого по площади офиса этажа.
- */
- class Node {
- private Office office;
- private Node next;
- public Node() {
- office = new Office();
- }
- public Node(Node next) {
- this.next = next;
- }
- public Node(Office office) {
- this.office = office;
- }
- public Office getElement() {
- return office;
- }
- public void setOffice(Office newOffice) {
- office = newOffice;
- }
- public Node getNext() {
- return next;
- }
- public void setNext(Node n) {
- next = n;
- }
- }
- public class OfficeFloor {
- int officeCount;
- private Node head;
- public OfficeFloor(int officeNumber) {
- head = new Node();
- Node lastOffice = head;
- for (int i = 1; i < officeNumber; i++) {
- Node office = new Node();
- lastOffice.setNext(office);
- lastOffice = office;
- if (i == officeNumber - 1) {
- office.setNext(head);
- }
- officeCount++;
- }
- }
- public OfficeFloor(Office[] officeMas) {
- head = new Node();
- head.setOffice(officeMas[0]);
- officeCount = officeMas.length;
- if (officeMas.length != 1) {
- Node lastOffice = head;
- for (int i = 1; i < officeMas.length; i++) {
- Node node = new Node(officeMas[i]);
- lastOffice.setNext(node);
- lastOffice = node;
- if (i == officeMas.length - 1) {
- lastOffice.setNext(head);
- }
- }
- }
- else {
- head.setNext(head);
- }
- }
- public Node getOffice(int number) {
- Node office = new Node();
- if (number != 0) {
- office = head;
- for (int i = 0; i < number; i++) {
- office = office.getNext();
- }
- return office;
- }
- else {
- return head;
- }
- }
- private void addOffice(int number) {
- Node node = new Node();
- if (number != 0) {
- node.setNext(this.getOffice(number - 1).getNext());
- this.getOffice(number - 1).setNext(node);
- }
- else {
- node.setNext(head);
- head = node;
- this.getOffice(this.officeCount - 1).setNext(node);
- }
- officeCount++;
- }
- private void delOffice(int number) {
- if (number != 0) {
- this.getOffice(number - 1).setNext(this.getOffice(number).getNext());
- }
- else {
- this.getOffice(this.officeCount - 1).setNext(head.getNext());
- head = head.getNext();
- }
- officeCount--;
- }
- public int getOfficeCount() {
- if (head != null) {
- Node node = head;
- int number = 1;
- while (node.getNext() != head) {
- node = node.getNext();
- number++;
- }
- return number;
- }
- else {
- return 0;
- }
- /*return officeCount;*/
- }
- public double getArea() {
- if (head != null) {
- int area = 0;
- Node node = head;
- for (int i = 0; i < this.getOfficeCount(); i++) {
- area += node.getElement().getArea();
- node = node.getNext();
- }
- return area;
- }
- else {
- return 0;
- }
- }
- public int getRoomsCount() {
- if (head != null) {
- int number = 0;
- Node node = head;
- for (int i = 0; i < this.getOfficeCount(); i++) {
- number += node.getElement().getRoomsCount();
- node = node.getNext();
- }
- return number;
- }
- else {
- return 0;
- }
- }
- public Office[] getOffices() {
- Office[] officeMassive = new Office[this.getOfficeCount()];
- if (head != null) {
- officeMassive[0] = head.getElement();
- Node node = head;
- for (int i = 1; i < this.getOfficeCount(); i++) {
- node = node.getNext();
- officeMassive[i] = node.getElement();
- }
- return officeMassive;
- }
- else {
- return officeMassive;
- }
- }
- public Office getOfficeByNumber(int number) {
- return this.getOffice(number).getElement();
- }
- public void changeOffice(int number, Office newOffice) {
- Node node = new Node(newOffice);
- node.setNext(this.getOffice(number).getNext());
- this.getOffice(number - 1).setNext(node);
- // return newOffice;
- }
- public void insertNewOffice(int number) {
- this.addOffice(number);
- }
- public void deleteOffice(int number) {
- this.delOffice(number);
- }
- public Office getBestSpace() {
- if (head != null) {
- double maxArea = head.getElement().getArea();
- Office maxAreaOffice = new Office();
- Node node = head;
- for (int i = 0; i < this.getOfficeCount(); i++) {
- if (node.getElement().getArea() > maxArea) {
- maxArea = node.getElement().getArea();
- maxAreaOffice = node.getElement();
- }
- node = node.getNext();
- }
- return maxAreaOffice;
- }
- else {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement