diff --git a/pom.xml b/pom.xml index 09ce2f3d..37b5b85f 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,7 @@ spring-core 5.1.5.RELEASE + 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..630866f8 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 @@ -20,4 +20,74 @@ */ 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 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); + } + + @Override + public int hashCode() { + return Objects.hash(name, lastName); + } + + @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..e587169d 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 @@ -17,4 +17,56 @@ */ 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 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 o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Book book = (Book) o; + + 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..fb67fb6e 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 @@ -21,4 +21,66 @@ */ 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..bd747249 --- /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; +import org.apache.commons.lang3.ArrayUtils; + +import java.util.Arrays; + +public class SimpleAuthorRepository implements AuthorRepository { + + private Author[] authors = new Author[0]; + + @Override + public boolean save(Author author) { + if (findByFullName(author.getName(), author.getLastName()) != null) { + return false; + } else { + if (authors == null) { + authors = new Author[1]; + authors[0] = author; + } else { + Author[] arrayCopy = Arrays.copyOf(authors, authors.length + 1); + arrayCopy[arrayCopy.length - 1] = author; + authors = arrayCopy; + } + return true; + } + + } + + @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) { + return false; + } else { + if (authors.length == 1) { + authors = new Author[0]; + return true; + } + for (int i = 0; i < authors.length - 1; i++) { + if (findByFullName(author.getName(), author.getLastName()).equals(authors[i])) { + Author[] arrayCopy = new Author[authors.length - 1]; + System.arraycopy(authors, 0, arrayCopy, 0, i); + System.arraycopy(authors, i + 1, arrayCopy, i, authors.length - i - 1); + authors = arrayCopy; + } + } + return true; + } + } + + @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..69cde5d0 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java @@ -0,0 +1,65 @@ +package com.epam.izh.rd.online.repository; + +import com.epam.izh.rd.online.entity.SchoolBook; + +import java.util.Arrays; + +public class SimpleSchoolBookRepository implements BookRepository{ + + private SchoolBook[] schoolBooks = new SchoolBook[0]; + + @Override + public boolean save(SchoolBook book) { + if (schoolBooks == null) { + schoolBooks = new SchoolBook[1]; + schoolBooks[0] = book; + } else { + SchoolBook[] arrayCopy = Arrays.copyOf(schoolBooks, schoolBooks.length + 1); + arrayCopy[arrayCopy.length - 1] = book; + schoolBooks = arrayCopy; + } + return true; + } + + @Override + public SchoolBook[] findByName(String name) { + SchoolBook[] books = new SchoolBook[0]; + for (SchoolBook book : schoolBooks) { + if (book.getName().equals(name)) { + SchoolBook[] arrayCopy = Arrays.copyOf(books, books.length + 1); + arrayCopy[arrayCopy.length - 1] = book; + books = arrayCopy; + } + } + return books; + } + + @Override + public boolean removeByName(String name) { + if (findByName(name).length == 0) { + return false; + } else { + SchoolBook[] newArr = new SchoolBook[schoolBooks.length - findByName(name).length]; + if (newArr.length == 0) { + schoolBooks = new SchoolBook[0]; + return true; + } + int n = 0; + for (int i = 0; i < schoolBooks.length; i++) { + if (schoolBooks[i].getName().equals(findByName(name)[0])) { + n++; + continue; + } else { + newArr[i - n] = schoolBooks[i]; + } + } + schoolBooks = newArr; + } + 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..4b390604 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java @@ -0,0 +1,37 @@ +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.repository.SimpleAuthorRepository; + +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..0b23a336 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java @@ -0,0 +1,61 @@ +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 { + + 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) { + if (schoolBookBookRepository.findByName(name).length == 0) { + return null; + } else { + String authorName = schoolBookBookRepository.findByName(name)[0].getAuthorName(); + String authorLastName = schoolBookBookRepository.findByName(name)[0].getAuthorLastName(); + return authorService.findByFullName(authorName, authorLastName); + } + } +}