/**
 * a being who breathes and pays for it
 */
public class Breather {
  
  /**
   * where I breathe
   */
  private Chamber chamber;
  
  /**
   * what I'm called
   */
  private String name;
  
  /**
   * how much I breathe per day
   */
  private int dailyBreath;
  
  /**
   * total assets
   */
  private double assets;
  
  /**
   * create breather name with capacity breath
   */
  public Breather(String name, int breath) {
    this.name= name;
    dailyBreath= breath;
  }
  
  /** 
   * create breather name, with capacity breath, and money assets
   */
  public Breather(String name, int breath, double assets) {
    this.name= name;
    dailyBreath= breath;
    this.assets= assets;
  }
  
  /**
   * create breather breatherString, which has format
   * "name, capacity, money"
   */
  public Breather(String breatherString) {
    name= breatherString.substring(0, breatherString.indexOf(','));
    dailyBreath=
      Integer.parseInt(breatherString.substring(breatherString.indexOf(',') + 2,
                                                breatherString.lastIndexOf(',')));
    assets=
      Double.parseDouble(breatherString.substring(breatherString.lastIndexOf(',') + 2));
  }
  
  /**
   * create breather name, with capacity breath, money assets, and chamber c
   */
  public Breather(String name, int breath, double assets, Chamber c) {
    this.name= name;
    dailyBreath= breath;
    this.assets= assets;
    chamber= c;
  }
                         
  
  /**
   * buy as much air as I can.
   */
  public void buyAir() {
    int newAir= chamber.getNearestAirFillShop().sellAir(assets);
    chamber.adjustAirBy(newAir);
    assets= 0;
  }
  
  /**
   * breathe for a day
   */
  public void breathe() {
    chamber.adjustAirBy(-dailyBreath);
  }
  
  /**
   * is there enough air to breathe?
   */
  public boolean canBreathe() {
    // return whether you've got a chamber with enough air
    return chamber != null &&
      chamber.getAvailableAir() >= dailyBreath;
  }
  
  /**
   * increase our money
   */
  public void earnMoney(double moreBucks) {
    assets += moreBucks;
  }
  
  /**
   * move to another chamber
   */
  public void move(Chamber newChamber) {
    chamber= newChamber;
  }
  
  /**
   * Describe this breather in an informative String
   */
  public String toString() {
    return name + ", breathes at " + chamber.getAddress() + 
      ", has $" + assets + ", can breathe: " + canBreathe() + ".";
  }
}
