Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion src/main/java/com/epam/izh/rd/online/entity/Author.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* Класс содержащий информацию об авторе.
*
* <p>
* Необходимо:
* 1) Создать список полей с указанными типами ровно в этом порядке:
* - name с типом String и приватным модификатором доступа
Expand All @@ -19,5 +19,72 @@
* 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset)

This comment was marked as resolved.

*/
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());
}
}
45 changes: 44 additions & 1 deletion src/main/java/com/epam/izh/rd/online/entity/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Базовая сущность для книги. Содержит базовые поля.
*
* <p>
* Необходимо:
* 1) Создать список полей с указанными типами ровно в этом порядке:
* - numberOfPages с типом int и приватным модификатором доступа
Expand All @@ -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());
}
}
66 changes: 65 additions & 1 deletion src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* Сущность учебника. Он должен быть унаследован от сущности Book
*
* <p>
* Необходимо:
* 1) Унаследовать данный класс от класса Book
* 2) Создать список полей с указанными типами ровно в этом порядке:
Expand All @@ -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 comment was marked as resolved.

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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.epam.izh.rd.online.repository;

import com.epam.izh.rd.online.entity.Author;

public class SimpleAuthorRepository implements AuthorRepository {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Комментарии всегда нужно писать. Описывать назначение класса. Указывать тег @author

private Author[] authors = new Author[0];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А почему здесь не использовать коллекции? Например, интерфейс List и его реализация ArrayList.


@Override
public boolean save(Author author) {
int numOfAuthors = this.authors.length;
if (numOfAuthors > 0) {
if (findByFullNameGetIndex(author.getName(), author.getLastName()) >= 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Обращение к методу текущего класса лучше выполнять через this.

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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Методы обязательно снабжаются комментариями, вкратце описывающими их алгоритм работы и входные параметры

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];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Весь этот код можно было не писать, используя ArrayList

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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.epam.izh.rd.online.repository;

import com.epam.izh.rd.online.entity.SchoolBook;

public class SimpleSchoolBookRepository implements BookRepository<SchoolBook> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В этом репозитории те же замечания, что и в предыдущем

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;
}
}
Loading