Skip to content

finish1 #58

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
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 + '\'' +
'}';
}
}
47 changes: 47 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,52 @@
* 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 + '\'' +
'}';
}
}
60 changes: 60 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,65 @@
* 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;
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(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,88 @@
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[1];

@Override
public boolean save(Author author) {
if (authors[0] == null) {
Author[] authorSave = new Author[authors.length];
authorSave[0] = author;
authors = authorSave;

return true;
} else if (findByFullName(author.getName(), author.getLastName()) == null) {

Author[] authorSecondSave = Arrays.copyOf(authors, authors.length + 1);

for (int i = 1; i < authorSecondSave.length; i++) {
if (authorSecondSave[i] == null)
authorSecondSave[i] = author;
}
authors = authorSecondSave;

return true;
} else
return false;

}

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

@Override
public boolean remove(Author author) {
if (findByFullName(author.getName(), author.getLastName()) == null) {
return false;
}
Author findAuthor = findByFullName(author.getName(), author.getLastName());

Author[] authorRemove = Arrays.copyOf(authors, authors.length);
for (int i = 0; i < authorRemove.length; i++) {
if (authorRemove[i].equals(findAuthor)) {
authorRemove[i] = null;
}
}
if (authorRemove[0] == null) {
authorRemove[0] = authorRemove[authorRemove.length - 1];
authorRemove[authorRemove.length - 1] = null;
Author[] authorRem = Arrays.copyOf(authorRemove, authorRemove.length - 1);
authors = authorRem;

} else if (authorRemove[authorRemove.length - 1] == null) {
Author[] authorRem = Arrays.copyOf(authorRemove, authorRemove.length - 1);
authors = authorRem;
} else
for (int i = 1; i < authorRemove.length; i++) {
if (authorRemove[i] == null) {
authorRemove[i] = authorRemove[authorRemove.length - 1];
authorRemove[authorRemove.length - 1] = null;
Author[] authorRem = Arrays.copyOf(authorRemove, authorRemove.length - 1);
authors = authorRem;
}
}
return true;
}

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

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

import java.util.Arrays;

public class SimpleSchoolBookRepository implements BookRepository<SchoolBook> {
private SchoolBook[] schoolBooks = new SchoolBook[1];


@Override
public boolean save(SchoolBook book) {
if (schoolBooks[0] == null) {
SchoolBook[] saveSchoolBooks = new SchoolBook[schoolBooks.length];
saveSchoolBooks[0] = book;
schoolBooks = saveSchoolBooks;
return true;
} else {
SchoolBook[] secondSaveSchoolBooks = Arrays.copyOf(schoolBooks, schoolBooks.length + 1);
for (int i = 0; i < secondSaveSchoolBooks.length; i++) {
if (secondSaveSchoolBooks[i] == null)
secondSaveSchoolBooks[i] = book;
}
schoolBooks = secondSaveSchoolBooks;
return true;
}

}

@Override
public SchoolBook[] findByName(String name) {
SchoolBook[] findSchoolBooks = new SchoolBook[schoolBooks.length];
int count = 0;

if (schoolBooks[0] == null) {
return findSchoolBooks;
}
for (int i = 0; i < schoolBooks.length; i++) {

if (schoolBooks[i].getName().equals(name)) {
findSchoolBooks[count] = schoolBooks[i];
count++;
}

}
SchoolBook[] finishFind = new SchoolBook[count];

for (int i = 0; i < findSchoolBooks.length; i++) {
if (findSchoolBooks[i] != null) {
finishFind[i] = findSchoolBooks[i];
}
}
return finishFind;
}

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

finishRemoveBook[count] = removeBook[i];
count++;
}
}
schoolBooks = finishRemoveBook;
return true;
}
return false;
}

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