@ -5,18 +5,56 @@ public class Book {
private Publisher publisher ;
private Publisher publisher ;
private int year ;
private int year ;
private String [ ] authors ;
private String [ ] authors ;
private Publisher [ ] publishers = Publisher . getDefaultArray ( ) ;
Book ( ) { }
Book ( ) { }
Book ( Object name , Object year , Object publisher ) { }
Book ( String name , int year , Publisher publisher ) { }
Book ( Object name , Object author , Object year , Object publisher ) {
Book ( String name , String author , int year , Publisher publisher ) {
this ( name , year , publisher ) ;
this ( name , year , publisher ) ;
setAuthors ( uppendAuthors ( ( String ) author ) ) ;
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 ) ;
this ( name , year , publisher ) ;
String [ ] a = ( String [ ] ) authors ;
String [ ] a = ( String [ ] ) authors ;
setAuthors ( a ) ;
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 ) {
public String [ ] uppendAuthors ( String author ) {
String [ ] result ;
String [ ] result ;
try {
try {
@ -25,15 +63,14 @@ public class Book {
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
result = new String [ 1 ] ;
result = new String [ 1 ] ;
result [ 0 ] = author ;
result [ 0 ] = author ;
return result ;
} finally {
}
result = new String [ getAuthorsCount ( ) + 1 ] ;
result = new String [ getAuthorsCount ( ) + 1 ] ;
int i = 0 ;
int i = 0 ;
while ( i < getAuthorsCount ( ) ) {
while ( i < getAuthorsCount ( ) ) {
result [ i ] = getAuthorByIndex ( i ) ;
result [ i ] = getAuthorByIndex ( i ) ;
i + + ;
i + + ;
}
}
result [ getAuthorsCount ( ) ] = author ;
}
return result ;
return result ;
}
}
public String getAuthorByIndex ( int index ) {
public String getAuthorByIndex ( int index ) {
@ -42,8 +79,15 @@ public class Book {
}
}
//количество авторов книги
//количество авторов книги
public int getAuthorsCount ( ) {
public int getAuthorsCount ( ) {
//TODO проверить наличие массива - null
int count = 0 ;
return getAuthors ( ) . length ;
try {
if ( getAuthors ( ) = = null ) throw new NullPointerException ( "авторы еще не добавлены в список ..." ) ;
} catch ( NullPointerException e ) {
count = 0 ;
} finally {
count = getAuthors ( ) . length ;
}
return count ;
}
}
/ * *
/ * *
* @param name the name to set
* @param name the name to set
@ -95,7 +139,25 @@ public class Book {
* @return the author
* @return the author
* /
* /
public String [ ] getAuthors ( ) {
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 + + ;
}
}
}
}
}