⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 renovationproject.java

📁 Java the UML Way 书中所有源码
💻 JAVA
字号:
/*
 * RenovationProject.java   E.L. 2001-08-15
 *
 * This class maintains a register with surfaces and different types of material.
 * The class works with references to the objects all the time. No copies of
 * objects are created.
 * A client may add new references to the register, or it may retrieve a
 * reference to an object. In this way it may change the content of the object
 * in the register, if the object belongs to a mutable class.
 */

import java.util.ArrayList;
class RenovationProject {
  private String name;
  private ArrayList allSurfaces = new ArrayList();
  private ArrayList allMaterials = new ArrayList();

  public RenovationProject(String initName) {
    name = initName;
  }

  public String getName() {
    return name;
  }

  /*
   * This method adds a new surface to the register. If a surface
   * with this name already exist, a reference to this object is returned,
   * if not a reference to the newly added object is returned.
   */
  public Surface addNewSurface(Surface newSurface) {
    Surface thisSurface = getSurface(newSurface.getName());
    if (thisSurface == null) {
      allSurfaces.add(newSurface);
      Material thisMaterial = newSurface.getMaterial();
      if (thisMaterial != null) addNewMaterial(thisMaterial);
      return newSurface;
    }
    else return thisSurface;  // surface with this name already registered
  }

  /*
   * This method searches for a surface with a given name.
   * If not found, the method returns null.
   */
  public Surface getSurface(String nameOfSsurface) {
    for (int i = 0; i < allSurfaces.size(); i++) {
      Surface thisSurface = (Surface) allSurfaces.get(i);
      if ((thisSurface.getName()).equals(nameOfSsurface)) return thisSurface;
    }
    return null;
  }

  /* This method is constructed in the same way as addNewSurface() */
  public Material addNewMaterial(Material newMaterial) {
    Material thisMaterial = getMaterial(newMaterial.getName());
    if (thisMaterial == null) {
      allMaterials.add(newMaterial);
      return newMaterial;
    }
    else return thisMaterial;
  }

  /* This method is constructed in the same way as getSurface() */
  public Material getMaterial(String nameOfMaterial) {
    for (int i = 0; i < allMaterials.size(); i++) {
      Material thisMaterial = (Material) allMaterials.get(i);
      if ((thisMaterial.getName()).equals(nameOfMaterial)) return thisMaterial;
    }
    return null;
  }

  /* This method calculates the total price of the project */
  public double getTotalPrice() {
    double totalPrice = 0.0;
    for (int i = 0; i < allSurfaces.size(); i++) {
      Surface theSurface = (Surface) allSurfaces.get(i);
      Material material = theSurface.getMaterial();
      if (material != null) totalPrice += material.getTotalPrice(theSurface);
    }
    return totalPrice;
  }

  /* The following methods may be used to run through all the materials
   * or all the surfaces in a sequential way.
   */
  public int getNoOfMaterials() {
    return allMaterials.size();
  }

  public Material getMaterial(int index){
    if (index >= 0 && index < allMaterials.size()) {
      return (Material) allMaterials.get(index);
    }
    else return null;
  }

  public int getNoOfSurfaces() {
    return allSurfaces.size();
  }

  public Surface getSurface(int index){
    if (index >= 0 && index < allSurfaces.size()) {
      return (Surface) allSurfaces.get(index);
    }
    else return null;
  }
}

⌨️ 快捷键说明

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