package kr.co.kihyun.beans.entity;

import kr.co.kihyun.lang.MString;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class AcceptanceCode {
    @SuppressWarnings("unused")
    private static final Logger LOG = LoggerFactory.getLogger(AcceptanceCode.class);

    private String deptName;

    private Integer number;

    public AcceptanceCode(String deptName, Integer number) {
        this.setDeptName(deptName);
        this.setNumber(number);
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public String getDeptName() {
        return deptName;
    }

    public final Integer getNumber() {
        return number;
    }

    public final void setNumber(Integer number) {
        this.number = number;
    }

    public static AcceptanceCode valueOf(String codeString) throws IllegalArgumentException {

        //44.적절하지 않은 예외처리(광범위예외클래스)_CWE-754 : Update by YOUNGJUN,CHO
        try {
            String deptName = codeString.split("_")[0];
            int number = Integer.parseInt(codeString.split("_")[1]);
            return new AcceptanceCode(deptName, number);
        } catch (NumberFormatException nfex) {
            throw new NumberFormatException();
        } catch (Exception e) {
            throw new IllegalArgumentException("\""+codeString+"\" is not a valid acceptance code.");
        }
        //================================================
    }

    @Override
    public String toString() {
        if(MString.isNull(this.getDeptName()) && this.getNumber() == null)
            return "";
        return MString.checkNull(this.getDeptName())+"_"+MString.checkNull(String.valueOf(this.getNumber()));
    }
}
