package cn.vscoder.experiment_3.reader;

import cn.vscoder.experiment_3.common.PayException;
import cn.vscoder.experiment_3.common.Promotion;

public class VIPReader extends Reader implements Promotion {
    private String readerGrade;//读者级别
    private double percent;//折扣率
    private int bonusPoints;//积分

    public VIPReader(String readerName, String readerGrade) {
        super(readerName);
        this.readerGrade = readerGrade;
        setPercent();//设置折扣率
        bonusPoints = 0;
    }

    public String getReaderGrade() {
        return readerGrade;
    }

    public void setReaderGrade(String readerGrade) {
        this.readerGrade = readerGrade;
    }

    public double getPercent() {
        return percent;
    }

    public void setPercent() {
        if (readerGrade.equals("VIP")) {
            this.percent = 0.8;//8折

        } else if (readerGrade.equals("普通会员")) {
            this.percent = 0.9;//9折
        } else {//非会员
            this.percent = 1;//不打折
        }
    }

    public boolean payRent(double money) throws PayException {//支付
//        if (this.getBalance() >= money) {
//            this.balance = this.balance - money;
//            return true;
//        } else {
//            System.out.println("余额不足，请及时充值！");
//            return false;
//        }
        if (this.balance < money) {
            throw new PayException();     //当余额不足支付租金时抛出支付异常
        } else {
            balance = balance - money;
            return true;
        }
    }

    public int getBonusPoints() {
        return bonusPoints;
    }

    public void setBonusPoints(int bonusPoints) {//积分
        this.bonusPoints += bonusPoints;
    }

    @Override
    public String toString() {
        return super.toString() +
                ",读者级别='" + readerGrade + '\'' +
                ", 折扣率=" + percent +
                ", 积分=" + bonusPoints;
    }

    @Override
    public void promotion(String level)          //实现接口中的抽象方法
    {
        if (this.getBonusPoints() >= promtpoints)   //如果会员积分超过晋级标准
        {
            switch (level) {
                case "VIP":
                    this.balance += 10;
                    break;     //对VIP会员，1000积分返利10元
                case "普通会员":
                    this.readerGrade = "VIP";
                    break; //对普通会员，达到1000积分则晋级为VIP会员
                case "非会员":
                    this.readerGrade = "普通会员";
                    break;//非会员达到1000积分则晋级为普通会员
            }
            bonusPoints -= promtpoints;    //晋级后，会员积分-晋级标准分
        } else
            readerGrade = level;  //积分不达标，身份不变
        setPercent();               //调用设置折扣的方法
    }
}
