import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Book> books = new ArrayList<>();
while (true) {
System.out.println("Name (empty will stop):");
String name = scanner.nextLine();
if (name.isEmpty()) {
break;
}
if (books.contains(name)) {
System.out.println("The book is already on the list. Let's not add the same book again");
continue;
}
System.out.println("Publication year:");
int publicationYear = Integer.valueOf(scanner.nextLine());
Book book = new Book(name, publicationYear);
books.add(book);
}
// NB! Don't alter the line below!
System.out.println("Thank you! Books added: " + books.size());
}
}
public class Book {
private String name;
private int publicationYear;
public Book(String name, int publicationYear) {
this.name = name;
this.publicationYear = publicationYear;
}
public String getName() {
return name;
}
public int getPublicationYear() {
return publicationYear;
}
public boolean equals(Object compared) {
// if the variables are located in the same position. they are equal
if (this == compared) {
return true;
}
// if the compared object is not of type Book, the objects are nt equal
if (!(compared instanceof Book)) {
return false;
}
// convert the object to a book Object
Book comparedBook = (Book) compared;
// if the values of the variables are equal, the objects are equal
if (this.name.equals(comparedBook.name)) {
return true;
}
return false;
}
}