The Book class

Once you have created a Gradle Java project in IntelliJ, you are ready to start developing the MyBooksApp. Our first step is to make a simple Book class!

package model;
import java.util.Objects;
public class Book {
private String title;
private String isbn;
private String publisher;
private int year;
private Author author;
public Book(String title, String isbn, String publisher, int year, Author author) {
this.title = title;
this.isbn = isbn;
this.publisher = publisher;
this.year = year;
this.author = author;
}
public String getTitle() {
return title;
}
public String getIsbn() {
return isbn;
}
public String getPublisher() {
return publisher;
}
public int getYear() {
return year;
}
public Author getAuthor() {
return author;
}
public void setTitle(String title) {
this.title = title;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public void setYear(int year) {
this.year = year;
}
public void setAuthor(Author author) {
this.author = author;
}
@Override
public String toString() {
return "Book{
title='" + title + '\'' +
", isbn='" + isbn + '\'' +
", publisher='" + publisher + '\'' +
", year=" + year +
", author=" + author.toString()
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return year == book.year &&
title.equals(book.title) &&
isbn.equals(book.isbn) &&
publisher.equals(book.publisher) &&
author.equals(book.author);
}
@Override
public int hashCode() {
return Objects.hash(title, isbn, publisher, year, author);
}
}

Notice Book has a number of fields: title, isbn, publisher, year, and author. All the other instance member methods are generated automatically by IntelliJ (custom constructor, setters & getters, toString, equals and hashCode).

You should store the Book class in a package called model.

Java Packages

A package in Java is used to group related classes much like a folder in a file directory. By convention, package names are written in lower case.

Keep in mind that Gradle is opinionated about how your project must be structured. Although you can change the default structure, we advise that you keep on to it. You must, therefore, place your source code in src/main/java and tests in src/test/java.

.
โ”œโ”€โ”€ build.gradle
โ”œโ”€โ”€ gradle
โ”œโ”€โ”€ settings.gradle
โ””โ”€โ”€ src
โ”œโ”€โ”€ main
โ”‚ โ””โ”€โ”€ java
โ”‚ โ””โ”€โ”€ model
โ”‚ โ””โ”€โ”€ Book.java
โ””โ”€โ”€ test
โ””โ”€โ”€ java
โ””โ”€โ”€ model
โ””โ”€โ”€ BookTest.java