package com.sf.day16.homework;

import java.util.HashSet;
import java.util.Set;

/**
 * **.现有若干图书信息（包含名称title、作者author、定价price）需要存储到set集合中，保证集合中无重复元素，并遍历查看。可以认为所有信息都相同的图书为重复数据。**
 */
public class Library {
    /**
     * 图书标题
     */
    private String title;
    /**
     * 图书作者
     */
    private String author;
    /**
     * 图书价格
     */
    private int price;


    public Library() {
    }

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

    public String getTitle() {
        return title;
    }

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

    public String getAuthor() {
        return author;
    }

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

    public int getPrice() {
        return price;
    }

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

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Library library = (Library) o;

        if (price != library.price) return false;
        if (title != null ? !title.equals(library.title) : library.title != null) return false;
        return author != null ? author.equals(library.author) : library.author == null;

    }

    @Override
    public int hashCode() {
        int result = title != null ? title.hashCode() : 0;
        result = 31 * result + (author != null ? author.hashCode() : 0);
        result = 31 * result + price;
        return result;
    }


    public static void main(String[] args) {
        Set<Library> set = new HashSet<>();

        Library library1 = new Library("语文","鲁迅",20);
        Library library2 = new Library("活着","余华",25);
        Library library3 = new Library("恶意","东野圭吾",18);
        Library library4 = new Library("活着","余华",25);

        set.add(library1);
        set.add(library2);
        set.add(library3);
        set.add(library4);

        for (Library library : set){
            System.out.println(library);
        }
    }
}
