Grille.java 2.08 KB
package noyau;

import noyau.exception.CellNotFoundException;

import java.util.HashMap;
import java.util.Map;

public class Grille {

    private Map<String, Case> cases = new HashMap<>();

    public String createCase(String column, Integer line, Double value) {
        String id = this.getId(column, line);
        Case cell = new Case(column, line, value);

        this.cases.put(id, cell);

        return id;
    }

    public String createCase(String column, Integer line, Formule formula) {
        String id = this.getId(column, line);
        Case cell = new Case(column, line, formula);

        this.cases.put(id, cell);

        return id;
    }

    public void setValue(String column, Integer line, Double value) throws CellNotFoundException {
        Case cell = this.getCase(column, line);

        try {
            cell.setValue(value);
        } catch (NullPointerException exception) {
            throw new CellNotFoundException();
        }
    }

    public void setFormula(String column, Integer line, Formule formula) throws CellNotFoundException {
        Case cell = this.getCase(column, line);

        try {
            cell.setFormula(formula);
        } catch (NullPointerException exception) {
            throw new CellNotFoundException();
        }
    }

    public Case getCase(String column, Integer line) {
        return this.cases.get(this.getId(column, line));
    }

    public Double getValeur(String column, Integer line) throws CellNotFoundException {
        Case cell = this.getCase(column, line);

        try {
            return cell.getValue();
        } catch (NullPointerException exception) {
            throw new CellNotFoundException();
        }
    }

    public String getFormuleAsString(String column, Integer line) throws CellNotFoundException {
        Case cell = this.getCase(column, line);

        try {
            return cell.getValue();
        } catch (NullPointerException exception) {
            throw new CellNotFoundException();
        }
    }

    private String getId(String column, Integer line) {
        return column + line.toString();
    }
}