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

final commit #52

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
9 changes: 9 additions & 0 deletions src/main/java/com/epam/izh/rd/online/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package com.epam.izh.rd.online;

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 com.epam.izh.rd.online.service.SimpleAuthorService;
import com.epam.izh.rd.online.service.SimpleSchoolBookService;

public class Main {


public static void main(String[] args) {



}

}
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 + '\'' +
'}';
}
}
49 changes: 49 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,54 @@
* 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 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 &&
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 @@ -35,6 +35,7 @@ public interface AuthorRepository {
* Метод должен находить в массиве authors автора по имени и фамилии (считаем, что двух авторов
* с одинаковыми именем и фамилией быть не может.)
* <p>
*
* Если автор с таким именем и фамилией найден - возвращаем его, если же не найден, метод должен вернуть null.
*/
Author findByFullName(String name, String lastname);
Expand All @@ -51,7 +52,6 @@ public interface AuthorRepository {
* должен вернуть false.
*/
boolean remove(Author author);

/**
* Метод возвращает количество сохраненных сущностей в массиве authors.
*/
Expand Down
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 Down Expand Up @@ -51,4 +52,6 @@ public interface BookRepository<T extends Book> {
* Метод возвращает количество сохраненных сущностей в массиве schoolBooks.
*/
int count();


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

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

import java.util.Arrays;

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

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

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


@Override
public boolean remove(Author author) {
Author[] authors1 = new Author[0];
authors1 = Arrays.copyOf(authors, count);
if (findByFullName(author.getName(), author.getLastName()) != null) {
for (int i = 0; i < authors1.length; i++) {
if (authors1[i].getName().equals(author.getName()) && authors1[i].getLastName().equals(author.getLastName())) {
Author author1 = new Author();
author1 = authors1[i];
if (authors1[i].equals(author1)) {
authors1[i] = authors1[authors1.length - 1];
authors1[authors1.length - 1] = author1;
count--;
}
}
}
authors = Arrays.copyOf(authors1, count);
return true;
}
return false;
}

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

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

import java.util.Arrays;

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


@Override
public boolean save(SchoolBook book) {
SchoolBook[] schoolBooks1 = new SchoolBook[count];
schoolBooks1 = Arrays.copyOf(schoolBooks, count);
count++;
schoolBooks = Arrays.copyOf(schoolBooks1, count);
schoolBooks[schoolBooks.length - 1] = book;
return true;
}


@Override
public SchoolBook[] findByName(String name) {
int index = 0;
SchoolBook[] foundBook = new SchoolBook[0];
for (SchoolBook book : schoolBooks) {
if (book.getName().equals(name)) {
index++;
}
}
foundBook = new SchoolBook[index];
int foundBookIndex = 0;
for (SchoolBook schoolBook : schoolBooks) {
if (schoolBook.getName().equals(name)) {
foundBook[foundBookIndex] = schoolBook;
foundBookIndex++;
}
}
return foundBook;
}

@Override
public boolean removeByName(String name) {
SchoolBook[] schoolBooks1 = new SchoolBook[0];
schoolBooks1 = Arrays.copyOf(schoolBooks, count);
if (findByName(name).length != 0) {
for (int i = 0; i < schoolBooks1.length; i++) {
if (schoolBooks1[i].getName().equals(name)) {
SchoolBook schoolBook = new SchoolBook();
schoolBook = schoolBooks1[i];
count--;
if (schoolBooks1[i].equals(schoolBook)) {
schoolBooks1[i] = schoolBooks1[schoolBooks1.length - 1];
schoolBooks1[schoolBooks1.length - 1] = schoolBook;
}
}
}
schoolBooks = Arrays.copyOf(schoolBooks1, count);
return true;
}

return false;
}


@Override
public int count() {
return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public interface BookService<T extends Book> {
/**
* Метод должен возвращать автора книги по названию книги.
*
* То есть приждется сходить и в репозиторий с книгами и в сервис авторов.
* То есть придется сходить и в репозиторий с книгами и в сервис авторов.
*
* Если такой книги не найдено, метод должен вернуть null.
*/
Expand Down
Loading