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
71 changes: 71 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,76 @@
* 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 Integer getNumberOfPages() {
return numberOfPages;
}

public void setNumberOfPages(Integer 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 Objects.equals(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 + '\'' +
'}';
}
}
69 changes: 69 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,74 @@
* 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 SchoolBook(Integer 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
@@ -0,0 +1,54 @@
package com.epam.izh.rd.online.repository;

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

public class SimpleAuthorRepository implements AuthorRepository {

@Override
public boolean save(Author author) {
for (int i = 0; i < authors.length; i++) {
if (authors[i].equals(author)) return false;
}
Author[] authors1 = new Author[authors.length + 1];
java.lang.System.arraycopy(authors, 0, authors1, 0, authors.length);
authors1[authors1.length - 1] = author;
this.authors = authors1;
return true;
}

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

@Override
public boolean remove(Author author) {
for (int i = authors.length - 1; i >= 0; i--) {
if (author.equals(authors[i])) {
Author[] author2 = new Author[authors.length - 1];
java.lang.System.arraycopy(authors, 0, author2, 0, authors.length - 1);
if (i != authors.length - 1)
java.lang.System.arraycopy(authors, i + 1, author2, i, authors.length - (authors.length - (i + 1)));
this.authors = author2;
return true;
}
}
return false;
}

@Override
public int count() {
int counter = 0;
for (Author author : authors) {
if (author != null)
counter++;
}
return counter;
}

private Author[] authors = new Author[0];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.epam.izh.rd.online.repository;

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

public class SimpleSchoolBookRepository implements BookRepository<SchoolBook>{

@Override
public boolean save(SchoolBook book) {
SchoolBook[] schoolBooks2 = new SchoolBook[schoolBooks.length + 1];
java.lang.System.arraycopy(schoolBooks, 0, schoolBooks2, 0, schoolBooks.length);
schoolBooks2[schoolBooks2.length - 1] = (SchoolBook) book;
this.schoolBooks = schoolBooks2;
return true;
}

@Override
public SchoolBook[] findByName(String name) {
SchoolBook[] books = new SchoolBook[0];
for (int i = 0; i < schoolBooks.length; i++) {
if (name.equals(schoolBooks[i].getName())) {
SchoolBook[] book2 = new SchoolBook[books.length + 1];
java.lang.System.arraycopy(books, 0, book2, 0, books.length);
book2[books.length] = schoolBooks[i];
books = book2;
}
}
return books;
}

@Override
public boolean removeByName(String name) {
boolean flag = false;
for (int i = schoolBooks.length -1; i >= 0; i--) {
if (name.equals(schoolBooks[i].getName())) {
SchoolBook[] books = new SchoolBook[schoolBooks.length - 1];
java.lang.System.arraycopy(schoolBooks, 0, books, 0, schoolBooks.length - 1);
if (i != schoolBooks.length - 1) {
java.lang.System.arraycopy(schoolBooks, i + 1, books, i, schoolBooks.length - (schoolBooks.length - (i + 1)));
}
this.schoolBooks = books;
flag = true;
}
}
return flag;
}

@Override
public int count() {
return schoolBooks.length;
}

private SchoolBook[] schoolBooks = new SchoolBook[0];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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.service.AuthorService;

public class SimpleAuthorService implements AuthorService {

@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();
}

private AuthorRepository authorRepository;

public SimpleAuthorService() {
}

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