package book;

import java.util.Comparator;
import java.util.Objects;

public class Book implements Comparable<Book> {
	private String author;
	private String title;
	private int price;

	public Book(String author, String title, int price) {
		super();
		this.author = author;
		this.title = title;
		this.price = price;
	}

	@Override
	public String toString() {
		return "Book [author=" + author + ", title=" + title + ", price=" + price + "]";
	}

	@Override
	public int hashCode() {
		return Objects.hash(author, title);
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		return Objects.equals(author, other.author) && Objects.equals(title, other.title);
	}

	@Override
	public int compareTo(Book other) {
		int bookAuthorComp = this.author.compareTo(other.author);

		if (bookAuthorComp == 0)
			return this.author.compareTo(this.title);

		return bookAuthorComp;
	}

	public int compareByPrice(Book other) {
		if (this.price > other.price)
			return 1;
		else if (this.price < other.price)
			return -1;
		return 0;
	}

	public static class PriceComparator implements Comparator<Book> {
		@Override
		public int compare(Book o1, Book o2) {
			return Integer.compare(o1.price, o2.price);
		}
	
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

}