package com.sf._04_set_contact.demo1;

import java.util.HashSet;
import java.util.Iterator;

public class BorrowManager {
    // 存储借阅记录信息
    static HashSet<BorrowRecord> recordHashSet = new HashSet<>();


    /**
     * 添加借阅记录信息到hashSet BorrowRecord
     * 参数: 解决记录信息
     */
    public static void addBorrowRecord(BorrowRecord borrowRecord){
        //找到我们要借阅书籍
        Book book = borrowRecord.getBook();   //红楼梦
        // 遍历recordHashSet
        //("zhangsan",book(西游记),"2026/1/13") , ("lisi",book1(三国演义),"2026/1/13")
        boolean flag = false;
        for (BorrowRecord record : recordHashSet) {
            // 获取已经借阅书籍
            Book borrowBook = record.getBook();
            // 判断要借的书 和已经借阅的书是否相同
            if(borrowBook.equals(book)){
                // 进来表示这本书已经被被人借走了
                flag = true;
                break;
            }
        }
        // flag =true 表示被接走  如果flag = false 表示书没被借走
        if(flag){
            System.out.println("书已经被借走了");
        }else{
            // 把他添加到set 集合当中
            System.out.println("借阅成功-借阅人:" + borrowRecord.getBorrower());
            recordHashSet.add(borrowRecord);
        }
    }

    /**
     * 根据借阅人姓名查询他的借阅记录信息
     * 1 可能最开始还没人借书籍recoredSet 是一个空
     * 2 这个人他可能没有借书
     */
    public static void queryBorrowRecordByName(String name) {
        // 当set .size 大于0 才进行遍历
        if (recordHashSet.size() > 0) {
            boolean flag = false;
            for (BorrowRecord borrowRecord : recordHashSet) {
                // 比较name 和 借阅记录name 进行比较
                if (borrowRecord.getBorrower().equals(name)) {
                    flag = true;
                    System.out.println("借阅人:"+ borrowRecord.getBorrower() +"借阅书籍:"+ borrowRecord.getBook().getName());
                    break;
                }
            }
            if(!flag){
                System.out.println("借阅人没有借阅书籍");
            }
        }else{
            System.out.println("当前没有人借阅书籍记录为空");
        }

    }

    /**
     * 查询借阅记录信息
     */
    public static void queryBorrowRecords(){
        for (BorrowRecord borrowRecord : recordHashSet) {
            System.out.println("借阅人:"+ borrowRecord.getBorrower()+"借阅书"+ borrowRecord.getBook().getName());
        }
    }

    /**
     * 根据书籍isbn 编号删除所有相关记录
     * @param isbn
     */
    public static void removeRecordsByBookIsbn(String isbn){
        if(recordHashSet.size()>0){
            Iterator<BorrowRecord> iterator = recordHashSet.iterator();
            while (iterator.hasNext()){
                BorrowRecord borrowRecord = iterator.next();
                if(borrowRecord.getBook().getIsbn().equals(isbn)){
                    System.out.println("删除借阅记录借阅人:"+ borrowRecord.getBorrower()+"借阅书"+ borrowRecord.getBook().getName());
                    iterator.remove();
                }
            }
        }else{
            System.out.println("现在没有任何记录");
        }
    }

    public static void main(String[] args) {
        Book book = new Book("01","西游记","吴承恩");
        Book book1 = new Book("02" , "三国演义", "罗贯中");
        //创建借阅记录
        // 借阅人张三  借了 西游记 时间2026 1/13
        BorrowRecord borrowRecord = new BorrowRecord("zhangsan",book,"2026/1/13");
        BorrowRecord borrowRecord1 = new BorrowRecord("lisi",book1,"2026/1/13");
        BorrowRecord borrowRecord2 = new BorrowRecord("lisi",book,"2026/1/13");
        addBorrowRecord(borrowRecord);    //成功
        // 同一个人不能够重复借阅同一本书
        addBorrowRecord(borrowRecord1);   //成功
//        // 为什么这步也成功了
//        addBorrowRecord(borrowRecord2);
////        addBorrowRecord(borrowRecord2);   //失败
//        System.out.println("-----------------------------");
////        queryBorrowRecords();
//        queryBorrowRecordByName("zhangsan");
//        queryBorrowRecordByName("wangwu");
//
        removeRecordsByBookIsbn("01");
        queryBorrowRecords();

    }
}
