From 0e88ffa52fd912522aceafd62273b2ffd649dd20 Mon Sep 17 00:00:00 2001 From: Pavel Ogorodnikov Date: Tue, 26 Nov 2019 21:21:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/epam/izh/rd/online/entity/Author.java | 73 +++++++++++++++++++ .../com/epam/izh/rd/online/entity/Book.java | 47 ++++++++++++ .../epam/izh/rd/online/entity/SchoolBook.java | 61 ++++++++++++++++ .../repository/SimpleAuthorRepository.java | 62 ++++++++++++++++ .../SimpleSchoolBookRepository.java | 65 +++++++++++++++++ .../izh/rd/online/service/BookService.java | 1 + .../online/service/SimpleAuthorService.java | 36 +++++++++ .../service/SimpleSchoolBookService.java | 63 ++++++++++++++++ 8 files changed, 408 insertions(+) create mode 100644 src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java create mode 100644 src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java create mode 100644 src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java create mode 100644 src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java 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..2deb7fbd 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 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 countty) { + this.country = countty; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Author author = (Author) o; + return Objects.equals(name, author.name) && + Objects.equals(lastName, author.lastName) && + Objects.equals(birthdate, author.birthdate) && + Objects.equals(country, author.country); + } + + @Override + public int hashCode() { + return Objects.hash(name, lastName, birthdate, country); + } + + + + @Override + public String toString() { + return "Author{" + + "name='" + name + '\'' + + ", lostName='" + lastName + '\'' + + ", birthdate=" + birthdate + + ", countty='" + 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..5ec16d02 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 int getNumberOfPages() { + return numberOfPages; + } + + public Book(int numberOfPages, String name) { + this.numberOfPages = numberOfPages; + this.name = name; + } + + 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 numberOfPages == book.numberOfPages && + Objects.equals(name, book.name); + } + + @Override + public int hashCode() { + return Objects.hash(numberOfPages, name); + } + + @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..3ddf6cc7 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,66 @@ * 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset) */ public class SchoolBook extends Book { + private String authorName; + private String authorLastName; + private LocalDate publishDate; + public SchoolBook() { + } + + 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 Objects.equals(authorName, that.authorName) && + Objects.equals(authorLastName, that.authorLastName) && + Objects.equals(publishDate, that.publishDate); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), authorName, authorLastName, publishDate); + } + + @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..6b2fa68c --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java @@ -0,0 +1,62 @@ +package com.epam.izh.rd.online.repository; + +import com.epam.izh.rd.online.entity.Author; +import com.epam.izh.rd.online.repository.AuthorRepository; + +public class SimpleAuthorRepository implements AuthorRepository { + private Author[] authors ={}; + + @Override + public boolean save(Author author) { + if(findByFullName(author.getName(),author.getLastName())==null){ + Author[] a = new Author[this.authors.length + 1]; + for(int i=0; i { + private SchoolBook[] schoolBooks = {}; + @Override + public boolean save(SchoolBook book) { + SchoolBook[] a = new SchoolBook[count() + 1]; + for(int i=0; i { /** 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..d3003b82 --- /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; +import com.epam.izh.rd.online.service.AuthorService; + +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..02a9f9b2 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java @@ -0,0 +1,63 @@ +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; +import com.epam.izh.rd.online.service.AuthorService; +import com.epam.izh.rd.online.service.BookService; + +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(SchoolBook book) { + if(authorService.findByFullName(book.getAuthorName(), book.getAuthorLastName() ) == null ){ + return false; + } else { + schoolBookBookRepository.save(book); + return true; + } + } + + @Override + public SchoolBook[] findByName(String name) { + return schoolBookBookRepository.findByName(name); + } + + @Override + public int getNumberOfBooksByName(String name) { + + return schoolBookBookRepository.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[] sArr = schoolBookBookRepository.findByName(name); + if(authorService.count()==0){ + return null; + } else { + return authorService.findByFullName(sArr[0].getAuthorName(), sArr[0].getAuthorLastName()); + } + } +}