package cn.com.qmth.stmms.biz.utils;

import java.text.DecimalFormat;

public class ScoreItem {

    public static final String SPLIT = ":";

    public static final DecimalFormat NUMBER_FORMAT = new DecimalFormat("###.#");

    private int mainNumber;

    private int subNumber;

    private String title;

    private double score;

    private String answer;

    private boolean objective;

    public ScoreItem(boolean objective) {
        this.objective = objective;
    }

    public int getMainNumber() {
        return mainNumber;
    }

    public void setMainNumber(int mainNumber) {
        this.mainNumber = mainNumber;
    }

    public int getSubNumber() {
        return subNumber;
    }

    public void setSubNumber(int subNumber) {
        this.subNumber = subNumber;
    }

    public String getTitle() {
        return title;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public boolean isObjective() {
        return objective;
    }

    public void setObjective(boolean objective) {
        this.objective = objective;
    }

    public String toString() {
        return objective ? answer + SPLIT + getScoreString() : getScoreString();
    }

    public static ScoreItem parse(String content, boolean objective) {
        ScoreItem item = new ScoreItem(objective);
        try {
            if (objective) {
                String[] values = content.split(SPLIT);
                item.setAnswer(values[0]);
                item.setScore(Double.valueOf(values[1]));
            } else {
                item.setScore(Double.valueOf(content));
            }
        } catch (Exception e) {
            item = null;
        }
        return item;
    }

    public String getScoreString() {
        try {
            return NUMBER_FORMAT.format(score);
        } catch (Exception e) {
            return "";
        }
    }
}
