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
68 changes: 68 additions & 0 deletions src/main/java/com/epam/izh/rd/online/entity/Author.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '\'' +
'}';
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/epam/izh/rd/online/entity/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 || 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 + '\'' +
'}';
}
}
61 changes: 60 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 @@ -14,11 +14,70 @@
* - publishDate с типом LocalDate и приватным модификатором доступа
* 2) Создать дефолтный конструктор (без параметров) (не забывайте alt+inset)
* 3) Создать конструктор со всеми параметрами (важно - не только с полями данного класса, но и с полями родителя Book)
* (создавать в том порядке в котором перечислены). Сначала нужно создать аналогичный конструтор для Book. Используйте alt+inset.
* (создавать в том порядке в котором перечислены). Сначала нужно создать аналогичный конструктор для Book. Используйте alt+inset.
* 4) Создать геттеры и сеттеры (не забывайте alt+inset)
* 5) Переопределить методы equals и hashCode - используйте генерацию (не забывайте alt+inset)
* 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 +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -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;

/**
* Интерфейс репозитория для хранения данных о книгах
Expand All @@ -13,7 +14,7 @@
* 5) Инициалазировать его пустым массивом
* 6) Написать в классе SimpleSchoolBookRepository реализацию для всех методов (коллекции не используем, работаем только с массивами)
*/
public interface BookRepository<T extends Book> {
public interface BookRepository<T extends SchoolBook> {

/**
* Метод должен сохранять школьную книгу в массив schoolBooks.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.epam.izh.rd.online.repository;

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


public class SimpleAuthorRepository implements AuthorRepository {
private Author[] authors = new Author[0];

@Override
public boolean save(Author author) {
if( findByFullName(author.getName(), author.getLastName()) == null) {
Author[] temp;
if(authors.length == 0) {
authors = new Author[1];
authors[0] = author;
return true;
} else {
temp = authors;
authors = new Author[temp.length+1];
for (int i = 0; i < temp.length; i++){
authors[i] = temp[i];
}
authors[authors.length-1] = author;
return true;
}
} else {
return false;
}
}

@Override
public Author findByFullName(String name, String lastname) {
for (int i = 0; i < authors.length ; i++) {
if (name == authors[i].getName() && lastname == authors[i].getLastName()){
return authors[i];
}
}
return null;
}

@Override
public boolean remove(Author author) {
if( findByFullName(author.getName(), author.getLastName()) != null) {
Author[] temp = new Author[authors.length-1];
for(int i = 0; i < authors.length; i++) {
int j = 0;
if(authors[i] != author ) {
temp[j] = authors[i];
j++;
};
}
authors = temp;
return true;
} else {
return false;
}
}

@Override
public int count() {
return authors.length;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.epam.izh.rd.online.repository;

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

public class SimpleSchoolBookRepository implements BookRepository<SchoolBook> {
public SchoolBook[] schoolBooks = new SchoolBook[0];


@Override
public boolean save(SchoolBook book) {
SchoolBook[] temp;
if (this.schoolBooks.length == 0) {
this.schoolBooks = new SchoolBook[1];
this.schoolBooks[0] = book;
return true;
} else {
temp = schoolBooks;
schoolBooks = new SchoolBook[temp.length + 1];
for (int i = 0; i < temp.length; i++) {
schoolBooks[i] = temp[i];
}
schoolBooks[schoolBooks.length-1] = book;
return true;

}
}

@Override
public SchoolBook[] findByName(String name) {
if (schoolBooks.length > 0) {
int j = 0;
for (int i = 0; i < this.schoolBooks.length; i++) {
if (this.schoolBooks[i]!= null) {
if (name == this.schoolBooks[i].getName()) {
j++;
}
}
}
if (j > 0) {
SchoolBook[] schoolBooksOneName = new SchoolBook[j];
int temp = 0;
for (int i = 0; i < this.schoolBooks.length; i++) {
if (this.schoolBooks[i]!= null) {
if (name == this.schoolBooks[i].getName()) {
schoolBooksOneName[temp] = schoolBooks[i];
temp++;
}
}
}
return schoolBooksOneName;

}

}
return new SchoolBook[0];
}

@Override
public boolean removeByName(String name) {
if( this.schoolBooks.length > 0 && findByName(name) != null ) {
SchoolBook[] temp = new SchoolBook[this.schoolBooks.length-findByName(name).length];
for(int i = 0; i < this.schoolBooks.length; i++) {
int j = 0;
if(this.schoolBooks[i].getName() != name ) {
temp[j] = this.schoolBooks[i];
j++;
};
}
this.schoolBooks = temp;
return true;
} else {
return false;
}
}

@Override
public int count() {
return this.schoolBooks.length;
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/epam/izh/rd/online/service/BookService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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;

/**
* Интерфейс сервиса для выполнения бизнес логики при работе с книга и авторами и взаимодействием с
Expand All @@ -20,7 +21,7 @@
* (который будет устанвливать в поле schoolBookBookRepository и в поле authorService значения)
* 8) Написать в классе SimpleSchoolBookService реализацию для всех методов
*/
public interface BookService<T extends Book> {
public interface BookService <T extends SchoolBook> {

/**
* Метод должен сохранять книгу.
Expand Down Expand Up @@ -70,4 +71,5 @@ public interface BookService<T extends Book> {
* Если такой книги не найдено, метод должен вернуть null.
*/
Author findAuthorByBookName(String name);

}
Loading