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..66423968 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,72 @@ * 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 this.name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return this.lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public LocalDate getBirthdate() { + return this.birthdate; + } + + public void setBirthdate(LocalDate birthdate) { + this.birthdate = birthdate; + } + + public String getCountry() { + return this.country; + } + + public void setCountry(String country) { + this.country = country; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || this.getClass() != o.getClass()) return false; + Author author = (Author) o; + return Objects.equals(this.getName(), author.getName()) && + Objects.equals(this.getLastName(), author.getLastName()) && + Objects.equals(this.getBirthdate(), author.getBirthdate()) && + Objects.equals(this.getCountry(), author.getCountry()); + } + + @Override + public int hashCode() { + return Objects.hash(this.getName(), this.getLastName(), this.getBirthdate(), this.getCountry()); + } + + @Override + public String toString() { + return String.format("Author{name= %s, lastName= %s, birthdate= %s, country= %s}", + this.getName(), this.getLastName(), this.getBirthdate(), this.getCountry()); + } } 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..5fa3fa3a 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,48 @@ * 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 this.numberOfPages; + } + + public void setNumberOfPages(int numberOfPages) { + this.numberOfPages = numberOfPages; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || this.getClass() != o.getClass()) return false; + Book book = (Book) o; + return this.getNumberOfPages() == book.getNumberOfPages() && Objects.equals(this.getName(), book.getName()); + } + + @Override + public int hashCode() { + return Objects.hash(this.getNumberOfPages(), this.getName()); + } + + @Override + public String toString() { + return String.format("Book{numberOfPages= %s,name= %s}", this.getNumberOfPages(), this.getName()); + } } 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..5380c70b 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 @@ -5,7 +5,7 @@ /** * Сущность учебника. Он должен быть унаследован от сущности Book - * + *

* Необходимо: * 1) Унаследовать данный класс от класса Book * 2) Создать список полей с указанными типами ровно в этом порядке: @@ -20,5 +20,69 @@ * 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 this.authorName; + } + + public void setAuthorName(String authorName) { + this.authorName = authorName; + } + + public String getAuthorLastName() { + return this.authorLastName; + } + + public void setAuthorLastName(String authorLastName) { + this.authorLastName = authorLastName; + } + + public LocalDate getPublishDate() { + return this.publishDate; + } + + public void setPublishDate(LocalDate publishDate) { + this.publishDate = publishDate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || this.getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + SchoolBook that = (SchoolBook) o; + return Objects.equals(this.getAuthorName(), that.getAuthorName()) && + Objects.equals(this.getAuthorLastName(), that.getAuthorLastName()) && + Objects.equals(this.getPublishDate(), that.getPublishDate()); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), this.getAuthorName(), this.getAuthorLastName(), this.getPublishDate()); + } + + @Override + public String toString() { + return String.format("SchoolBook{authorName= %s, authorLastName= %s, publishDate= %s}", + this.getAuthorName(), this.getAuthorLastName(), this.getPublishDate()); + } } 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..73e9b746 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java @@ -0,0 +1,65 @@ +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[0]; + + @Override + public boolean save(Author author) { + int numOfAuthors = this.authors.length; + if (numOfAuthors > 0) { + if (findByFullNameGetIndex(author.getName(), author.getLastName()) >= 0) { + return false; + } + } + Author[] updateAuthors = new Author[numOfAuthors + 1]; + System.arraycopy(this.authors, 0, updateAuthors, 0, numOfAuthors); + updateAuthors[numOfAuthors] = author; + this.authors = updateAuthors; + return true; + } + + @Override + public Author findByFullName(String name, String lastname) { + int i = findByFullNameGetIndex(name, lastname); + if (i >= 0) { + return this.authors[i]; + } + return null; + } + + private int findByFullNameGetIndex(String name, String lastname) { + for (int i = 0; i < this.authors.length; i++) { + if (this.authors[i].getName().equals(name) && this.authors[i].getLastName().equals(lastname)) { + return i; + } + } + return -1; + } + + @Override + public boolean remove(Author author) { + int numOfAuthors = this.authors.length; + if (numOfAuthors > 0) { + int j0 = findByFullNameGetIndex(author.getName(), author.getLastName()); + if (j0 >= 0) { + Author[] updateAuthors = new Author[numOfAuthors - 1]; + for (int i = 0, j = 0; i < numOfAuthors; i++) { + if (i != j0) { + updateAuthors[j] = this.authors[i]; + j++; + } + } + this.authors = updateAuthors; + return true; + } + } + return false; + } + + @Override + public int count() { + return this.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..eb105f93 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java @@ -0,0 +1,82 @@ +package com.epam.izh.rd.online.repository; + +import com.epam.izh.rd.online.entity.SchoolBook; + +public class SimpleSchoolBookRepository implements BookRepository { + private SchoolBook[] schoolBooks = new SchoolBook[0]; + + @Override + public boolean save(SchoolBook book) { + int numOfBooks = this.schoolBooks.length; + SchoolBook[] updateBooks = new SchoolBook[numOfBooks + 1]; + System.arraycopy(this.schoolBooks, 0, updateBooks, 0, numOfBooks); + updateBooks[numOfBooks] = book; + this.schoolBooks = updateBooks; + return true; + } + + @Override + public SchoolBook[] findByName(String name) { + int[] indexes = findByNameGetIndexes(name); + int indexesLen = indexes.length; + if (indexesLen > 0) { + SchoolBook[] arrayOfBooks = new SchoolBook[indexesLen]; + for (int i = 0; i < indexesLen; i++) { + arrayOfBooks[i] = this.schoolBooks[indexes[i]]; + } + return arrayOfBooks; + } + return new SchoolBook[0]; + } + + private int[] findByNameGetIndexes(String name) { + int numOfBooks = this.schoolBooks.length; + int[] indexes = new int[numOfBooks]; + int j = 0; + for (int i = 0; i < numOfBooks; i++) { + if (this.schoolBooks[i].getName().equals(name)) { + indexes[j] = i; + j++; + } + } + if (j > 0) { + int[] indexes2 = new int[j]; + System.arraycopy(indexes, 0, indexes2, 0, j); + return indexes2; + } + return new int[0]; + } + + @Override + public boolean removeByName(String name) { + int numOfBooks = this.schoolBooks.length; + if (numOfBooks > 0) { + int[] indexes = findByNameGetIndexes(name); + int indexesLen = indexes.length; + if (indexesLen > 0) { + SchoolBook[] arrayOfBooks = new SchoolBook[numOfBooks - indexesLen]; + boolean flag; + for (int i = 0, j = 0, k = 0; i < numOfBooks; i++) { + flag = true; + if (k < indexesLen) + if (i == indexes[k]) { + k++; + flag = false; + } + if (flag) { + arrayOfBooks[j] = this.schoolBooks[i]; + j++; + } + } + this.schoolBooks = arrayOfBooks; + return true; + } + } + return false; + } + + @Override + public int count() { + return this.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..64b869f4 --- /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 this.authorRepository.save(author); + } + + @Override + public Author findByFullName(String name, String lastname) { + return this.authorRepository.findByFullName(name, lastname); + } + + @Override + public boolean remove(Author author) { + return this.authorRepository.remove(author); + } + + @Override + public int count() { + return this.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..fe455cd4 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java @@ -0,0 +1,58 @@ +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) { + SchoolBook bookLikeSB = ((SchoolBook) book); + if (this.authorService.findByFullName(bookLikeSB.getAuthorName(), bookLikeSB.getAuthorLastName()) != null) { + this.schoolBookBookRepository.save(bookLikeSB); + return true; + } + return false; + } + + @Override + public Book[] findByName(String name) { + return this.schoolBookBookRepository.findByName(name); + } + + @Override + public int getNumberOfBooksByName(String name) { + return this.schoolBookBookRepository.findByName(name).length; + } + + @Override + public boolean removeByName(String name) { + return this.schoolBookBookRepository.removeByName(name); + } + + @Override + public int count() { + return this.schoolBookBookRepository.count(); + } + + @Override + public Author findAuthorByBookName(String name) { + SchoolBook[] books = this.schoolBookBookRepository.findByName(name); + if (books.length > 0) + return this.authorService.findByFullName(books[0].getAuthorName(), books[0].getAuthorLastName()); + return null; + } + +}