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..d96d8a8b 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 @@ -19,5 +19,78 @@ * 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 String getLastName() { + return lastName; + } + + public LocalDate getBirthdate() { + return birthdate; + } + + public String getCountry() { + return country; + } + + public void setName(String name) { + this.name = name; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public void setBirthdate(LocalDate birthdate) { + this.birthdate = birthdate; + } + + public void setCountry(String country) { + this.country = country; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Author author = (Author) o; + return getName().equals(author.getName()) && + getLastName().equals(author.getLastName()) && + getBirthdate().equals(author.getBirthdate()) && + getCountry().equals(author.getCountry()); + } + + @Override + public int hashCode() { + + return Objects.hash(getName(), getLastName(), getBirthdate(), getCountry()); + } + + @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..a0fe6849 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 @@ -16,5 +16,52 @@ * 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset) */ public abstract class Book { + private int numberOfPages ; + private String name ; + public Book() { + } + + public Book(int numberOfPages, String name) { + this.numberOfPages = numberOfPages; + this.name = name; + } + + public int getNumberOfPages() { + return numberOfPages; + } + + public String getName() { + return name; + } + + public void setNumberOfPages(int numberOfPages) { + this.numberOfPages = numberOfPages; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Book book = (Book) o; + return getNumberOfPages() == book.getNumberOfPages() && + getName().equals(book.getName()); + } + + @Override + public int hashCode() { + return Objects.hash(getNumberOfPages(), getName()); + } + + @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..9a87fda2 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,72 @@ * 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 o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + SchoolBook that = (SchoolBook) o; + return getAuthorName().equals(that.getAuthorName()) && + getAuthorLastName().equals(that.getAuthorLastName()) && + getPublishDate().equals(that.getPublishDate()); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), getAuthorName(), getAuthorLastName(), getPublishDate()); + } + + @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..b0fbaae8 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java @@ -0,0 +1,50 @@ +package com.epam.izh.rd.online.repository; + +import com.epam.izh.rd.online.entity.Author; + +public class SimpleAuthorRepository implements AuthorRepository { + + private Author[] authors = new Author[]{} ; + + @Override + public boolean save(Author author) { + int saveCount = count() ; + if (findByFullName(author.getName(), author.getLastName()) == null){ + Author[] saveAuthor = new Author[saveCount+1] ; + saveAuthor[saveCount] = author ; + authors = saveAuthor ; + 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){ + Author[] removeAuthors = new Author[authors.length - 1] ; + for (int i = 0 , j = 0 ; j { + + SchoolBook[] schoolBooks = new SchoolBook[]{}; + + @Override + public boolean save(SchoolBook book) { + int saveCount = count() ; + SchoolBook [] saveBook = new SchoolBook[saveCount + 1] ; + System.arraycopy(schoolBooks, 0 ,saveBook, 0 ,saveCount); + saveBook[saveCount] = book ; + schoolBooks = saveBook ; + + return true; + } + + @Override + public SchoolBook[] findByName(String name) { + SchoolBook[] books = new SchoolBook[]{}; + int bookNum = 0 ; + for (SchoolBook book: schoolBooks ) { + if(book.getName().equals(name)){ + SchoolBook[]tempBook = new SchoolBook[bookNum +1] ; + System.arraycopy(books, 0, tempBook, 0 ,bookNum); + tempBook[bookNum] = book ; + books = tempBook ; + bookNum++ ; + } + } + return books; + } + + @Override + public boolean removeByName(String name) { + int removeCount = 0 ; + int num = count(); + for (SchoolBook book : schoolBooks) { + if( book.getName().equals(name)){ + removeCount++ ; + SchoolBook[] removeBook = new SchoolBook[num-removeCount] ; + //System.arraycopy(schoolBooks,0,removeBook,0,removeBook.length); + schoolBooks = removeBook ; + } + + + } + return removeCount!=0;//return removeCount!=cont() тоже отработало, почему? + } + + @Override + public int count() { + return schoolBooks.length; + } +} \ No newline at end of file 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..edb13b90 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java @@ -0,0 +1,36 @@ +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 { + + AuthorRepository authorRepository ; + + public SimpleAuthorService(AuthorRepository authorRepository) { + this.authorRepository = authorRepository ; + } + + public SimpleAuthorService() { + } + + @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..ce522fdb --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java @@ -0,0 +1,55 @@ +package com.epam.izh.rd.online.service; + +import com.epam.izh.rd.online.entity.Author; +import com.epam.izh.rd.online.entity.SchoolBook; +import com.epam.izh.rd.online.repository.BookRepository; + +public class SimpleSchoolBookService implements BookService { + + BookRepository schoolBookBookRepository ; + AuthorService authorService ; + + public SimpleSchoolBookService() { + } + + public SimpleSchoolBookService(BookRepository schoolBookBookRepository, AuthorService authorService) { + this.schoolBookBookRepository = schoolBookBookRepository; + this.authorService = authorService; + } + + @Override + public boolean save(SchoolBook book) { + if (authorService.findByFullName(book.getAuthorName(), book.getAuthorLastName()) != null){ + schoolBookBookRepository.save(book) ; + return true ; + } + return false; + } + + @Override + public SchoolBook[] 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) { + return authorService.findByFullName(schoolBookBookRepository.findByName(name)[0].getAuthorName(), + schoolBookBookRepository.findByName(name)[0].getAuthorLastName()) ; + + } +} \ No newline at end of file