package seatrading.main;

/**
 * @author Daniel "3ICE" Berezvai
 */
public class Goods {

  private String name;
  private double weight;
  private int price;
  private static final String[] TradeGoods = {
    "Coffee", "Corn", "Maize", "Oats", "Rice", "Soybeans", "Wheat",
    "Beef", "Cattle", "Eggs", "Pork",
    "Butter", "Cocoa", "Orange juice", "Sugar",
    "Aluminum", "Copper", "Ferrous scrap", "Lead", "Nickel",
    "Gold", "Plastic", "Platinum", "Silver"
  };

  public Goods(String name, double weight) {
    this.name = name;
    this.weight = weight;
    price = 1 + SeaTrading.rnd.nextInt(10);
    //System.out.println("Created " + toString());
  }

  public Goods(String name, double weight, int price) {
    this.name = name;
    this.weight = weight;
    this.price = price;
    //System.out.println("Goods with " + toString() + " created.");
  }

  /**
   * Hides weight behind a getter.
   *
   * @return the weight
   * @see addWeight()
   * @see rmWeight()
   */
  public double getWeight() {
    return weight;
  }

  /**
   * Adds instead of sets weight.
   *
   * @param weight
   */
  public void addWeight(double weight) {
    this.weight += weight;
  }

  /**
   * Carefully removes as much weight as possible, but doesn't underflow.
   *
   * @param weight the amount to try and subtract.
   * @return how much we actually managed to subtract
   */
  public double rmWeight(double weight) {
    if (this.weight >= weight) {//We can subtract...
      this.weight -= weight;//So we subtract.
      return weight;
    } else {
      //We need to return how much we actually managed to subtract.
      double tmp = SeaTrading.round(this.weight);
      this.weight = 0;
      return tmp;
    }
  }

  public String getName() {
    return name;
  }

  /**
   * Picks a random name from a internal list.
   *
   * @return a random tradegood name
   */
  public static String rndName() {
    return TradeGoods[SeaTrading.rnd.nextInt(TradeGoods.length)];
  }

  public int getPrice() {
    return price;
  }

  /**
   * Human-readable object printing.
   *
   * @return human-readable representation of the Goods object.
   */
  public String toString() {
    return "Goods{name:\"" + name + "\", weight:" + weight + ", price:" + price + "}";
  }
}
