如何检查用户输入的容量是否大于当前容量?

时间:2015-04-07 22:04:02

标签: java constructor

我目前正在开发一个与狗一起工作的项目,我正在研究的当前方法应该让用户改变狗窝容量。下面的代码来自kennel构造函数类:

public class Kennel {
  private String name;
  private ArrayList < Dog > dogs;
  private int nextFreeLocation;
  private int capacity;

  /**
   * Creates a kennel with a default size 20
   *
   * @param maxNoDogs
   *            The capacity of the kennel
   */
  public Kennel() {
    this(20);
  }

  /**
   * Create a kennel
   *
   * @param maxNoDogs
   *            The capacity of the kennel
   */
  public Kennel(int maxNoDogs) {
    nextFreeLocation = 0; // no Dogs in collection at start
    capacity = maxNoDogs;
    dogs = new ArrayList < Dog > (capacity); // set up default. This can
    // actually be exceeded
    // when using ArrayList but we
    // won't allow that
    // to happen.
  }

  /**
   * This method sets the value for the name attribute. The purpose of the
   * attribute is: The name of the kennel e.g. "DogsRUs"
   *
   * @param theName
   */
  public void setName(String theName) {
    name = theName;
  }

  /**
   * Set the size of the kennel
   *
   * @param capacity
   *            The max dogs we can house
   * @return
   */

  public void setCapacity(int capacity) {
    if (this.capacity > capacity) {
      this.capacity = capacity;
    } else
      System.out.println("The capacity you entered is lower than the current capacity");
  }

  /**
   * Maximum capacity of the kennels
   *
   * @return The max size of the kennel
   */
  public int getCapacity() {
    return capacity;
  }

  /**
   * This method gets the value for the name attribute. The purpose of the
   * attribute is: The name of the Kennel e.g. "DogsRUs"
   *
   * @return String The name of the kennel
   */
  public String getName() {
    return name;
  }

  /**
   * This method returns the number of dogs in a kennel
   *
   * @return int Current number of dogs in the kennel
   */
  public int getNumOfDogs() {
    return nextFreeLocation;
  }

  /**
   * Enables a user to add a Dog to the Kennel
   *
   * @param theDog
   *            A new dog to home
   */
  public void addDog(Dog theDog) {
    if (nextFreeLocation >= capacity) {
      System.out.println("Sorry kennel is full - cannot add team");
      return;
    }
    // we add in the position indexed by nextFreeLocation
    // This starts at zero
    dogs.add(theDog);

    // now increment index ready for next one
    nextFreeLocation = nextFreeLocation + 1;
  }

  /**
   * Enables a user to delete a Dog from the Kennel.
   *
   * @param theDog
   *            The dog to remove
   */
  public void removeDog(String who) {
    Dog which = null;
    // Search for the dog by name
    for (Dog d: dogs) {
      if (who.equals(d.getName())) {
        which = d;
      }
    }
    if (which != null) {
      dogs.remove(which); // Requires that Dog has an equals method
      System.out.println("removed " + who);
      nextFreeLocation = nextFreeLocation - 1;
    } else
      System.err.println("cannot remove - not in kennel");
  }

  /**
   * @return String showing all the information in the kennel
   */
  public String toString() {
    StringBuilder sbr = new StringBuilder();
    sbr.append("Data in Kennel " + name + " is:");
    for (Dog d: dogs) {
      sbr.append(d.toString() + "\n");
    }
    return sbr.toString();
  }

  /**
   * Returns an array of the dogs in the kennels
   *
   * @return An array of the correct size
   */
  public Dog[] obtainAllDogs() {
    Dog[] result = new Dog[dogs.size()];
    result = dogs.toArray(result);
    return result;
  }

  /**
   * Only returns those dogs who like bones
   *
   * @return An array of dogs of the correct size. If no dogs like bones then
   *         returns an empty array (size 0)
   */
  public List < Dog > obtainDogsWhoLikeBones() {
    List < Dog > result = new ArrayList < Dog > (); // changed to List<dog> so can
    // freely add data without
    // intializing to fixed size
    for (Dog d: dogs) {
      if (d.getLikesBones()) {
        result.add(d);
      }
    }
    return result;
  }

  public Dog search(String name) {
    Dog result = null;
    for (Dog d: dogs) {
      if (name.equals(d.getName())) {
        result = d;
      }
    }
    return result;
  }

}

这个方法来自狗窝应用程序类:

private void setKennelCapacity() {
	System.out.print("Enter max number of dogs: ");
	int max = scan.nextInt();
	scan.nextLine();
	kennel.setCapacity(max);
	System.out.println(max + " " + kennel.getCapacity());
}
 

结果是运行了else语句,它总是说输入的数字太低了。这可能是一个小错误,但我没有经验,谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

usercapacity是一个局部变量。每次调用setCapacity(int)时,变量usercapacity都将初始化为0(该变量将始终为0)。而是创建一个字段变量来存储容量

class Kernal {
    private int capacity;

    public void setCapacity(int newCapacity) {
        if(newCapacity < capacity) {
            throw new IllegalArgumentException("You cannot enter a value that is lower than the current capacity");
        }

        capacity = newCapacity;
    }
}

如果没有IllegalArgumentException(使用简单的print语句通知用户),您的方法将如下所示:

public void setCapacity(int capacity) {
    if (this.capacity < capacity) {
        this.capacity = capacity;
    } else
        System.out.println("The capacity you entered is lower than the current capacity");
}