Skip to content

Test fix #50

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 2 commits 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
20 changes: 19 additions & 1 deletion src/main/java/com/epam/izh/rd/online/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
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.repository.SimpleAuthorRepository;
import com.epam.izh.rd.online.repository.SimpleSchoolBookRepository;

import java.util.Arrays;

public class Main {

public static void main(String[] args) {
/*SimpleAuthorRepository sa = new SimpleAuthorRepository();
Author author = new Author();
author.setName("Ruslan");
author.setLastName("Mavlyashov");
sa.save(author);
System.out.println(author.getName() + " " + author.getLastName() + " " + author.getBirthdate());*/

SimpleSchoolBookRepository sb = new SimpleSchoolBookRepository();
SchoolBook schoolBooks = new SchoolBook();
schoolBooks.setName("Master");
schoolBooks.setName("Margarita");
sb.removeByName("Master");
}

}
73 changes: 72 additions & 1 deletion src/main/java/com/epam/izh/rd/online/entity/Author.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* Класс содержащий информацию об авторе.
*
* <p>
* Необходимо:
* 1) Создать список полей с указанными типами ровно в этом порядке:
* - name с типом String и приватным модификатором доступа
Expand All @@ -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 void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getLastName() {
return lastName;
}

public void setBirthdate(LocalDate birthdate) {
this.birthdate = birthdate;
}

public LocalDate getBirthdate() {
return birthdate;
}

public void setCountry(String country) {
this.country = country;
}

public String getCountry() {
return 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 + '\'' +
", birthday=" + birthdate +
", country='" + country + '\'' +
'}';
}
}
45 changes: 45 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,50 @@
* 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 void setNumberOfPage(int numberOfPages) {
this.numberOfPages = numberOfPages;
}
public int getNumberOfPages() {
return numberOfPages;
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return 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 + '\'' +
'}';
}
}
63 changes: 62 additions & 1 deletion src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* Сущность учебника. Он должен быть унаследован от сущности Book
*
* <p>
* Необходимо:
* 1) Унаследовать данный класс от класса Book
* 2) Создать список полей с указанными типами ровно в этом порядке:
Expand All @@ -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 numberOfpage, String name, String authorName, String authorlastName, LocalDate publishDate) {
super(numberOfpage, name);
this.authorName = authorName;
this.authorLastName = authorlastName;
this.publishDate = publishDate;
}

public String getAuthorName() {
return authorName;
}

public String getAuthorLastName() {
return authorLastName;
}

public LocalDate getPublishDate() {
return publishDate;
}

public void setAuthorName(String authorName) {
this.authorName = authorName;
}

public void setAuthorLastName(String authorLastName) {
this.authorLastName = authorLastName;
}

public void setPublishDate(LocalDate publishDate) {
this.publishDate = publishDate;
}

@Override
public int hashCode() {
return Objects.hash(authorName, authorLastName, publishDate);
}

@Override
public String toString() {
return "SchoolBook{" +
"authorName='" + authorName + '\'' +
", authorLastName='" + authorLastName + '\'' +
", 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 authorName.equals(that.authorName) &&
authorLastName.equals(that.authorLastName) &&
publishDate.equals(that.publishDate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.epam.izh.rd.online.repository;

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

public class SimpleAuthorRepository implements AuthorRepository {
@Override
public boolean save(Author author) {
if (findByFullName(author.getName(), author.getLastName()) == null) {
Author[] temp = new Author[authors.length + 1];
for (int i = 0; i < authors.length; i++) {
temp[i] = authors[i];
}
temp[authors.length] = author;
authors = temp;
return true;
}
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) {
if (findByFullName(author.getName(), author.getLastName()) == null) {
return false;
} else {
int i = 0;
Author[] temp = new Author[authors.length - 1];
for (Author a: authors) {
if (a != findByFullName(author.getName(), author.getLastName())) {
temp[i] = a;
i++;
}
}
authors = temp;
return true;
}
}

@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,61 @@
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) {
SchoolBook[] temp = new SchoolBook[schoolBooks.length + 1];
for (int i = 0; i < schoolBooks.length; i++) {
temp[i] = schoolBooks[i];
}
temp[schoolBooks.length] = book;
schoolBooks = temp;
return true;
}

@Override
public SchoolBook[] findByName(String name) {
if (schoolBooks != null) {
int i = 0;
SchoolBook[] findBook = new SchoolBook[schoolBooks.length];
for (SchoolBook scBook : schoolBooks) {
if (scBook.getName().equals(name)) {
findBook[i] = scBook;
i++;
}
}
return Arrays.copyOf(findBook, i);
} else return new SchoolBook[0];
}

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

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

private SchoolBook[] schoolBooks = new SchoolBook[]{};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.epam.izh.rd.online.service;

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

public class SimpleAuthorService implements AuthorService {
@Override
public boolean save(Author author) {
return authorRepository.save(author);
}

@Override
public Author findByFullName(String name, String lastname) {
return authorRepository.findByFullName(name, lastname);
}

@Override
public boolean remove(Author author) {
return authorRepository.remove(author);
}

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

public SimpleAuthorService() {
}


public SimpleAuthorService(AuthorRepository authorRepository) {
this.authorRepository = authorRepository;
}

private AuthorRepository authorRepository;
}
Loading