diff --git a/src/ru/skypro/Author.java b/src/ru/skypro/Author.java new file mode 100644 index 0000000..09bb5bc --- /dev/null +++ b/src/ru/skypro/Author.java @@ -0,0 +1,39 @@ +package ru.skypro; + +import java.util.Objects; + +//@AllArgsConstructor +//@Getter +public class Author { + private String firstName; + private String lastName; + + public Author(final String firstName,final String lastName) { + this.firstName=firstName; + this.lastName=lastName; + } + + public String getFirstName(){ + return this.firstName; + } + public String getLastName(){ + return this.lastName; + } + @Override + public String toString() { + return this.getFirstName() + ' ' + this.getLastName(); + } +// Alt+Insert generated + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Author author = (Author) o; + return getFirstName().equals(author.getFirstName()) && getLastName().equals(author.getLastName()); + } + + @Override + public int hashCode() { + return Objects.hash(getFirstName(), getLastName()); + } +} diff --git a/src/ru/skypro/Book.java b/src/ru/skypro/Book.java new file mode 100644 index 0000000..c4f67a5 --- /dev/null +++ b/src/ru/skypro/Book.java @@ -0,0 +1,48 @@ +package ru.skypro; + +import java.util.Objects; + +//@AllArgsConstructor +//@Getter +public class Book { + private String name; + private Author author; + //@Setter + private int publicationYear; + + public Book(final String name,final Author author,final int publicationYear) { + this.name=name; + this.publicationYear=publicationYear; + this.author=author; + } + public String getName() { + return this.name; + } + public Author getAuthor() { + return this.author; + } + public int getPublicationYear() { + return this.publicationYear; + } + public void setPublicationYear(final int publicationYear) { + this.publicationYear = publicationYear; + } + @Override + public String toString() { + return this.getName() + " " + this.getAuthor() + ' ' + this.getPublicationYear(); + } + // Alt+Insert generated + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Book book = (Book) o; + return getPublicationYear() == book.getPublicationYear() && getName().equals(book.getName()) && getAuthor().equals(book.getAuthor()); + } + + @Override + public int hashCode() { + return Objects.hash(getName(), getAuthor(), getPublicationYear()); + } + +} diff --git a/src/ru/skypro/Main.java b/src/ru/skypro/Main.java index 625884e..664541c 100644 --- a/src/ru/skypro/Main.java +++ b/src/ru/skypro/Main.java @@ -1,7 +1,14 @@ package ru.skypro; public class Main { - public static void main(String[] args){ + public static void main(String[] args){ + var author1 = new Author("Ivan","Ivanov"); + var author2 = new Author("Петя","Петров"); + var book1 = new Book("ivanovskaya",author1,2013); + var book2 = new Book("Петровская книга",author1,1999); + book2.setPublicationYear(2015); + System.out.println(book1); + System.out.println(book2); } }