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
1 change: 0 additions & 1 deletion src/main/java/com/epam/izh/rd/online/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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 + '\'' +
'}';
}
}
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 + '\'' +
'}';
}
}
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 +
"} " + super.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.epam.izh.rd.online.repository;

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

import java.util.Arrays;

public class SimpleAuthorRepository implements AuthorRepository {

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

return true;
} else {
for (int i = 0; i < authors.length; i++) {
if (author.getName().equals(authors[i].getName()) && author.getLastName().equals(authors[i].getLastName())) {
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 element: authors) {
if (element.getName().equals(name) && element.getLastName().equals(lastname)){
return element;
}
}

return null;
}

@Override
public boolean remove(Author author) {
Author[] checkLength = Arrays.copyOf(authors, authors.length);
int valueOfIndexArray = 0;

for (int i = 0; i < authors.length; i++) {
if (author.getName().equals(authors[i].getName()) && author.getLastName().equals(authors[i].getLastName())) {
valueOfIndexArray = i;
authors[i] = null;
}
}

if (!Arrays.equals(authors, checkLength)) {
if (valueOfIndexArray != 0) {
for (int i = valueOfIndexArray; i < authors.length - 1; i++){
authors[i-1] = authors[i];
}

authors = new Author[authors.length - 1];
authors = Arrays.copyOf(checkLength, authors.length);

return true;
} else {
for (int j = 0; j < authors.length / 2; j++) {
Author element = authors[j];
authors[j] = authors[authors.length -j -1];
authors[authors.length -j -1] = element;
}

authors = new Author[authors.length - 1];
authors = Arrays.copyOf(checkLength, authors.length);

return true;
}
}

return false;
}

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

private Author[] authors = new Author[]{};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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> {

@Override
public boolean save(SchoolBook book) {
if (schoolBooks.length == 0) {
schoolBooks = new SchoolBook[1];
schoolBooks[0] = book;
} else {
schoolBooks = Arrays.copyOf(schoolBooks, schoolBooks.length + 1);
schoolBooks[schoolBooks.length - 1] = book;
}

return true;
}

@Override
public SchoolBook[] findByName(String name) {
int neededBooksInArray = 0;
SchoolBook neededBook = null;

for (SchoolBook element : schoolBooks) {
if (name.equals(element.getName())) {
neededBooksInArray += 1;
neededBook = element;
}
}

SchoolBook[] arrayForSend = new SchoolBook[neededBooksInArray];

if (neededBooksInArray != 0) {
Arrays.fill(arrayForSend, neededBook);
}

return arrayForSend;
}

@Override
public boolean removeByName(String name) {
int booksForRemove = 0;
SchoolBook bookForSearchInArray = null;

for (SchoolBook element : schoolBooks) {
if (name.equals(element.getName())) {
booksForRemove += 1;
bookForSearchInArray = element;
}
}

if (booksForRemove != 0) {
if (booksForRemove == schoolBooks.length) {
schoolBooks = new SchoolBook[]{};
} else {
for (int i = 0; i < schoolBooks.length; i++) {
if (bookForSearchInArray.equals(schoolBooks[i])) {
schoolBooks[i] = null;
}
}

for (int j = 0; j < schoolBooks.length; j++) {
if (schoolBooks[j] == null) {
for (int l = j + 1; l < schoolBooks.length; l++) {
schoolBooks[l - 1] = schoolBooks[l];
schoolBooks[l] = null;
}
}
}

schoolBooks = Arrays.copyOf(schoolBooks, schoolBooks.length - booksForRemove);
}

return true;
}

return false;
}

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

private SchoolBook[] schoolBooks = new SchoolBook[]{};
}
Loading