Skip to content

Commit ba63600

Browse files
author
luna
committed
feature
1 parent f666c89 commit ba63600

11 files changed

Lines changed: 403 additions & 4 deletions

src/main/java/com/epam/izh/rd/online/entity/Author.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,81 @@
1818
* 5) Переопределить методы equals и hashCode - используйте генерацию (не забывайте alt+inset)
1919
* 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset)
2020
*/
21-
public class Author {
21+
public class Author{
22+
private String name;
23+
private String lastName;
24+
private LocalDate birthdate;
25+
private String country;
2226

27+
public Author() {
28+
}
29+
30+
public Author(String name, String lastName, LocalDate birthdate, String country) {
31+
this.name = name;
32+
this.lastName = lastName;
33+
this.birthdate = birthdate;
34+
this.country = country;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public void setName(String name) {
42+
this.name = name;
43+
}
44+
45+
public String getLastName() {
46+
return lastName;
47+
}
48+
49+
public void setLastName(String lastName) {
50+
this.lastName = lastName;
51+
}
52+
53+
public LocalDate getBirthdate() {
54+
return birthdate;
55+
}
56+
57+
public void setBirthdate(LocalDate birthdate) {
58+
this.birthdate = birthdate;
59+
}
60+
61+
public String getCountry() {
62+
return country;
63+
}
64+
65+
public void setCountry(String country) {
66+
this.country = country;
67+
}
68+
69+
@Override
70+
public boolean equals(Object o) {
71+
if (this == o) {
72+
return true;
73+
}
74+
if (o == null || getClass() != o.getClass()) {
75+
return false;
76+
}
77+
Author author = (Author) o;
78+
return Objects.equals(name, author.name) &&
79+
Objects.equals(lastName, author.lastName) &&
80+
Objects.equals(birthdate, author.birthdate) &&
81+
Objects.equals(country, author.country);
82+
}
83+
84+
@Override
85+
public int hashCode() {
86+
return Objects.hash(name, lastName, birthdate, country);
87+
}
88+
89+
@Override
90+
public String toString() {
91+
return "Author{" +
92+
"name='" + name + '\'' +
93+
", lastName='" + lastName + '\'' +
94+
", birthdate=" + birthdate +
95+
", country='" + country + '\'' +
96+
'}';
97+
}
2398
}

src/main/java/com/epam/izh/rd/online/entity/Book.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,56 @@
1717
*/
1818
public abstract class Book {
1919

20+
private int numberOfPages;
21+
private String name;
22+
23+
public Book() {
24+
}
25+
26+
public Book(int numberOfPages, String name) {
27+
this.numberOfPages = numberOfPages;
28+
this.name = name;
29+
}
30+
31+
public int getNumberOfPages() {
32+
return numberOfPages;
33+
}
34+
35+
public void setNumberOfPages(int numberOfPages) {
36+
this.numberOfPages = numberOfPages;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
@Override
48+
public boolean equals(Object o) {
49+
if (this == o) {
50+
return true;
51+
}
52+
if (o == null || getClass() != o.getClass()) {
53+
return false;
54+
}
55+
Book book = (Book) o;
56+
return numberOfPages == book.numberOfPages &&
57+
Objects.equals(name, book.name);
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return Objects.hash(numberOfPages, name);
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return "Book{" +
68+
"numberOfPages=" + numberOfPages +
69+
", name='" + name + '\'' +
70+
'}';
71+
}
2072
}

src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,72 @@
2020
* 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset)
2121
*/
2222
public class SchoolBook extends Book {
23+
private String authorName;
24+
private String authorLastName;
25+
private LocalDate publishDate;
2326

27+
public SchoolBook() {
28+
}
29+
30+
public SchoolBook(String authorName, String authorLastName, LocalDate publishDate) {
31+
this.authorName = authorName;
32+
this.authorLastName = authorLastName;
33+
this.publishDate = publishDate;
34+
}
35+
36+
public SchoolBook(int numberOfPages, String name, String authorName, String authorLastName, LocalDate publishDate) {
37+
super(numberOfPages, name);
38+
this.authorName = authorName;
39+
this.authorLastName = authorLastName;
40+
this.publishDate = publishDate;
41+
}
42+
43+
public String getAuthorName() {
44+
return authorName;
45+
}
46+
47+
public void setAuthorName(String authorName) {
48+
this.authorName = authorName;
49+
}
50+
51+
public String getAuthorLastName() {
52+
return authorLastName;
53+
}
54+
55+
public void setAuthorLastName(String authorLastName) {
56+
this.authorLastName = authorLastName;
57+
}
58+
59+
public LocalDate getPublishDate() {
60+
return publishDate;
61+
}
62+
63+
public void setPublishDate(LocalDate publishDate) {
64+
this.publishDate = publishDate;
65+
}
66+
67+
@Override
68+
public boolean equals(Object o) {
69+
if (this == o) return true;
70+
if (!(o instanceof SchoolBook)) return false;
71+
if (!super.equals(o)) return false;
72+
SchoolBook that = (SchoolBook) o;
73+
return authorName.equals(that.authorName) &&
74+
authorLastName.equals(that.authorLastName) &&
75+
publishDate.equals(that.publishDate);
76+
}
77+
78+
@Override
79+
public int hashCode() {
80+
return Objects.hash(super.hashCode(), authorName, authorLastName, publishDate);
81+
}
82+
83+
@Override
84+
public String toString() {
85+
return "SchoolBook{" +
86+
"authorName='" + authorName + '\'' +
87+
", authorLastName='" + authorLastName + '\'' +
88+
", publishDate=" + publishDate +
89+
'}';
90+
}
2491
}

src/main/java/com/epam/izh/rd/online/repository/AuthorRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

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

5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
59
/**
610
* Интерфейс репозитория для хранения данных об авторах.
711
* <p>

src/main/java/com/epam/izh/rd/online/repository/BookRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ public interface BookRepository<T extends Book> {
5151
* Метод возвращает количество сохраненных сущностей в массиве schoolBooks.
5252
*/
5353
int count();
54-
}
54+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.epam.izh.rd.online.repository;
2+
3+
import com.epam.izh.rd.online.entity.Author;
4+
5+
import java.util.Arrays;
6+
7+
public class SimpleAuthorRepository implements AuthorRepository{
8+
9+
private Author[] authors = new Author[0];
10+
11+
@Override
12+
public boolean save(Author author) {
13+
if(findByFullName(author.getName(), author.getLastName()) == null){
14+
authors = Arrays.copyOf(authors, authors.length+1);
15+
authors[authors.length-1] = author;
16+
return true;
17+
}
18+
return false;
19+
}
20+
21+
@Override
22+
public Author findByFullName(String name, String lastname) {
23+
for (Author obj:authors) {
24+
if(obj.getName().equals(name) && obj.getLastName().equals(lastname))
25+
return obj;
26+
}
27+
return null;
28+
}
29+
30+
@Override
31+
public boolean remove(Author author) {
32+
int id = 0;
33+
while(id < authors.length){
34+
if(authors[id] == author)
35+
{
36+
authors[id] = authors[authors.length-1];
37+
authors[authors.length-1] = new Author();
38+
39+
authors = Arrays.copyOf(authors, authors.length-1);
40+
return true;
41+
}
42+
id++;
43+
}
44+
return false;
45+
}
46+
47+
@Override
48+
public int count() {
49+
return authors.length;
50+
}
51+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.epam.izh.rd.online.repository;
2+
3+
import com.epam.izh.rd.online.entity.SchoolBook;
4+
5+
import java.util.Arrays;
6+
7+
public class SimpleSchoolBookRepository implements BookRepository<SchoolBook>{
8+
9+
private SchoolBook[] schoolBooks = new SchoolBook[0];
10+
11+
@Override
12+
public boolean save(SchoolBook book) {
13+
try {
14+
schoolBooks = Arrays.copyOf(schoolBooks, schoolBooks.length+1);
15+
schoolBooks[schoolBooks.length-1] = book;
16+
return true;
17+
}
18+
catch (Exception e){
19+
return false;
20+
}
21+
}
22+
23+
@Override
24+
public SchoolBook[] findByName(String name) {
25+
SchoolBook[] books = new SchoolBook[0];
26+
for (SchoolBook book:schoolBooks) {
27+
if(book.getName().equals(name)){
28+
books = Arrays.copyOf(books, books.length+1);
29+
books[books.length-1] = book;
30+
}
31+
}
32+
return books;
33+
}
34+
35+
@Override
36+
public boolean removeByName(String name) {
37+
38+
int counter = 1;
39+
for (int i = 0; i < schoolBooks.length; i++) {
40+
if (schoolBooks[i].getName().equals(name)) {
41+
schoolBooks[i] = schoolBooks[schoolBooks.length - counter];
42+
counter++;
43+
}
44+
}
45+
if (counter>1) {
46+
schoolBooks = Arrays.copyOf(schoolBooks, schoolBooks.length - (counter - 1));
47+
return true;
48+
}
49+
else
50+
return false;
51+
}
52+
53+
@Override
54+
public int count() {
55+
return (int)Arrays.stream(schoolBooks).count();
56+
}
57+
}

src/main/java/com/epam/izh/rd/online/service/AuthorService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ public interface AuthorService {
4141
* По факту, он просто обращается к репозиторию с авторами и вызывает аналогичный метод, псоле чего возвращает результат.
4242
*/
4343
int count();
44-
}
44+
}

src/main/java/com/epam/izh/rd/online/service/BookService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.epam.izh.rd.online.entity.Author;
44
import com.epam.izh.rd.online.entity.Book;
5+
import com.epam.izh.rd.online.repository.BookRepository;
56

67
/**
78
* Интерфейс сервиса для выполнения бизнес логики при работе с книга и авторами и взаимодействием с
@@ -70,4 +71,4 @@ public interface BookService<T extends Book> {
7071
* Если такой книги не найдено, метод должен вернуть null.
7172
*/
7273
Author findAuthorByBookName(String name);
73-
}
74+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.epam.izh.rd.online.service;
2+
3+
import com.epam.izh.rd.online.entity.Author;
4+
import com.epam.izh.rd.online.repository.AuthorRepository;
5+
6+
public class SimpleAuthorService implements AuthorService {
7+
8+
private AuthorRepository authorRepository;
9+
10+
public SimpleAuthorService() {
11+
}
12+
13+
public SimpleAuthorService(AuthorRepository authorRepository) {
14+
this.authorRepository = authorRepository;
15+
}
16+
17+
@Override
18+
public boolean save(Author author) {
19+
return authorRepository.save(author);
20+
}
21+
22+
@Override
23+
public Author findByFullName(String name, String lastname) {
24+
return authorRepository.findByFullName(name, lastname);
25+
}
26+
27+
@Override
28+
public boolean remove(Author author) {
29+
return authorRepository.remove(author);
30+
}
31+
32+
@Override
33+
public int count() {
34+
return authorRepository.count();
35+
}
36+
}

0 commit comments

Comments
 (0)