Skip to content

Added implementation of the app structure #44

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
75 changes: 74 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 @@ -18,6 +18,79 @@
* 5) Переопределить методы equals и hashCode - используйте генерацию (не забывайте alt+inset)
* 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset)
*/

public class Author {

private String name;
private String lastName;
private LocalDate birthdate;
private String country;

public Author() {
}

@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 + '\'' +
'}';
}

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

public Author(String name, String lastName, LocalDate birthdate, String country) {
this.name = name;
this.lastName = lastName;
this.birthdate = birthdate;
this.country = country;
}
}
50 changes: 49 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 @@ -17,4 +17,52 @@
*/
public abstract class Book {

private int numberOfPages;
private String name;

@Override
public String toString() {
return "Book{" +
"numberOfPages=" + numberOfPages +
", 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);
}

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

public Book(int numberOfPages, String name) {
this.numberOfPages = numberOfPages;
this.name = name;
}

public Book() {
}
}
65 changes: 64 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 @@ -21,4 +21,67 @@
*/
public class SchoolBook extends Book {

private String authorName;
private String authorLastName;
private LocalDate publishDate;


public SchoolBook() {
}

@Override
public String toString() {
return "SchoolBook{" +
"authorName='" + authorName + '\'' +
", authorLastName='" + authorLastName + '\'' +
", 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);
}

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

public SchoolBook(int numberOfPages, String name, String authorName, String authorLastName, LocalDate publishDate) {
super(numberOfPages, name);
this.authorName = authorName;
this.authorLastName = authorLastName;
this.publishDate = publishDate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.epam.izh.rd.online.repository;

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

import java.util.Arrays;

public class SimpleAuthorRepository implements AuthorRepository {

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

@Override
public boolean save(Author author) {
for (Author value : authors) {
if (value.equals(author)) {
return false;
}
}

authors = Arrays.copyOf(authors, authors.length + 1);
authors[authors.length - 1] = author;
return true;
}

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

@Override
public boolean remove(Author author) {
for (int i = 0; i < authors.length; i++) {
if (authors[i].equals(author)) {
authors[i] = null;

System.arraycopy(authors, 0, authors, 0, i);
System.arraycopy(authors, i + 1, authors, i, authors.length - 1 - i);
authors = Arrays.copyOf(authors, authors.length - 1);

return true;
}
}


return false;
}

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

public SimpleSchoolBookRepository() {
}

public SimpleSchoolBookRepository(SchoolBook[] schoolBooks) {
this.schoolBooks = schoolBooks;
}

@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[] nameBook = new SchoolBook[0];
for (SchoolBook schoolBook : schoolBooks) {
if (schoolBook.getName().equals(name)) {
nameBook = Arrays.copyOf(nameBook, nameBook.length + 1);
nameBook[nameBook.length - 1] = schoolBook;
}
}
return nameBook;
}

@Override
public boolean removeByName(String name) {
boolean deleteSuccess = false;
for (int i = 0; i < schoolBooks.length; i++) {
if (schoolBooks[i].getName().equals(name)) {
schoolBooks[i] = null;

System.arraycopy(schoolBooks, 0, schoolBooks, 0, i);
System.arraycopy(schoolBooks, i + 1, schoolBooks, i, schoolBooks.length - 1 - i);
schoolBooks = Arrays.copyOf(schoolBooks, schoolBooks.length - 1);

deleteSuccess = true;
i--;
}
}
return deleteSuccess;
}

@Override
public int count() {
return schoolBooks.length;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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) {
if (author != null) {
return authorRepository.save(author);
}
return false;
}

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

@Override
public boolean remove(Author author) {
if (author != null) {
return authorRepository.remove(author);
}
return false;
}

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