paint.java

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

JAVA
53
字号
/*
 * Paint.java   E.L. 2001-08-15
 *.
 */
class Paint {
  private static final double limit = 0.02;  // limit to buy 0.5 liters more
  private String name;  // idnetification
  private double price;  // price a liter
  private int noOfCoats;
  private double noOfSqMPerLiter;

  public Paint(String initName, double initPrice, int initNoOfCoats,
      double initNoOfSqMPerLiter) {
    name = initName;
    noOfCoats = initNoOfCoats;
    noOfSqMPerLiter = initNoOfSqMPerLiter;
    price = initPrice;
  }

  public String getName() {
    return name;
  }

  public double getPricePerLiter() {
    return price;
  }

  public int getNoOfCoats() {
    return noOfCoats;
  }

  public double getNoOfSqMPerLiter() {
    return noOfSqMPerLiter;
  }

  /*
   * This method calculates the amount of paint required to cover the surface.
   * The result is rounded upward to the nearest 0.5 liters.
   */
  public double getNoOfLiters(Surface aSurface) {
    double area = aSurface.getArea();
    double noOfLiters = area * noOfCoats / noOfSqMPerLiter;
    int noOfLitersInteger = (int) noOfLiters;
    double more = noOfLiters - noOfLitersInteger;
    if (more >= 0.5 + limit) return noOfLitersInteger + 1.0;
    else if (more >= limit) return noOfLitersInteger + 0.5;
    else return noOfLitersInteger;
  }

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

⌨️ 快捷键说明

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