ErosionData.java [src/java/util] Revision: default  Date:
package util;

import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author sidereus
 */
public class ErosionData {

  Integer year = null;
  Integer secondYear = null;
  List<Double> waterErosion;


  public ErosionData() {
    waterErosion = new LinkedList<>();
  }


  public void add(double waterErosion) {
    this.waterErosion.add(waterErosion);
  }


  public void setYear(int year) {
    this.year = year;
    if (this.year % 2 == 0) { // even
      this.year = this.year - 1;
      secondYear = this.year + 1;
    } else { // odd
      secondYear = this.year + 1;
    }
  }


  public int getYear() {
    if (year == null) {
      year = 1;
      secondYear = 2;
    }
    return year;
  }


  public int getSecondYear() {
    return secondYear;
  }


  private void checkYear() {
    if (year == null) {
      String msg = "Year is null, need to set it first";
      throw new NullPointerException(msg);
    }
  }


  private double average(List<Double> list) {
    double val1 = list.get(year - 1);
    double val2 = list.get(secondYear - 1);
    return (val1 + val2) / 2;
  }
  
  private double erosionCrop2(List<Double> list) {
    double val = list.get(secondYear - 1);
    return val;
  }

  
  public double getWaterErosion() {
    checkYear();
    return average(waterErosion);
  }
  
  public double getWaterErosionCrop2() {
    checkYear();
    return erosionCrop2(waterErosion);
  }

}