From dc8523c847f1c4858ce8289bdbb6b2a044a38bc4 Mon Sep 17 00:00:00 2001
From: Rustam Ramazanov <1234>
Date: Tue, 14 Sep 2021 10:35:42 +0300
Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?=
=?UTF-8?q?=20=D0=B2=D1=81=D1=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/epam/izh/rd/online/entity/Author.java | 61 +++++++++++++-
.../com/epam/izh/rd/online/entity/Book.java | 49 ++++++++++-
.../epam/izh/rd/online/entity/SchoolBook.java | 68 ++++++++++++++-
.../repository/SimpleAuthorRepository.java | 65 +++++++++++++++
.../SimpleSchoolBookRepository.java | 82 +++++++++++++++++++
.../online/service/SimpleAuthorService.java | 38 +++++++++
.../service/SimpleSchoolBookService.java | 59 +++++++++++++
7 files changed, 419 insertions(+), 3 deletions(-)
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..56b7ab5d 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,64 @@
* 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset)
*/
public class Author {
+ private String name;
+ private String lastName;
+ private LocalDate birthdate;
+ private String country;
+ public Author() {
+ super();
+ }
+
+ 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 name.equals(author.name) && Objects.equals(lastName, author.lastName) && birthdate.equals(author.birthdate) && country.equals(author.country);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name, lastName, birthdate, 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..e99169ef 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,52 @@
* 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset)
*/
public abstract class Book {
+ private int numberOfPages;
+ private String name;
+ public Book() {
+ super();
+ }
+
+ 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 && name.equals(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..8723642b 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,71 @@
* 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset)
*/
public class SchoolBook extends Book {
+ private String authorName;
+ private String authorLastName;
+ private LocalDate publishDate;
+ public SchoolBook() {
+ super();
+ }
+
+ 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 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 authorName.equals(that.authorName) && Objects.equals(authorLastName, that.authorLastName) && publishDate.equals(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..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..90a2e3a2
--- /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];
+ private int numOfBooks = 0;
+
+ @Override
+ public boolean save(SchoolBook book) {
+ SchoolBook[] updateBooks = new SchoolBook[this.numOfBooks + 1];
+ System.arraycopy(this.schoolBooks, 0, updateBooks, 0, this.numOfBooks);
+ updateBooks[this.numOfBooks] = book;
+ this.numOfBooks = this.numOfBooks + 1;
+ 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[] indexes = new int[this.numOfBooks];
+ int j = 0;
+ for (int i = 0; i < this.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) {
+ if (this.numOfBooks > 0) {
+ int[] indexes = findByNameGetIndexes(name);
+ int indexesLen = indexes.length;
+ if (indexesLen > 0) {
+ SchoolBook[] arrayOfBooks = new SchoolBook[this.numOfBooks - indexesLen];
+ boolean flag;
+ for (int i = 0, j = 0, k = 0; i < this.numOfBooks; i++) {
+ flag = true;
+ if (k < indexesLen)
+ if (i == indexes[k]) {
+ k++;
+ flag = false;
+ }
+ if (flag) {
+ arrayOfBooks[j] = this.schoolBooks[i];
+ j++;
+ }
+ }
+ this.numOfBooks = this.numOfBooks - indexesLen;
+ this.schoolBooks = arrayOfBooks;
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public int count() {
+ return this.numOfBooks;
+ }
+}
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..9b756a37
--- /dev/null
+++ b/src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java
@@ -0,0 +1,38 @@
+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
+{
+ AuthorRepository authorRepository;
+
+ public SimpleAuthorService() {
+ super();
+ }
+
+ 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..cd92493d
--- /dev/null
+++ b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java
@@ -0,0 +1,59 @@
+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() {
+ super();
+ }
+
+ public SimpleSchoolBookService(BookRepository schoolBookBookRepository, AuthorService authorService) {
+ this.schoolBookBookRepository = schoolBookBookRepository;
+ this.authorService = authorService;
+ }
+
+ @Override
+ public boolean save(Book book) {
+ SchoolBook bookLikeSB = ((SchoolBook) book);
+ if (authorService.findByFullName(bookLikeSB.getAuthorName(), bookLikeSB.getAuthorLastName()) != null) {
+ schoolBookBookRepository.save(bookLikeSB);
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public Book[] 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[] books = schoolBookBookRepository.findByName(name);
+ if (books.length > 0)
+ return authorService.findByFullName(books[0].getAuthorName(), books[0].getAuthorLastName());
+ return null;
+ }
+
+}
From f5c00305decdd0dac0172c2509e9355577773857 Mon Sep 17 00:00:00 2001
From: Rustam Ramazanov <1234>
Date: Wed, 15 Sep 2021 11:31:24 +0300
Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?=
=?UTF-8?q?=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/epam/izh/rd/online/entity/Author.java | 24 ++++++++++++-------
.../com/epam/izh/rd/online/entity/Book.java | 16 +++++--------
.../epam/izh/rd/online/entity/SchoolBook.java | 22 ++++++++---------
.../SimpleSchoolBookRepository.java | 24 +++++++++----------
.../online/service/SimpleAuthorService.java | 15 +++++-------
.../service/SimpleSchoolBookService.java | 17 +++++++------
6 files changed, 58 insertions(+), 60 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 56b7ab5d..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
@@ -25,7 +25,6 @@ public class Author {
private String country;
public Author() {
- super();
}
public Author(String name, String lastName, LocalDate birthdate, String country) {
@@ -36,7 +35,7 @@ public Author(String name, String lastName, LocalDate birthdate, String country)
}
public String getName() {
- return name;
+ return this.name;
}
public void setName(String name) {
@@ -44,7 +43,7 @@ public void setName(String name) {
}
public String getLastName() {
- return lastName;
+ return this.lastName;
}
public void setLastName(String lastName) {
@@ -52,7 +51,7 @@ public void setLastName(String lastName) {
}
public LocalDate getBirthdate() {
- return birthdate;
+ return this.birthdate;
}
public void setBirthdate(LocalDate birthdate) {
@@ -60,7 +59,7 @@ public void setBirthdate(LocalDate birthdate) {
}
public String getCountry() {
- return country;
+ return this.country;
}
public void setCountry(String country) {
@@ -70,13 +69,22 @@ public void setCountry(String country) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
+ if (o == null || this.getClass() != o.getClass()) return false;
Author author = (Author) o;
- return name.equals(author.name) && Objects.equals(lastName, author.lastName) && birthdate.equals(author.birthdate) && country.equals(author.country);
+ 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(name, lastName, birthdate, country);
+ 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 e99169ef..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
@@ -20,7 +20,6 @@ public abstract class Book {
private String name;
public Book() {
- super();
}
public Book(int numberOfPages, String name) {
@@ -29,7 +28,7 @@ public Book(int numberOfPages, String name) {
}
public int getNumberOfPages() {
- return numberOfPages;
+ return this.numberOfPages;
}
public void setNumberOfPages(int numberOfPages) {
@@ -37,7 +36,7 @@ public void setNumberOfPages(int numberOfPages) {
}
public String getName() {
- return name;
+ return this.name;
}
public void setName(String name) {
@@ -47,21 +46,18 @@ public void setName(String name) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
+ if (o == null || this.getClass() != o.getClass()) return false;
Book book = (Book) o;
- return numberOfPages == book.numberOfPages && name.equals(book.name);
+ return this.getNumberOfPages() == book.getNumberOfPages() && Objects.equals(this.getName(), book.getName());
}
@Override
public int hashCode() {
- return Objects.hash(numberOfPages, name);
+ return Objects.hash(this.getNumberOfPages(), this.getName());
}
@Override
public String toString() {
- return "Book{" +
- "numberOfPages=" + numberOfPages +
- ", name='" + name + '\'' +
- '}';
+ 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 8723642b..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
@@ -25,7 +25,6 @@ public class SchoolBook extends Book {
private LocalDate publishDate;
public SchoolBook() {
- super();
}
public SchoolBook(String authorName, String authorLastName, LocalDate publishDate) {
@@ -42,7 +41,7 @@ public SchoolBook(int numberOfPages, String name, String authorName, String auth
}
public String getAuthorName() {
- return authorName;
+ return this.authorName;
}
public void setAuthorName(String authorName) {
@@ -50,7 +49,7 @@ public void setAuthorName(String authorName) {
}
public String getAuthorLastName() {
- return authorLastName;
+ return this.authorLastName;
}
public void setAuthorLastName(String authorLastName) {
@@ -58,7 +57,7 @@ public void setAuthorLastName(String authorLastName) {
}
public LocalDate getPublishDate() {
- return publishDate;
+ return this.publishDate;
}
public void setPublishDate(LocalDate publishDate) {
@@ -68,23 +67,22 @@ public void setPublishDate(LocalDate publishDate) {
@Override
public boolean equals(Object o) {
if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
+ if (o == null || this.getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
SchoolBook that = (SchoolBook) o;
- return authorName.equals(that.authorName) && Objects.equals(authorLastName, that.authorLastName) && publishDate.equals(that.publishDate);
+ 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(), authorName, authorLastName, publishDate);
+ return Objects.hash(super.hashCode(), this.getAuthorName(), this.getAuthorLastName(), this.getPublishDate());
}
@Override
public String toString() {
- return "SchoolBook{" +
- "authorName='" + authorName + '\'' +
- ", authorLastName='" + authorLastName + '\'' +
- ", publishDate=" + publishDate +
- '}';
+ 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/SimpleSchoolBookRepository.java b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java
index 90a2e3a2..eb105f93 100644
--- a/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java
+++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java
@@ -4,14 +4,13 @@
public class SimpleSchoolBookRepository implements BookRepository {
private SchoolBook[] schoolBooks = new SchoolBook[0];
- private int numOfBooks = 0;
@Override
public boolean save(SchoolBook book) {
- SchoolBook[] updateBooks = new SchoolBook[this.numOfBooks + 1];
- System.arraycopy(this.schoolBooks, 0, updateBooks, 0, this.numOfBooks);
- updateBooks[this.numOfBooks] = book;
- this.numOfBooks = this.numOfBooks + 1;
+ 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;
}
@@ -31,9 +30,10 @@ public SchoolBook[] findByName(String name) {
}
private int[] findByNameGetIndexes(String name) {
- int[] indexes = new int[this.numOfBooks];
+ int numOfBooks = this.schoolBooks.length;
+ int[] indexes = new int[numOfBooks];
int j = 0;
- for (int i = 0; i < this.numOfBooks; i++) {
+ for (int i = 0; i < numOfBooks; i++) {
if (this.schoolBooks[i].getName().equals(name)) {
indexes[j] = i;
j++;
@@ -49,13 +49,14 @@ private int[] findByNameGetIndexes(String name) {
@Override
public boolean removeByName(String name) {
- if (this.numOfBooks > 0) {
+ int numOfBooks = this.schoolBooks.length;
+ if (numOfBooks > 0) {
int[] indexes = findByNameGetIndexes(name);
int indexesLen = indexes.length;
if (indexesLen > 0) {
- SchoolBook[] arrayOfBooks = new SchoolBook[this.numOfBooks - indexesLen];
+ SchoolBook[] arrayOfBooks = new SchoolBook[numOfBooks - indexesLen];
boolean flag;
- for (int i = 0, j = 0, k = 0; i < this.numOfBooks; i++) {
+ for (int i = 0, j = 0, k = 0; i < numOfBooks; i++) {
flag = true;
if (k < indexesLen)
if (i == indexes[k]) {
@@ -67,7 +68,6 @@ public boolean removeByName(String name) {
j++;
}
}
- this.numOfBooks = this.numOfBooks - indexesLen;
this.schoolBooks = arrayOfBooks;
return true;
}
@@ -77,6 +77,6 @@ public boolean removeByName(String name) {
@Override
public int count() {
- return this.numOfBooks;
+ 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
index 9b756a37..64b869f4 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
@@ -2,14 +2,11 @@
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
-{
- AuthorRepository authorRepository;
+public class SimpleAuthorService implements AuthorService {
+ private AuthorRepository authorRepository;
public SimpleAuthorService() {
- super();
}
public SimpleAuthorService(AuthorRepository authorRepository) {
@@ -18,21 +15,21 @@ public SimpleAuthorService(AuthorRepository authorRepository) {
@Override
public boolean save(Author author) {
- return authorRepository.save(author);
+ return this.authorRepository.save(author);
}
@Override
public Author findByFullName(String name, String lastname) {
- return authorRepository.findByFullName(name,lastname);
+ return this.authorRepository.findByFullName(name, lastname);
}
@Override
public boolean remove(Author author) {
- return authorRepository.remove(author);
+ return this.authorRepository.remove(author);
}
@Override
public int count() {
- return authorRepository.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
index cd92493d..fe455cd4 100644
--- a/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java
+++ b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java
@@ -10,7 +10,6 @@ public class SimpleSchoolBookService implements BookService {
private AuthorService authorService;
public SimpleSchoolBookService() {
- super();
}
public SimpleSchoolBookService(BookRepository schoolBookBookRepository, AuthorService authorService) {
@@ -21,8 +20,8 @@ public SimpleSchoolBookService(BookRepository schoolBookBookReposito
@Override
public boolean save(Book book) {
SchoolBook bookLikeSB = ((SchoolBook) book);
- if (authorService.findByFullName(bookLikeSB.getAuthorName(), bookLikeSB.getAuthorLastName()) != null) {
- schoolBookBookRepository.save(bookLikeSB);
+ if (this.authorService.findByFullName(bookLikeSB.getAuthorName(), bookLikeSB.getAuthorLastName()) != null) {
+ this.schoolBookBookRepository.save(bookLikeSB);
return true;
}
return false;
@@ -30,29 +29,29 @@ public boolean save(Book book) {
@Override
public Book[] findByName(String name) {
- return schoolBookBookRepository.findByName(name);
+ return this.schoolBookBookRepository.findByName(name);
}
@Override
public int getNumberOfBooksByName(String name) {
- return schoolBookBookRepository.findByName(name).length;
+ return this.schoolBookBookRepository.findByName(name).length;
}
@Override
public boolean removeByName(String name) {
- return schoolBookBookRepository.removeByName(name);
+ return this.schoolBookBookRepository.removeByName(name);
}
@Override
public int count() {
- return schoolBookBookRepository.count();
+ return this.schoolBookBookRepository.count();
}
@Override
public Author findAuthorByBookName(String name) {
- SchoolBook[] books = schoolBookBookRepository.findByName(name);
+ SchoolBook[] books = this.schoolBookBookRepository.findByName(name);
if (books.length > 0)
- return authorService.findByFullName(books[0].getAuthorName(), books[0].getAuthorLastName());
+ return this.authorService.findByFullName(books[0].getAuthorName(), books[0].getAuthorLastName());
return null;
}