Skip to content
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

Feature / All done #78

Open
wants to merge 8 commits 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
70 changes: 69 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,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 name.equals(author.name) && lastName.equals(author.lastName) && birthdate.equals(author.birthdate) && country.equals(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 + '\'' +
'}';
}
}
48 changes: 47 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 @@ -15,6 +15,52 @@
* 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 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 + '\'' +
'}';
}
}
63 changes: 62 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 @@ -19,6 +19,67 @@
* 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 authorName.equals(that.authorName) && authorLastName.equals(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 +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 SimpleAuthorRepository 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) {
// Arrays.stream(authors).forEach(authorForEach.getName()::equals);
isName = name == authorForEach.getName();
isLastName = lastname == authorForEach.getLastName();
if (isName && isLastName) {
return authorForEach;
}
}
return null;
}

@Override
public boolean remove(Author author) {
boolean isAuthor = author.equals(findByFullName(author.getName(), author.getLastName()));
if (isAuthor) {
Author[] tempAuthors = Arrays.stream(authors)
.filter(authorDel -> !author.equals(authorDel))
.toArray(Author[]::new);
authors = tempAuthors;
return true;
}
return false;
}

@Override
public int count() {
return authors.length;
}
}
Original file line number Diff line number Diff line change
@@ -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<SchoolBook> {

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 по названию.
* Если книг с одинаковым названием в массиве несколько, метод должен удалить их все.
* <p>
* Важно: при удалении книги из массива размер массива должен уменьшиться!
* То есть, если мы сохранили 2 разные книги и вызвали count() (метод ниже), то он должен вернуть 2.
* Если после этого мы удалили 1 книгу, метод count() должен вернуть 1.
* <p>
* Если хотя бы одна книга была найдена и удалена, метод должен вернуть 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;
}
}
Original file line number Diff line number Diff line change
@@ -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 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();
}
}
Loading