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..4fe0cac4 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,73 @@ * 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 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 + '\'' + + ", 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..3990ce7f 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,51 @@ * 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 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; + 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..9b8a8f2b 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,64 @@ * 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/AuthorRepository.java b/src/main/java/com/epam/izh/rd/online/repository/AuthorRepository.java index c62bd4e9..0a03f405 100644 --- a/src/main/java/com/epam/izh/rd/online/repository/AuthorRepository.java +++ b/src/main/java/com/epam/izh/rd/online/repository/AuthorRepository.java @@ -2,6 +2,9 @@ import com.epam.izh.rd.online.entity.Author; +import java.util.Arrays; +import java.util.Objects; + /** * Интерфейс репозитория для хранения данных об авторах. *
diff --git a/src/main/java/com/epam/izh/rd/online/repository/BookRepository.java b/src/main/java/com/epam/izh/rd/online/repository/BookRepository.java
index 7358e99b..ef351769 100644
--- a/src/main/java/com/epam/izh/rd/online/repository/BookRepository.java
+++ b/src/main/java/com/epam/izh/rd/online/repository/BookRepository.java
@@ -1,6 +1,7 @@
package com.epam.izh.rd.online.repository;
import com.epam.izh.rd.online.entity.Book;
+import com.epam.izh.rd.online.entity.SchoolBook;
/**
* Интерфейс репозитория для хранения данных о книгах
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..a7aeecaa
--- /dev/null
+++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java
@@ -0,0 +1,63 @@
+package com.epam.izh.rd.online.repository;
+
+import com.epam.izh.rd.online.entity.Author;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+public class SimpleAuthorRepository implements AuthorRepository {
+ private static Author[] authors = new Author[0];
+
+ public SimpleAuthorRepository() {
+ }
+
+ public static Author[] getAuthors() {
+ return authors;
+ }
+
+ @Override
+ public boolean save(Author author) {
+ if (findByFullName(author.getName(), author.getLastName()) == null) {
+ Author[] temp = Arrays.copyOf(authors, authors.length + 1);
+ temp[temp.length - 1] = author;
+ authors = Arrays.copyOf(temp, temp.length);
+ return true;
+ }
+
+ return false;
+ }
+
+ @Override
+ public Author findByFullName(String name, String lastname) {
+ for (Author author : authors) {
+ if (Objects.equals(author.getName(), name) && Objects.equals(author.getLastName(), lastname)) return author;
+ }
+ return null;
+ }
+
+ @Override
+ public boolean remove(Author author) {
+ int numOfAuthor = -1;
+ for (int i = 0; i < authors.length; i++) {
+ if (Objects.equals(author.getName(), authors[i].getName()) && Objects.equals(author.getLastName(), authors[i].getLastName())) numOfAuthor = i;
+ }
+
+ if (numOfAuthor != -1) {
+ for(int i = numOfAuthor; i < authors.length - 1; i++) {
+ Author temp = authors[i];
+ authors[i] = authors[i + 1];
+ authors[i + 1] = temp;
+ }
+ authors = Arrays.copyOf(authors, authors.length - 1);
+ 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..f9751697
--- /dev/null
+++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java
@@ -0,0 +1,69 @@
+package com.epam.izh.rd.online.repository;
+
+import com.epam.izh.rd.online.entity.Author;
+import com.epam.izh.rd.online.entity.SchoolBook;
+
+import java.util.Arrays;
+import java.util.Objects;
+
+public class SimpleSchoolBookRepository implements BookRepository