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
410 changes: 410 additions & 0 deletions src/main/java/com/epam/izh/rd/online/Main.java

Large diffs are not rendered by default.

78 changes: 78 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 @@ -20,4 +20,82 @@
*/
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;

if (name != null ? !name.equals(author.name) : author.name != null) return false;
if (lastName != null ? !lastName.equals(author.lastName) : author.lastName != null) return false;
if (birthdate != null ? !birthdate.equals(author.birthdate) : author.birthdate != null) return false;
return country != null ? country.equals(author.country) : author.country == null;
}

@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
result = 31 * result + (birthdate != null ? birthdate.hashCode() : 0);
result = 31 * result + (country != null ? country.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "instance of class Author = { " +
"name='" + name + '\'' +
", lastName='" + lastName + '\'' +
", birthdate=" + birthdate +
", country='" + country + '\'' +
" }";
}
}
52 changes: 52 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 @@ -17,4 +17,56 @@
*/
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;

if (numberOfPages != book.numberOfPages) return false;
return name != null ? name.equals(book.name) : book.name == null;
}

@Override
public int hashCode() {
int result = numberOfPages;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "instance of class Book = { " +
"numberOfPages=" + numberOfPages +
", name='" + name + '\'' +
"' }";
}
}
71 changes: 71 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 @@ -21,4 +21,75 @@
*/
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;

if (authorName != null ? !authorName.equals(that.authorName) : that.authorName != null) return false;
if (authorLastName != null ? !authorLastName.equals(that.authorLastName) : that.authorLastName != null)
return false;
return publishDate != null ? publishDate.equals(that.publishDate) : that.publishDate == null;
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (authorName != null ? authorName.hashCode() : 0);
result = 31 * result + (authorLastName != null ? authorLastName.hashCode() : 0);
result = 31 * result + (publishDate != null ? publishDate.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "instance of class SchoolBook = { " +
"Number of pages = " + this.getNumberOfPages() +
", Name='" + this.getName() + '\'' +
", authorName='" + authorName + '\'' +
", authorLastName='" + authorLastName + '\'' +
", publishDate=" + publishDate +
"' }";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.epam.izh.rd.online.repository;

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

import java.util.Arrays;

/**
* Класс SimpleAuthorRepository репозитория для хранения данных об авторах.
*
* Необходимо:
* 1) Имплементировать в данном классе интерфейс AuthorRepository
* 2) Добавить все методы интерфейса (пока можно не писать реализацию)
* 3) Добавить приватное поле "Author[] authors" для хранения авторов
* 4) Инициалазировать его пустым массивом
* 5) Написать реализацию для всех методов (коллекции не используем, работаем только с массивами)
*/
public class SimpleAuthorRepository implements AuthorRepository {

private Author[] authors = {};
private int counter = 0;

/**
* Метод должен сохранять автора в массив authors.
* Массив при каждом сохранении должен увеличиваться в размере ровно на 1.
* То есть он ровно того размера, сколько сущностей мы в него сохранили.
* <p>
* Если на вход для сохранения приходит автор, который уже есть в массиве (сохранен), то автор не сохраняется и
* метод возвращает false.
* <p>
* Можно сравнивать только по полному имени (имя и фамилия), считаем, что двух авторов
* с одинаковыми именем и фамилией быть не может.
* Подсказка - можно использовать для проверки метод findByFullName.
* <p>
* Если сохранение прошло успешно, метод должен вернуть true.
*/
@Override
public boolean save(Author author) {

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

Author[] temp;
temp = authors;
authors = new Author[counter + 1];

for (int i = 0; i < counter; i++) {
authors[i] = temp[i];
}

authors[counter++] = author;
return true;
}
}

/**
* Метод должен находить в массиве authors автора по имени и фамилии (считаем, что двух авторов
* с одинаковыми именем и фамилией быть не может.)
* <p>
* Если автор с таким именем и фамилией найден - возвращаем его, если же не найден, метод должен вернуть null.
*/
@Override
public Author findByFullName(String name, String lastname) {

if (authors.length == 0) {
return null;
}

for(Author a : authors) {
if ((a.getName() == name) && (a.getLastName() == lastname)) {
return a;
}
}

return null;
}

/**
* Метод должен удалять автора из массива authors, если он там имеется.
* Автора опять же, можно определять только по совпадению имении и фамилии.
* <p>
* Важно: при удалении автора из массива размер массива должен уменьшиться!
* То есть, если мы сохранили 2 авторов и вызвали count() (метод ниже), то он должен вернуть 2.
* Если после этого мы удалили 1 автора, метод count() должен вернуть 1.
* <p>
* Если автор был найден и удален, метод должен вернуть true, в противном случае, если автор не был найден, метод
* должен вернуть false.
*/
@Override
public boolean remove(Author author) {

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

Author[] temp;
temp = authors;
authors = new Author[counter - 1];

for (int i = 0, k = 0; i < counter; i++) {
if(temp[i] == author) {
continue;
}
authors[k++] = temp[i];
}

counter--;
return true;
}
}

/**
* Метод возвращает количество сохраненных сущностей в массиве authors.
*/
@Override
public int count() {
return counter;
}
}
Loading