package caseStudy;

public class Gauge extends SaveAsXML {
    // Describes the gauge (# of stitches and rows) needed to obtain the desired sample size (inches or cm)
    private SaveableField <Double> width;
    private SaveableField <Double> length;
    private SaveableField <Integer> stitches;
    private SaveableField <Integer> rows;
    
    // XML Tags
    public static final String STARTCLASSTAG = "<Gauge>";
    public static final String ENDCLASSTAG = "</Gauge>";
    
    // Constructors
    public Gauge() {
        initializeFields();
        width.setValue(0.0);
        length.setValue(0.0);
        stitches.setValue(0);
        rows.setValue(0);
    }
    public Gauge(Gauge g) {
        initializeFields();
        width.setValue(g.getWidth());
        length.setValue(g.getLength());
        stitches.setValue(g.getNumStitches());
        rows.setValue(g.getNumRows());
    }
    public Gauge(double width, double length, int stitches, int rows) {
        initializeFields();
        this.width.setValue(width);
        this.length.setValue(length);
        this.stitches.setValue(stitches);
        this.rows.setValue(rows);
    }
    public Gauge(String xml) {
        initializeFields();
        this.load(xml);
    }
    
    // Accessors
    public double getWidth() {
        return width.getValue();
    }
    public double getLength() {
        return length.getValue();
    }
    public int getNumStitches() {
        return stitches.getValue();
    }
    public int getNumRows() {
        return rows.getValue();
    }
    public String save() {
        return STARTCLASSTAG
                + width.save()
                + length.save()
                + stitches.save()
                + rows.save()
                + ENDCLASSTAG;
    }
    public String toString() {
        return "To obtain gauge, use hook needed to make a " + width.getValue()
            + "\" x " + length.getValue() + "\" sample of " + stitches.getValue()
            + " stitches and " + rows.getValue() + " rows.";
    }
    public boolean equals(Gauge g) {
        if((width.getValue() == g.getWidth()) 
                && (length.getValue() == g.getLength())
                && (stitches.getValue() == g.getNumStitches())
                && (rows.getValue() == g.getNumRows())) {
            return true;
        }
        return false;
    }
    
    // Modifiers
    public void setWidth(double newWidth) {
        width.setValue(newWidth);
    }
    public void setLength(double newLength) {
        length.setValue(newLength);
    }
    public void setNumStitches(int newStitches) {
        stitches.setValue(newStitches);
    }
    public void setNumRows(int newRows) {
        rows.setValue(newRows);
    }
    public void load(String xml) {
        width.setValue(Double.parseDouble(width.getStringDataFromXML(xml)));
        length.setValue(Double.parseDouble(length.getStringDataFromXML(xml)));
        stitches.setValue(Integer.parseInt(stitches.getStringDataFromXML(xml)));
        rows.setValue(Integer.parseInt(rows.getStringDataFromXML(xml)));
    }
    private void initializeFields() {
        width = new SaveableField <Double> ("width");
        length = new SaveableField <Double> ("length");
        stitches = new SaveableField <Integer> ("stitches");
        rows = new SaveableField <Integer> ("rows");
    }
}
