package cn.vscoder.experiment_3.rent;

/* 第3-6题是RentBookManage修改后的图书租赁信息管理类 */


import cn.vscoder.experiment_3.book.RentBook;                  // 引入book包中的RentBook类
import cn.vscoder.experiment_3.reader.VIPReader;            // 引入reader包中的VIPReader类
import cn.vscoder.experiment_3.common.PayException;            // 引入common包中的PayException异常类
import java.util.*;                    // 导入常用集合类，如LinkedList和ArrayList

public class RentBookManagenew {       // 类定义，使用public修饰

   /* 1. 定义所需的成员变量 */
	static final int deadTime = 10;     // 静态常量：规定租期上限天数，final修饰不可修改
	double normalRent = 0.1;            // 实例变量：正常租金费率
	double delayRent = 0.4;             // 实例变量：逾期租金费率

	VIPReader renter;                // 当前还书的会员读者对象
	RentRecord rentrecord;             // 当前操作的租赁记录对象

	public LinkedList<RentBook> booklist;               // 图书列表，保存所有可租图书
	public ArrayList<VIPReader> readerlist;          // 读者列表，保存所有会员读者
	public ArrayList<RentRecord> rentlist;              // 租借记录列表
	public ArrayList<String> returnlist;                // 还书记录信息列表

	int j;  // 循环索引变量

	/* 2. 无参构造函数 */
	public RentBookManagenew() {
		booklist = new LinkedList<>();
		readerlist = new ArrayList<>();
		rentlist = new ArrayList<>();
		returnlist = new ArrayList<>();
	}

    /* 3. 相关getter/setter方法 */

	public static double getDeadTime() {
		return deadTime;
	}

	public void setNormalRent(double newNR) {
		normalRent = newNR;
	}

	public double getNormalRent() {
		return normalRent;
	}

	public void setDelayRent(double newNR) {
		delayRent = newNR;
	}

	public double getDelayRent() {
		return delayRent;
	}

   /* 4. 图书信息操作相关方法 */

	public void addBook(String isbn, String bname, String bauthor, String bpublisher, double bprice, String no) {
		booklist.add(new RentBook(isbn, bname, bauthor, bpublisher, bprice, no));
	}

	public void addBook(int i, String isbn, String bname, String bauthor, String bpublisher, double bprice, String no) {
		booklist.add(i, new RentBook(isbn, bname, bauthor, bpublisher, bprice, no));
	}

	public void searchBook(String bookNo) {
		boolean flag = false;
		for (j = 0; j < booklist.size(); j++) {
			if (booklist.get(j).getBookNo().equals(bookNo)) {
				System.out.println(booklist.get(j));
				flag = true;
			}
		}
		if (!flag)
			System.out.println("未找到指定图书。");
	}

	public void editBook(String isbn, double bprice) {
		for (j = 0; j < booklist.size(); j++) {
			if (booklist.get(j).getISBN().equals(isbn)) {
				booklist.get(j).setPrice(bprice);
				System.out.println(booklist.get(j));
			}
		}
	}

	public void deleteBook(String isbn) {
		for (j = 0; j < booklist.size(); j++) {
			if (booklist.get(j).getISBN().equals(isbn)) {
				booklist.remove(j);
				System.out.println("成功删除图书。");
			}
		}
	}

	public void displayBook() {
		for (RentBook b : booklist)
			System.out.println(b);
	}

	/* 5. 读者信息添加与展示 */

	public void addReader(String name, String grade) {
		readerlist.add(new VIPReader(name, grade));
	}

	public void displayReader() {
		for (VIPReader r : readerlist)
			System.out.println(r);
	}

	/* 6. 与租赁业务相关的方法 */

	public void addRentRecord(RentBook bk, VIPReader rd, String rdate) {
		if (rentlist.size() == 0) {
			rentlist.add(new RentRecord(bk, rd, rdate));
		} else {
			boolean flag = true;
			for (j = 0; j < rentlist.size(); j++) {
				if (rentlist.get(j).getBookNo().equals(bk.getBookNo())) {
					System.out.println(rentlist.get(j).getBookNo() + "  已被租借");
					flag = false;
				}
			}
			if (flag) {
				rentlist.add(new RentRecord(bk, rd, rdate));
			}
		}
	}

	public void returnBook(RentBook bk, VIPReader rd, String redate) {
		for (j = 0; j < rentlist.size(); j++) {
			if (rentlist.get(j).getBookNo().equals(bk.getBookNo())) {
				renter = rd;
				rentlist.get(j).setReturnDate(redate);
				rentlist.get(j).setRent(deadTime, normalRent, delayRent, renter.getPercent());
				rentlist.get(j).setBonusPoints();
				renter.setBonusPoints(rentlist.get(j).getBonusPoints());
				renter.promotion(renter.getReaderGrade());

				if (renting()) {
					returnlist.add("\n" + renter.getReaderName() + " | " + renter.getReaderGrade() + " 会员账户余额=" + renter.getBalance()
							+ " 当前积分=" + renter.getBonusPoints() + "\n借阅图书信息: " + rentlist.get(j).toString());
					rentlist.remove(j);
				}
			}
		}
	}

	public boolean renting() {
		try {
			renter.payRent(rentlist.get(j).getRent());
			System.out.println(renter.getReaderName() + " 支付租金：" + rentlist.get(j).getRent() + " 成功！");
			return true;
		} catch (PayException pe) {
			System.out.println(renter.getReaderName() + pe);
			return false;
		}
	}

	public void displayRentInfo() {
		for (RentRecord br : rentlist)
			System.out.println(br);
	}

	public void displayReturnInfo() {
		for (String bre : returnlist)
			System.out.println(bre);
	}
}