Here is code for class Book. In toString
method there is String.format
method which makes readable string. Nothing more special.
When equals
method is overridden then hashCode
method should be too. They both compare objects if they are equals. You can find it more for example from here.
public class Book {
private String title;
private String author;
private int pages;
private String publisher;
private int year;
public Book(String title, String author, int pages, String publisher, int year) {
this.title = title;
this.author = author;
this.pages = pages;
this.publisher = publisher;
this.year = year;
}
public Book(Book book) {
this.title = book.title;
this.author = book.author;
this.pages = book.pages;
this.publisher = book.publisher;
this.year = book.year;
}
public String getTitle() {
return title;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Book)) {
return false;
}
Book other = (Book) o;
if (this.title.equals(other.title) &&
this.author.equals(other.author) &&
this.publisher.equals(other.publisher) &&
this.pages == other.pages && this.year == other.year) {
return true;
}
return false;
}
@Override
public String toString() {
return String.format("%-10s %s\n", "Title:", title) +
String.format("%-10s %s\n", "Author:", author) +
String.format("%-10s %d\n", "Pages:", pages) +
String.format("%-10s %s\n", "Publisher:", publisher) +
String.format("%-10s %d", "Year:", year);
}
}
Here is main class which uses all of the methods.
public class Main {
public static void main(String[] args) {
Book first = new Book("The Book", "Famous Writer", 199, "Big Book Publisher", 1984);
Book second = new Book("The Second Book", "Less F. Writer", 249, "The Book Publishers", 1999);
System.out.println("First book");
System.out.println(first + "\n");
System.out.println("Second book");
System.out.println(second + "\n");
System.out.println("Title of the first book: " + first.getTitle() + "\n");
Book firstCopy = new Book(first);
System.out.println("Copy of the first book:");
System.out.println(firstCopy + "\n");
System.out.println("Copy equals first book: " + firstCopy.equals(first));
System.out.println("Second book equals first book: " + second.equals(first));
System.out.println();
System.out.println("Change writer of the copy to :'Most F. Writer'");
firstCopy.setAuthor("Most F. Writer");
System.out.println(firstCopy + "\n");
System.out.println("Copy equals first book: " + firstCopy.equals(first));
}
}
When executed main class outputs this:
First book
Title: The Book
Author: Famous Writer
Pages: 199
Publisher: Big Book Publisher
Year: 1984
Second book
Title: The Second Book
Author: Less F. Writer
Pages: 249
Publisher: The Book Publishers
Year: 1999
Title of the first book: The Book
Copy of the first book:
Title: The Book
Author: Famous Writer
Pages: 199
Publisher: Big Book Publisher
Year: 1984
Copy equals first book: true
Second book equals first book: false
Change writer of the copy to :'Most F. Writer'
Title: The Book
Author: Most F. Writer
Pages: 199
Publisher: Big Book Publisher
Year: 1984
Copy equals first book: false