From 8d68b190bf8dcaaf63e35addf93eab23c903c367 Mon Sep 17 00:00:00 2001 From: EvVlF Date: Tue, 22 Mar 2022 20:31:29 +0400 Subject: [PATCH 1/8] Author.java class edited --- .../com/epam/izh/rd/online/entity/Author.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) 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..04f7a31f 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,70 @@ * 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 obj) { + return super.equals(obj); + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + @Override + public String toString() { + return "Author{" + + "name='" + name + '\'' + + ", lastName='" + lastName + '\'' + + ", birthdate=" + birthdate + + ", country='" + country + '\'' + + '}'; + } } From bac1a4b6ec99b665474a3683e0ac31b112cdaf41 Mon Sep 17 00:00:00 2001 From: EvVlF Date: Tue, 22 Mar 2022 20:37:36 +0400 Subject: [PATCH 2/8] Book.java class edited --- .../com/epam/izh/rd/online/entity/Book.java | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) 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..0ca45319 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 @@ -15,6 +15,49 @@ * 5) Переопределить методы equals и hashCode - используйте генерацию (не забывайте alt+inset) * 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset) */ -public abstract class Book { +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 obj) { + return super.equals(obj); + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + @Override + public String toString() { + return "Book{" + + "numberOfPages=" + numberOfPages + + ", name='" + name + '\'' + + '}'; + } } From 4ce4d23154f6dbecb872a651cad5da7c7299d462 Mon Sep 17 00:00:00 2001 From: EvVlF Date: Tue, 22 Mar 2022 21:13:49 +0400 Subject: [PATCH 3/8] SchoolBook.java class edited --- .../epam/izh/rd/online/entity/SchoolBook.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) 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..7ecf84bb 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,60 @@ * 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 obj) { + return super.equals(obj); + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + @Override + public String toString() { + return "SchoolBook{" + + "authorName='" + authorName + '\'' + + ", authorLastName='" + authorLastName + '\'' + + ", publishDate=" + publishDate + + '}'; + } } From 549b60f648397fb9725b6ad163285a1be04afa41 Mon Sep 17 00:00:00 2001 From: EvVlF Date: Mon, 28 Mar 2022 22:31:22 +0400 Subject: [PATCH 4/8] repository/SimpleAuthorService.java class added & edited --- .../repository/SimpleAuthorService.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorService.java diff --git a/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorService.java b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorService.java new file mode 100644 index 00000000..50fe00eb --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorService.java @@ -0,0 +1,64 @@ +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 SimpleAuthorService implements AuthorRepository { + + private Author[] authors = new Author[0]; + + @Override + public boolean save(Author author) { + boolean isAuthor = findByFullName(author.getName(), author.getLastName()) == null; + if (isAuthor) { + Author[] tempAuthors = new Author[authors.length + 1]; + System.arraycopy(authors, 0, tempAuthors, 0, authors.length); + tempAuthors[tempAuthors.length - 1] = author; + authors = tempAuthors; + return true; + } + return false; + } + + @Override + public Author findByFullName(String name, String lastname) { + boolean isName = false; + boolean isLastName = false; + for (Author authorForEach : authors) { + isName = Arrays.stream(authors).anyMatch(name::equals); + isLastName = Arrays.stream(authors).anyMatch(lastname::equals); + if (isName && isLastName) { + return authorForEach; + } + } + return null; + } + + @Override + public boolean remove(Author author) { + boolean isName = false; + boolean isLastName = false; + boolean isAuthor = Objects.nonNull(findByFullName(author.getName(), author.getLastName())); + if (isAuthor) { + for (Author authorForEach : authors) { + isName = Arrays.stream(authors).anyMatch(author.getName()::equals); + isLastName = Arrays.stream(authors).anyMatch(author.getLastName()::equals); + if (isName && isLastName) { + authorForEach = null; + Author[] tempAuthors = Arrays.stream(authors).filter(Objects::nonNull).toArray(Author[]::new); + authors = tempAuthors; + return false; + } + } + } + + return false; + } + + @Override + public int count() { + return authors.length; + } +} From d4ed242ea114fc6783edac6898add96bc6bffb67 Mon Sep 17 00:00:00 2001 From: EvVlF Date: Mon, 28 Mar 2022 22:32:48 +0400 Subject: [PATCH 5/8] service/SimpleAuthorService.java class added & edited --- .../online/service/SimpleAuthorService.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java 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..c0c8cd1a --- /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 { + + private AuthorRepository authorRepository; + + public SimpleAuthorService() { + } + + public SimpleAuthorService(AuthorRepository authorRepository) { + this.authorRepository = authorRepository; + } + + @Override + public boolean save(Author author) { + return false; + } + + @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(); + } +} From 9aa015238a1c05efa40330116835b58dd458a914 Mon Sep 17 00:00:00 2001 From: EvVlF Date: Tue, 5 Apr 2022 20:23:51 +0400 Subject: [PATCH 6/8] close to finish, but equals in SchoolBooks not work --- .../com/epam/izh/rd/online/entity/Author.java | 15 ++++- .../com/epam/izh/rd/online/entity/Book.java | 2 - .../epam/izh/rd/online/entity/SchoolBook.java | 20 +++++-- ...rvice.java => SimpleAuthorRepository.java} | 27 ++++----- .../SimpleSchoolBookRepository.java | 57 +++++++++++++++++++ .../online/service/SimpleAuthorService.java | 2 +- .../service/SimpleSchoolBookService.java | 57 +++++++++++++++++++ 7 files changed, 154 insertions(+), 26 deletions(-) rename src/main/java/com/epam/izh/rd/online/repository/{SimpleAuthorService.java => SimpleAuthorRepository.java} (56%) 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/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 04f7a31f..a9faf3b5 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 и приватным модификатором доступа @@ -68,7 +68,18 @@ public void setCountry(String country) { @Override public boolean equals(Object obj) { - return super.equals(obj); + if (obj == this) { + return true; + } else if ((obj == null || getClass() != obj.getClass())) { + return false; + } + Author other = (Author) obj; + boolean isName = this.name == null && other.name == null || this.name != null && this.name.equals(other.name); + boolean isLastname = this.lastName == null && other.lastName == null || this.lastName != null && this.lastName.equals(other.lastName);; + boolean isBirthDate = this.birthdate == null && other.birthdate == null || this.birthdate != null && this.birthdate.equals(other.birthdate);; + boolean isCountry = this.country == null && other.country == null || this.country != null && this.country.equals(other.country);; + boolean isEqual = (isName && isLastname && isBirthDate && isCountry); + return isEqual; } @Override 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 0ca45319..6b4d9fa5 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 @@ -1,7 +1,5 @@ package com.epam.izh.rd.online.entity; -import java.util.Objects; - /** * Базовая сущность для книги. Содержит базовые поля. * 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 7ecf84bb..9a013f26 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 @@ -1,11 +1,10 @@ package com.epam.izh.rd.online.entity; import java.time.LocalDate; -import java.util.Objects; /** * Сущность учебника. Он должен быть унаследован от сущности Book - * + *

* Необходимо: * 1) Унаследовать данный класс от класса Book * 2) Создать список полей с указанными типами ровно в этом порядке: @@ -19,9 +18,11 @@ * 5) Переопределить методы equals и hashCode - используйте генерацию (не забывайте alt+inset) * 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset) */ + + public class SchoolBook extends Book { private String authorName; - private String authorLastName; + private String authorLastName; private LocalDate publishDate; public SchoolBook() { @@ -60,7 +61,18 @@ public void setPublishDate(LocalDate publishDate) { @Override public boolean equals(Object obj) { - return super.equals(obj); + if (obj == this) { + return true; + } else if ((obj == null || getClass() != obj.getClass()) || !super.equals(obj)) { + return false; + } + SchoolBook other = (SchoolBook) obj; + boolean isName = this.getName() == null && other.getName() == null || this.getName() != null && this.getName().equals(other.getName()); + boolean isAuthorName = this.authorName == null && other.authorName == null || this.authorName != null && this.authorName.equals(other.authorName); + boolean isAuthorLastName = this.authorLastName == null && other.authorLastName == null || this.authorLastName != null && this.authorLastName.equals(other.authorLastName); + boolean isPublishDate = this.publishDate == null && other.publishDate == null || this.publishDate != null && this.publishDate.equals(other.publishDate); + boolean isEqual = (isName && isAuthorName && isAuthorLastName && isPublishDate); + return this.getNumberOfPages() == other.getNumberOfPages() && isEqual; } @Override diff --git a/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorService.java b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java similarity index 56% rename from src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorService.java rename to src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java index 50fe00eb..084f9d12 100644 --- a/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorService.java +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java @@ -5,7 +5,7 @@ import java.util.Arrays; import java.util.Objects; -public class SimpleAuthorService implements AuthorRepository { +public class SimpleAuthorRepository implements AuthorRepository { private Author[] authors = new Author[0]; @@ -27,8 +27,9 @@ public Author findByFullName(String name, String lastname) { boolean isName = false; boolean isLastName = false; for (Author authorForEach : authors) { - isName = Arrays.stream(authors).anyMatch(name::equals); - isLastName = Arrays.stream(authors).anyMatch(lastname::equals); +// Arrays.stream(authors).forEach(authorForEach.getName()::equals); + isName = name == authorForEach.getName(); + isLastName = lastname == authorForEach.getLastName(); if (isName && isLastName) { return authorForEach; } @@ -38,22 +39,14 @@ public Author findByFullName(String name, String lastname) { @Override public boolean remove(Author author) { - boolean isName = false; - boolean isLastName = false; - boolean isAuthor = Objects.nonNull(findByFullName(author.getName(), author.getLastName())); + boolean isAuthor = author.equals(findByFullName(author.getName(), author.getLastName())); if (isAuthor) { - for (Author authorForEach : authors) { - isName = Arrays.stream(authors).anyMatch(author.getName()::equals); - isLastName = Arrays.stream(authors).anyMatch(author.getLastName()::equals); - if (isName && isLastName) { - authorForEach = null; - Author[] tempAuthors = Arrays.stream(authors).filter(Objects::nonNull).toArray(Author[]::new); - authors = tempAuthors; - return false; - } - } + Author[] tempAuthors = Arrays.stream(authors) + .filter(authorDel -> !author.equals(authorDel)) + .toArray(Author[]::new); + authors = tempAuthors; + return true; } - return false; } 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..15eb1394 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java @@ -0,0 +1,57 @@ +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) { + SchoolBook[] tempSchoolBooks = new SchoolBook[schoolBooks.length + 1]; + System.arraycopy(schoolBooks, 0, tempSchoolBooks, 0, schoolBooks.length); + tempSchoolBooks[tempSchoolBooks.length - 1] = book; + schoolBooks = tempSchoolBooks; + return true; + } + + @Override + public SchoolBook[] findByName(String name) { + return Arrays.stream(schoolBooks) + .filter(nameField -> name.equals(nameField.getName())) + .toArray(SchoolBook[]::new); + } + + /** + * Метод должен удалять книги из массива schoolBooks по названию. + * Если книг с одинаковым названием в массиве несколько, метод должен удалить их все. + *

+ * Важно: при удалении книги из массива размер массива должен уменьшиться! + * То есть, если мы сохранили 2 разные книги и вызвали count() (метод ниже), то он должен вернуть 2. + * Если после этого мы удалили 1 книгу, метод count() должен вернуть 1. + *

+ * Если хотя бы одна книга была найдена и удалена, метод должен вернуть true, в противном случае, + * если книга не была найдена, метод должен вернуть false. + */ + + @Override + public boolean removeByName(String name) { + boolean isContain = Arrays.stream(schoolBooks) + .anyMatch(nameField -> name.equals(nameField.getName())); + if (isContain) { + SchoolBook[] temp = Arrays.stream(schoolBooks) + .filter(nameField -> !name.equals(nameField.getName())) + .toArray(SchoolBook[]::new); + schoolBooks = temp; + return true; + } + return isContain; + } + + @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 index c0c8cd1a..b29370d7 100644 --- a/src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java @@ -16,7 +16,7 @@ public SimpleAuthorService(AuthorRepository authorRepository) { @Override public boolean save(Author author) { - return false; + return authorRepository.save(author); } @Override 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..4611e1e6 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java @@ -0,0 +1,57 @@ +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) { + schoolBookBookRepository.save(book); + return true; + } + return false; + } + + @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[] forSearch = schoolBookBookRepository.findByName(name); + if (forSearch.length == 0) { + return null; + } + return authorService.findByFullName(forSearch[0].getAuthorName(), forSearch[0].getAuthorLastName()); + } +} From c8fa48c1b6b2fedb3ad3df1f1a13d8e648e6fdf7 Mon Sep 17 00:00:00 2001 From: EvVlF Date: Tue, 5 Apr 2022 21:37:17 +0400 Subject: [PATCH 7/8] corrected equals and hashCode -_\\\ --- .../com/epam/izh/rd/online/entity/Book.java | 11 +++++++--- .../epam/izh/rd/online/entity/SchoolBook.java | 22 +++++++------------ 2 files changed, 16 insertions(+), 17 deletions(-) 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 6b4d9fa5..8bc615b5 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 @@ -1,5 +1,7 @@ package com.epam.izh.rd.online.entity; +import java.util.Objects; + /** * Базовая сущность для книги. Содержит базовые поля. * @@ -42,13 +44,16 @@ public void setName(String name) { } @Override - public boolean equals(Object obj) { - return super.equals(obj); + 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 && name.equals(book.name); } @Override public int hashCode() { - return super.hashCode(); + return Objects.hash(numberOfPages, name); } @Override 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 9a013f26..dd76e993 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 @@ -1,6 +1,7 @@ package com.epam.izh.rd.online.entity; import java.time.LocalDate; +import java.util.Objects; /** * Сущность учебника. Он должен быть унаследован от сущности Book @@ -60,24 +61,17 @@ public void setPublishDate(LocalDate publishDate) { } @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } else if ((obj == null || getClass() != obj.getClass()) || !super.equals(obj)) { - return false; - } - SchoolBook other = (SchoolBook) obj; - boolean isName = this.getName() == null && other.getName() == null || this.getName() != null && this.getName().equals(other.getName()); - boolean isAuthorName = this.authorName == null && other.authorName == null || this.authorName != null && this.authorName.equals(other.authorName); - boolean isAuthorLastName = this.authorLastName == null && other.authorLastName == null || this.authorLastName != null && this.authorLastName.equals(other.authorLastName); - boolean isPublishDate = this.publishDate == null && other.publishDate == null || this.publishDate != null && this.publishDate.equals(other.publishDate); - boolean isEqual = (isName && isAuthorName && isAuthorLastName && isPublishDate); - return this.getNumberOfPages() == other.getNumberOfPages() && isEqual; + 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 authorName.equals(that.authorName) && authorLastName.equals(that.authorLastName) && publishDate.equals(that.publishDate); } @Override public int hashCode() { - return super.hashCode(); + return Objects.hash(super.hashCode(), authorName, authorLastName, publishDate); } @Override From ed15054f2f428ec8205d607cc2f4771cf573c1a6 Mon Sep 17 00:00:00 2001 From: EvVlF Date: Tue, 5 Apr 2022 22:05:41 +0400 Subject: [PATCH 8/8] corrected equals and hashCode v1.0 --- .../com/epam/izh/rd/online/entity/Author.java | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) 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 a9faf3b5..da47f237 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 @@ -67,24 +67,16 @@ public void setCountry(String country) { } @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } else if ((obj == null || getClass() != obj.getClass())) { - return false; - } - Author other = (Author) obj; - boolean isName = this.name == null && other.name == null || this.name != null && this.name.equals(other.name); - boolean isLastname = this.lastName == null && other.lastName == null || this.lastName != null && this.lastName.equals(other.lastName);; - boolean isBirthDate = this.birthdate == null && other.birthdate == null || this.birthdate != null && this.birthdate.equals(other.birthdate);; - boolean isCountry = this.country == null && other.country == null || this.country != null && this.country.equals(other.country);; - boolean isEqual = (isName && isLastname && isBirthDate && isCountry); - return isEqual; + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Author author = (Author) o; + return name.equals(author.name) && lastName.equals(author.lastName) && birthdate.equals(author.birthdate) && country.equals(author.country); } @Override public int hashCode() { - return super.hashCode(); + return Objects.hash(name, lastName, birthdate, country); } @Override