flooring.java

来自「Java the UML Way 书中所有源码」· Java 代码 · 共 50 行

JAVA
50
字号
/*
 * Flooring.java   E.L. 2001-05-17
 *
 */
class Flooring {
  private static final double limit = 0.02; // limit for one more width

  private String name;  // for identification purposes
  private double price;  // price per meter
  private double widthOfFlooring;    // meter

  public Flooring(String initName, double initPrice, double initWidth) {
    name = initName;
    price = initPrice;
    widthOfFlooring = initWidth;
  }

  public String getName() {
    return name;
  }

  public double getPricePerM() {
    return price;
  }

  public double getWidth() {
    return widthOfFlooring;
  }

  /*
   * We are going to calculate the amount which is needed to cover one surface.
   * The flooring is always placed crosswise relative to the length of the surface.
   * If you want to find the amount the other way, you have to change
   * width and length in the surface argument.
   */
  public double getNoOfMeters(Surface aSurface) {
    double lengthSurface = aSurface.getLength();
    double widthSurface = aSurface.getWidth();

    int noOfWidths = (int)(lengthSurface / widthOfFlooring);
    double rest = lengthSurface % widthOfFlooring;
    if (rest >= limit) noOfWidths++;
    return noOfWidths * widthSurface;
  }

  public double getTotalPrice(Surface aSurface) {
    return getNoOfMeters(aSurface) * price;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?