Skip to content

did all tasks #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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: 69 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,74 @@
* 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);
}

@Override
public int hashCode() {
return Objects.hash(name, lastName, birthdate, country);
}

@Override
public String toString() {
return "Author{" +
"name='" + name + '\'' +
", lastName='" + lastName + '\'' +
", birthday=" + birthdate +
", country='" + country + '\'' +
'}';
}
}
47 changes: 47 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,52 @@
* 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 String getName() {
return name;
}

public void setNumberOfPages(int numberOfPages) {
this.numberOfPages = numberOfPages;
}

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: 61 additions & 0 deletions src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,66 @@
* 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
Expand Up @@ -2,6 +2,9 @@

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

import java.util.ArrayList;
import java.util.Arrays;

/**
* Интерфейс репозитория для хранения данных об авторах.
* <p>
Expand Down Expand Up @@ -57,3 +60,4 @@ public interface AuthorRepository {
*/
int count();
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.epam.izh.rd.online.repository;

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 java.util.ArrayList;
import java.util.Arrays;

/**
* Интерфейс репозитория для хранения данных о книгах
Expand Down Expand Up @@ -52,3 +57,4 @@ public interface BookRepository<T extends Book> {
*/
int count();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.epam.izh.rd.online.repository;

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

import java.util.Arrays;

public class SimpleAuthorRepository implements AuthorRepository{

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

@Override
public boolean save(Author author) {
if (findByFullName(author.getName(), author.getLastName()) != null){
return false;
}
authors = Arrays.copyOf(authors, authors.length + 1);
authors[authors.length - 1] = author;
return true;
}

@Override
public Author findByFullName(String name, String lastname) {
cnt = 0;
for(Author author:authors){
if (author.getName().equals(name) && author.getLastName().equals(lastname)){
return author;
}
cnt++;
}
return null;
}

/**
* Метод должен удалять автора из массива authors, если он там имеется.
* Автора опять же, можно определять только по совпадению имении и фамилии.
* <p>
* Важно: при удалении автора из массива размер массива должен уменьшиться!
* То есть, если мы сохранили 2 авторов и вызвали count() (метод ниже), то он должен вернуть 2.
* Если после этого мы удалили 1 автора, метод count() должен вернуть 1.
* <p>
* Если автор был найден и удален, метод должен вернуть true, в противном случае, если автор не был найден, метод
* должен вернуть false.
*/
@Override
public boolean remove(Author author) {
if (findByFullName(author.getName(), author.getLastName()) == null){
return false;
}

System.arraycopy(authors, cnt + 1, authors, cnt,
authors.length - cnt - 1);
authors = Arrays.copyOf(authors,authors.length - 1);
return true;
}

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

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

import java.util.Arrays;

public class SimpleSchoolBookRepository implements BookRepository<SchoolBook>{

SchoolBook[] schoolBooks = new SchoolBook[0];
/**
* Метод должен сохранять школьную книгу в массив schoolBooks.
* Массив при каждом сохранении должен увеличиваться в размере ровно на 1.
* То есть он ровно того размера, сколько сущностей мы в него сохранили.
* <p>
* Одну и ту же книгу МОЖНО сохранить в массиве несколько раз, проверки на то, что такая книга уже сохранена, делать не нужно.
* <p>
* Если сохранение прошло успешно, метод должен вернуть true.
*/
@Override
public boolean save(SchoolBook book) {
schoolBooks = Arrays.copyOf(schoolBooks, schoolBooks.length + 1);
schoolBooks[schoolBooks.length - 1] = book;
return true;
}

@Override
public SchoolBook[] findByName(String name) {
SchoolBook[] reapeatedBooks = new SchoolBook[0];
for(int i = 0; i < schoolBooks.length; i++){
if (schoolBooks[i].getName().equals(name)){
reapeatedBooks = Arrays.copyOf(reapeatedBooks, reapeatedBooks.length + 1);
reapeatedBooks[reapeatedBooks.length - 1] = schoolBooks[i];
}
//cnt++;
}
return reapeatedBooks;
}

@Override
public boolean removeByName(String name) {
boolean isBookFound = false;
for(int i = 0; i < schoolBooks.length; i++){
if (schoolBooks[i].getName().equals(name)) {
isBookFound = true;
System.arraycopy(schoolBooks, i + 1, schoolBooks, i,
schoolBooks.length - i - 1);
schoolBooks = Arrays.copyOf(schoolBooks, schoolBooks.length - 1);
if (schoolBooks.length == 1){
System.arraycopy(schoolBooks, 1, schoolBooks, 0,
schoolBooks.length - 0 - 1);
schoolBooks = Arrays.copyOf(schoolBooks, schoolBooks.length - 1);
}
}
}
return isBookFound;
}

@Override
public int count() {
return schoolBooks.length;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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{
private AuthorRepository authorRepository;
private SimpleAuthorRepository simpleAuthorRepository = new SimpleAuthorRepository();

public SimpleAuthorService() {
}

public SimpleAuthorService(AuthorRepository authorRepository) {
this.authorRepository = authorRepository;
}

/**
* Метод должен сохранять автора.
* По факту, он просто обращается к репозиторию с авторами и вызывает аналогичный метод, псоле чего возвращает результат.
*/
@Override
public boolean save(Author author) {
return simpleAuthorRepository.save(author);
}

@Override
public Author findByFullName(String name, String lastname) {
return simpleAuthorRepository.findByFullName(name, lastname);
}

@Override
public boolean remove(Author author) {
return simpleAuthorRepository.remove(author);
}

@Override
public int count() {
return simpleAuthorRepository.count();
}
}
Loading