diff --git a/src/main/java/com/epam/izh/rd/online/entity/Author.java b/src/main/java/com/epam/izh/rd/online/entity/Author.java index 166be587..8c73ec6b 100644 --- a/src/main/java/com/epam/izh/rd/online/entity/Author.java +++ b/src/main/java/com/epam/izh/rd/online/entity/Author.java @@ -5,7 +5,7 @@ /** * Класс содержащий информацию об авторе. - * + *

* Необходимо: * 1) Создать список полей с указанными типами ровно в этом порядке: * - name с типом String и приватным модификатором доступа @@ -19,5 +19,82 @@ * 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset) */ public class Author { + private String name; + private String lastName; + private LocalDate birthdate; + private String country; + public Author() { + } + + public Author(String name, String lastName, LocalDate birthdate, String country) { + this.name = name; + this.lastName = lastName; + this.birthdate = birthdate; + this.country = country; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public LocalDate getBirthdate() { + return birthdate; + } + + public void setBirthdate(LocalDate birthdate) { + this.birthdate = birthdate; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + + Author author = (Author) obj; + + if (!Objects.equals(name, author.name)) return false; + if (!Objects.equals(lastName, author.lastName)) return false; + if (!Objects.equals(birthdate, author.birthdate)) return false; + return Objects.equals(country, author.country); + } + + @Override + public int hashCode() { + int result = name != null ? name.hashCode() : 0; + result = 31 * result + (lastName != null ? lastName.hashCode() : 0); + result = 31 * result + (birthdate != null ? birthdate.hashCode() : 0); + result = 31 * result + (country != null ? country.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "Author{" + + "name='" + name + '\'' + + ", lastName='" + lastName + '\'' + + ", birthdate=" + birthdate + + ", country='" + country + '\'' + + '}'; + } } diff --git a/src/main/java/com/epam/izh/rd/online/entity/Book.java b/src/main/java/com/epam/izh/rd/online/entity/Book.java index 08bdccb8..b87a8163 100644 --- a/src/main/java/com/epam/izh/rd/online/entity/Book.java +++ b/src/main/java/com/epam/izh/rd/online/entity/Book.java @@ -4,7 +4,7 @@ /** * Базовая сущность для книги. Содержит базовые поля. - * + *

* Необходимо: * 1) Создать список полей с указанными типами ровно в этом порядке: * - numberOfPages с типом int и приватным модификатором доступа @@ -16,5 +16,60 @@ * 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset) */ public abstract class Book { + private int numberOfPages; //1)Создать список полей с указанными типами + private String name; + public Book() { //2)Создать дефолтный конструктор (без параметров) + } + + public Book(int numberOfPages, String name) {//3)Создать конструктор со всеми параметрами + this.numberOfPages = numberOfPages; //(в том порядке в котором перечислены) + this.name = name; + } + + public abstract String getAuthorName(); + + public abstract String getAuthorLastName(); + + public int getNumberOfPages() { + return numberOfPages; + } + + public void setNumberOfPages(int numberOfPages) { + this.numberOfPages = numberOfPages; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + + Book book = (Book) obj; + + if (numberOfPages != book.numberOfPages) return false; + return Objects.equals(name, book.name); + } + + @Override + public int hashCode() { + int result = numberOfPages; + result = 31 * result + (name != null ? name.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "Book{" + + "numberOfPages=" + numberOfPages + + ", name='" + name + '\'' + + '}'; + } } diff --git a/src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java b/src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java index a9834db4..fb610a88 100644 --- a/src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java +++ b/src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java @@ -20,5 +20,80 @@ * 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset) */ public class SchoolBook extends Book { + private String authorName; + private String authorLastName; + private LocalDate publishDate; + public SchoolBook() { + } + + public SchoolBook(String authorName, String authorLastName, LocalDate publishDate) { + this.authorName = authorName; + this.authorLastName = authorLastName; + this.publishDate = publishDate; + } + + public SchoolBook(int numberOfPages, String name, String authorName, String authorLastName, LocalDate publishDate) { + super(numberOfPages, name); + this.authorName = authorName; + this.authorLastName = authorLastName; + this.publishDate = publishDate; + } + + public String getAuthorName() { + return authorName; + } + + public void setAuthorName(String authorName) { + this.authorName = authorName; + } + + public String getAuthorLastName() { + return authorLastName; + } + + public void setAuthorLastName(String authorLastName) { + this.authorLastName = authorLastName; + } + + public LocalDate getPublishDate() { + return publishDate; + } + + public void setPublishDate(LocalDate publishDate) { + this.publishDate = publishDate; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + if (!super.equals(obj)) return false; + + SchoolBook that = (SchoolBook) obj; + + if (!Objects.equals(authorName, that.authorName)) return false; + if (!Objects.equals(authorLastName, that.authorLastName)) + return false; + return Objects.equals(publishDate, that.publishDate); + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (authorName != null ? authorName.hashCode() : 0); + result = 31 * result + (authorLastName != null ? authorLastName.hashCode() : 0); + result = 31 * result + (publishDate != null ? publishDate.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "SchoolBook{" + + "authorName='" + authorName + '\'' + + ", authorLastName='" + authorLastName + '\'' + + ", publishDate=" + publishDate + + '}'; + } } + diff --git a/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java new file mode 100644 index 00000000..e854a92c --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java @@ -0,0 +1,42 @@ +package com.epam.izh.rd.online.repository; + +import com.epam.izh.rd.online.entity.Author; +import org.apache.commons.lang3.ArrayUtils; + +public class SimpleAuthorRepository implements AuthorRepository { + private Author[] authors = new Author[]{}; + + @Override + public boolean save(Author author) { + if (findByFullName(author.getName(),author.getLastName()) == null) { + authors = ArrayUtils.add(authors, author); + return true; + } + return false; + } + + @Override + public Author findByFullName(String name, String lastname) { + for (Author author: authors) { + if (author.getName().equals(name) && author.getLastName().equals(lastname)) { + return author; + } + } + return null; + } + + @Override + public boolean remove(Author author) { + if (findByFullName(author.getName(),author.getLastName()) != null) { + int index = ArrayUtils.indexOf(authors, author); + authors = ArrayUtils.remove(authors, index); + return true; + } + return false; + } + + @Override + public int count() { + return authors.length; + } +} diff --git a/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java new file mode 100644 index 00000000..a4722af8 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java @@ -0,0 +1,44 @@ +package com.epam.izh.rd.online.repository; + +import com.epam.izh.rd.online.entity.SchoolBook; +import org.apache.commons.lang3.ArrayUtils; + +public class SimpleSchoolBookRepository implements BookRepository { + private SchoolBook[] schoolBooks = new SchoolBook[]{}; + + @Override + public boolean save(SchoolBook book) { + schoolBooks = ArrayUtils.add(schoolBooks, book); + return true; + } + + @Override + public SchoolBook[] findByName(String name) { + SchoolBook[] foundBooks = new SchoolBook[]{}; + for (SchoolBook newBook: schoolBooks) { + if (newBook.getName().equals(name)) { + foundBooks = ArrayUtils.add(foundBooks, newBook); + } + } + return foundBooks; + } + + @Override + public boolean removeByName(String name) { + SchoolBook[] onDelete = findByName(name); + int index; + if (onDelete.length == 0) { + return false; + } + for (SchoolBook bookOnDelete: onDelete) { + index = ArrayUtils.indexOf(schoolBooks, bookOnDelete); + schoolBooks = ArrayUtils.remove(schoolBooks, index); + } + return true; + } + + @Override + public int count() { + return schoolBooks.length; + } +} diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java b/src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java new file mode 100644 index 00000000..bd885470 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java @@ -0,0 +1,35 @@ +package com.epam.izh.rd.online.service; + +import com.epam.izh.rd.online.entity.Author; +import com.epam.izh.rd.online.repository.AuthorRepository; + +public class SimpleAuthorService implements AuthorService { + private AuthorRepository authorRepository; + + public SimpleAuthorService() { + } + + public SimpleAuthorService(AuthorRepository authorRepository) { + this.authorRepository = authorRepository; + } + + @Override + public boolean save(Author author) { + return authorRepository.save(author); + } + + @Override + public Author findByFullName(String name, String lastname) { + return authorRepository.findByFullName(name, lastname); + } + + @Override + public boolean remove(Author author) { + return authorRepository.remove(author); + } + + @Override + public int count() { + return authorRepository.count(); + } +} diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java new file mode 100644 index 00000000..dc3f8fb5 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java @@ -0,0 +1,54 @@ +package com.epam.izh.rd.online.service; + +import com.epam.izh.rd.online.entity.Author; +import com.epam.izh.rd.online.entity.Book; +import com.epam.izh.rd.online.entity.SchoolBook; +import com.epam.izh.rd.online.repository.BookRepository; + +public class SimpleSchoolBookService implements BookService { + private BookRepository schoolBookBookRepository; + private AuthorService authorService; + + public SimpleSchoolBookService() { + } + + public SimpleSchoolBookService(BookRepository schoolBookBookRepository, AuthorService authorService) { + this.schoolBookBookRepository = schoolBookBookRepository; + this.authorService = authorService; + } + + @Override + public boolean save(Book book) { + if (authorService.findByFullName(book.getAuthorName(),book.getAuthorLastName()) != null) { + schoolBookBookRepository.save((SchoolBook) book); + return true; + } + return false; + } + + @Override + public Book[] findByName(String name) { + return schoolBookBookRepository.findByName(name); + } + + @Override + public int getNumberOfBooksByName(String name) { + return findByName(name).length; + } + + @Override + public boolean removeByName(String name) { + return schoolBookBookRepository.removeByName(name); + } + + @Override + public int count() { + return schoolBookBookRepository.count(); + } + + @Override + public Author findAuthorByBookName(String name) { + SchoolBook[] books = schoolBookBookRepository.findByName(name); + return authorService.findByFullName(books[0].getAuthorName(),books[0].getAuthorLastName()); + } +}