You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.4 KiB

2 years ago
package ru.molokoin;
public class Book {
private String name;
private Publisher publisher;
private int year;
private String[] authors;
2 years ago
private Publisher[] publishers = Publisher.getDefaultArray();
2 years ago
Book(){}
2 years ago
Book(String name, int year, Publisher publisher){}
Book(String name, String author, int year, Publisher publisher){
2 years ago
this(name, year, publisher);
2 years ago
setAuthors(uppendAuthors((String)author));
2 years ago
}
2 years ago
Book(String name, String[] authors, int year, Publisher publisher){
2 years ago
this(name, year, publisher);
String[] a = (String[]) authors;
setAuthors(a);
}
2 years ago
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 добавляем список авторов
2 years ago
public String[] uppendAuthors(String author){
String[] result;
try {
if (getAuthors() == null)throw new NullPointerException("Перечень авторов пока пуст ...");
} catch (NullPointerException e) {
e.printStackTrace();
result = new String[1];
result[0] = author;
2 years ago
} finally {
result = new String[getAuthorsCount() +1];
int i = 0;
while (i < getAuthorsCount()){
result[i] = getAuthorByIndex(i);
i++;
}
2 years ago
}
return result;
}
2 years ago
public String getAuthorByIndex(int index){
String author = getAuthors()[index];
return author;
}
//количество авторов книги
public int getAuthorsCount(){
2 years ago
int count = 0;
try {
if (getAuthors() == null) throw new NullPointerException("авторы еще не добавлены в список ...");
} catch (NullPointerException e) {
count = 0;
} finally {
count = getAuthors().length;
}
return count;
2 years ago
}
/**
* @param name the name to set
*/
public void setName(String name) {
2 years ago
//TODO название книги не должно быть пустой ссылкой;
2 years ago
this.name = name;
}
/**
* @param publisher the publisher to set
*/
public void setPublisher(Publisher publisher) {
2 years ago
//TODO издательство не должно быть пустой ссылкой;
2 years ago
this.publisher = publisher;
}
/**
* @param year the year to set
*/
public void setYear(int year) {
2 years ago
//TODO год издания должен быть строго больше нуля
2 years ago
this.year = year;
}
/**
* @param author the author to set
*/
public void setAuthors(String[] authors) {
2 years ago
//TODO массив не должен содержать пустых ссылок
2 years ago
this.authors = authors;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the publisher
*/
public Publisher getPublisher() {
return publisher;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @return the author
*/
public String[] getAuthors() {
2 years ago
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++;
}
2 years ago
}
}