diff --git a/book/src/main/java/ru/molokoin/Book.java b/book/src/main/java/ru/molokoin/Book.java index a07a96b..8988d32 100644 --- a/book/src/main/java/ru/molokoin/Book.java +++ b/book/src/main/java/ru/molokoin/Book.java @@ -5,18 +5,56 @@ public class Book { private Publisher publisher; private int year; private String[] authors; + private Publisher[] publishers = Publisher.getDefaultArray(); + Book(){} - Book(Object name, Object year, Object publisher){} - Book(Object name, Object author, Object year, Object publisher){ + Book(String name, int year, Publisher publisher){} + Book(String name, String author, int year, Publisher publisher){ this(name, year, publisher); setAuthors(uppendAuthors((String)author)); } - Book(Object name, Object[] authors, Object year, Object publisher){ + Book(String name, String[] authors, int year, Publisher publisher){ this(name, year, publisher); String[] a = (String[]) authors; setAuthors(a); } - //добавляем список авторов + public static Book[] getDefaultArray(){ + Book[] books = new Book[2]; + + //book 1 + String name = "Computer Science: основы программирования на Java, ООП, алгоритмы и структуры данных"; + books[0] = new Book(); + books[0].setName(name); + books[0].uppendAuthors("Седжвик Роберт"); + books[0].uppendAuthors("Уэйн Кевин"); + books[0].setPublisher(Publisher.getPublisherByName(Publisher.getDefaultArray(), "Питер")); + books[0].setYear(2018); + + //book 2 + name = "Разработка требований к программному обеспечению. 3-е издание, дополненное"; + books[1] = new Book(); + books[1].setName(name); + books[1].uppendAuthors("Вигерс Карл"); + books[1].setPublisher(Publisher.getPublisherByName(Publisher.getDefaultArray(), "БХВ")); + books[1].setYear(2019); + + return books; + } + public void print(){ + System.out.println("-------------------------------"); + System.out.println(toString()); + System.out.println("Наименование книги (name) :: " + name); + getPublisher().print(); + System.out.println("Год издания (year) :: " + year); + System.out.println("Авторы:"); + int i = 0; + while (i < getAuthorsCount()){ + System.out.println((i + 1) + ". " + getAuthors()[i]); + i++; + } + System.out.println("-------------------------------"); + } + //TODO добавляем список авторов public String[] uppendAuthors(String author){ String[] result; try { @@ -25,15 +63,14 @@ public class Book { e.printStackTrace(); result = new String[1]; result[0] = author; - return result; - } - result = new String[getAuthorsCount() + 1]; - int i = 0; - while (i < getAuthorsCount()){ - result[i] = getAuthorByIndex(i); - i++; + } finally { + result = new String[getAuthorsCount() +1]; + int i = 0; + while (i < getAuthorsCount()){ + result[i] = getAuthorByIndex(i); + i++; + } } - result[getAuthorsCount()] = author; return result; } public String getAuthorByIndex(int index){ @@ -42,8 +79,15 @@ public class Book { } //количество авторов книги public int getAuthorsCount(){ - //TODO проверить наличие массива - null - return getAuthors().length; + int count = 0; + try { + if (getAuthors() == null) throw new NullPointerException("авторы еще не добавлены в список ..."); + } catch (NullPointerException e) { + count = 0; + } finally { + count = getAuthors().length; + } + return count; } /** * @param name the name to set @@ -95,7 +139,25 @@ public class Book { * @return the author */ public String[] getAuthors() { - return authors; + String[] result = new String[0]; + try { + if (authors == null) throw new NullPointerException("Авторы еще не заносились в список ..."); + } catch (NullPointerException e) { + // TODO: handle exception + e.printStackTrace(); + result = new String[0]; + } finally { + result = authors; + } + return result; + } + public static void main(String[] args) { + Book[] books = Book.getDefaultArray(); + int i = 0; + while (i < books.length){ + books[i].print(); + i++; + } } } diff --git a/book/src/main/java/ru/molokoin/Publisher.java b/book/src/main/java/ru/molokoin/Publisher.java index 3d1563d..23a0a3d 100644 --- a/book/src/main/java/ru/molokoin/Publisher.java +++ b/book/src/main/java/ru/molokoin/Publisher.java @@ -11,15 +11,11 @@ public class Publisher { setName(name); setSity(sity); } - //выводим в консоль данные Издательства - public void print(){ - System.out.println("Издательство: " + getName() + " ----> " + "город: " + getSity()); - } /** * Метод класса. Возвращает массив данных об издательствах по умолчанию * @return */ - public static Publisher[] getDefaults(){ + public static Publisher[] getDefaultArray(){ Publisher[] defaultPublishers = new Publisher[4]; defaultPublishers[0] = new Publisher("Проспект", "Москва"); defaultPublishers[1] = new Publisher("Питер", "Санкт-Петербург"); @@ -27,6 +23,10 @@ public class Publisher { defaultPublishers[3] = new Publisher("Диалектика", "Киев"); return defaultPublishers; } + //выводим в консоль данные Издательства + public void print(){ + System.out.println("Издательство: " + getName() + " ----> " + "город: " + getSity()); + } /** * @param name the name to set * DONE: имя издательства не олжно быть пустой ссылкой @@ -67,8 +67,26 @@ public class Publisher { public String getSity() { return sity; } + public static Publisher getPublisherByName(Publisher[] publishers, String name){ + Publisher publisher = new Publisher(); + boolean isAcsists = false; + int i = 0; + while (i < publishers.length){ + if (name.equals((String)publishers[i].getName())){ + publisher = publishers[i]; + isAcsists = true; + } + i++; + } + try { + if(!isAcsists) throw new Exception("Указанное наименование издательства не найдено в каталоге. Установлено наименование города по умолчанию {noname sity} ..."); + } catch (Exception e) { + publisher = new Publisher(name, null); + } + return publisher; + } public static void main(String[] args) { - Publisher[] publishers = Publisher.getDefaults(); + Publisher[] publishers = Publisher.getDefaultArray(); int i = 0; while (i < publishers.length){ publishers[i].print(); diff --git a/book/target/classes/ru/molokoin/Book.class b/book/target/classes/ru/molokoin/Book.class index 689b20b..a71e01d 100644 Binary files a/book/target/classes/ru/molokoin/Book.class and b/book/target/classes/ru/molokoin/Book.class differ diff --git a/book/target/classes/ru/molokoin/Publisher.class b/book/target/classes/ru/molokoin/Publisher.class index d36d316..b522194 100644 Binary files a/book/target/classes/ru/molokoin/Publisher.class and b/book/target/classes/ru/molokoin/Publisher.class differ