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

📄 renovationchap12.java

📁 Java the UML Way 书中所有源码
💻 JAVA
字号:
/*
 * RenovationChap12.java   E.L. 2001-08-17
 *
 */
import java.text.*;
import javax.swing.JOptionPane;
import myLibrary.InputReader;  // see chap. 6.8
class ProjectChap12 {
  public static final int exit = -1;
  public static final int newPaint = -2;
  public static final int newFlooring = -3;
  public static final int newWallpaper = -4;

  private ReaderRenovationCase details = new ReaderRenovationCase();
  private RenovationProject project = new RenovationProject("My Flat");
  private DecimalFormat numberFormat = new DecimalFormat("###0.00"); // see ch.8.2
  private DecimalFormat currencyFormat =
                       (DecimalFormat) NumberFormat.getCurrencyInstance();

  public void showInstructions() {
    String instruction =
      "This program calculates the price and amount needed to renovate several surfaces\n" +
      "with different kinds of material. You may choose to change the type of material on\n" +
      "a registered surface, or to register a new surface. You have to register a new \n" +
      "surface as the first thing you do. Every time you do a change, the amount of\n " +
      "material and the price are shown in the list box.";
    JOptionPane.showMessageDialog(null, instruction);
  }

  public int doAChoice() {
    String[] options = setOptions();
    String selectionDone = (String) JOptionPane.showInputDialog(null,
      "Drop down the list, and do a choice, " +
      "\nif you click on a surface, you will get the opportunity to " +
      "\nchange the material for that surface:",
      "Renovation", JOptionPane.DEFAULT_OPTION, null, options, options[0]);
    int selection = getSelectionAsInt(options, selectionDone);
    if (selection == options.length - 1) return exit;
    else return selection;
  }

  public void carryOutTheRightThing(int option) {
    if (option < project.getNoOfSurfaces()) { // changes material
      Material theMaterial = getMaterial();
      Surface theSurface = project.getSurface(option); // get a reference to the surface object
      theSurface.setMaterial(theMaterial);
    } else {   // new surface is to be registered
      Surface aSurface = details.readAndInstantiateSurface();
      if (project.addNewSurface(aSurface) == aSurface) { // ok, the material should be found
        Material theMaterial = getMaterial();
        aSurface.setMaterial(theMaterial);
      } else {
        JOptionPane.showMessageDialog(null,
                         "Surface with this name is already registered.");
      }
    }
  }

  /*
   * This method create a String array to be presented to the user as a menu.
   * The array will contain all registered surfaces with their material and price,
   * and in addition the total price and the items "New Surface" and "Exit".
   */
  private String[] setOptions() {
    int noOfSurfaces = project.getNoOfSurfaces();
    int noOfOptions = noOfSurfaces + 3;
    String[] options = new String[noOfOptions];
    for (int i = 0; i < noOfSurfaces; i++) {
      Surface thisSurface = project.getSurface(i);
      Material theMaterial = thisSurface.getMaterial();
      if (theMaterial != null) {
        double noOfUnits = theMaterial.getMaterialReq(thisSurface);
        String noOfUnitsFormatted = numberFormat.format(noOfUnits);
        double price = theMaterial.getTotalPrice(thisSurface);
        String priceFormatted = currencyFormat.format(price);
        String classname = theMaterial.getClass().getName();
        options[i] = thisSurface.getName() + ", " + classname + ": " + theMaterial.getName() + ", "
                        + noOfUnitsFormatted + " units, price: " + priceFormatted;
      }
      else options[i] = thisSurface.getName() + ", material not registered.";
    }
    options[noOfSurfaces] = "The total price is: "
                            + currencyFormat.format(project.getTotalPrice());
    options[noOfSurfaces + 1] = "New Surface";
    options[noOfSurfaces + 2] = "Exit";
    return options;
  }

  /*
   * This method finds the correct material, or registers a new one.
   * In this case, the method returns null if a material with the
   * same name already is registered.
   */
  private Material getMaterial() {
    int materialIndex = getMaterialIndex();  // the user choose the right material
    Material aMaterial;
    if (materialIndex >= 0) {
      return project.getMaterial(materialIndex);  // RETURNs registered material.
    }
    else {  // we are going to register a new material
      if (materialIndex == newPaint) aMaterial = details.readAndInstantiatePaint();
      else if (materialIndex == newFlooring) aMaterial = details.readAndInstantiateFlooring();
      else aMaterial = details.readAndInstantiateWallpaper();
      Material theMaterial = project.addNewMaterial(aMaterial);
      if (theMaterial != aMaterial) {
        JOptionPane.showMessageDialog(null, "Material with this name is already registered.");
      }
    }
    return aMaterial;
  }

  /*
   * This method let the user choose between all existing materials, or she may
   * choose to register a new material. In the first case the method returns
   * the index of the choosen material, in the second case the method returns -1.
   */
  private int getMaterialIndex() {
    int noOfMaterials = project.getNoOfMaterials();
    int noOfOptions = noOfMaterials + 3;
    String[] options = new String[noOfOptions];
    for (int i = 0; i < noOfMaterials; i++) {
      Material thisMaterial = project.getMaterial(i);
      String classname = thisMaterial.getClass().getName();
      options[i] = classname + ": " + thisMaterial.getName();
    }
    options[noOfMaterials] = new String("New Paint");
    options[noOfMaterials + 1] = new String("New Flooring");
    options[noOfMaterials + 2] = new String("New Wallpaper");

    /* We show the menu list to the user, and we note his choice. */
    String selectionDone = (String) JOptionPane.showInputDialog(null,
      "Drop down the list, and do a choice:",
      "Renovation", JOptionPane.DEFAULT_OPTION, null, options, options[0]);
    int theMaterial = getSelectionAsInt(options, selectionDone);
    if (theMaterial < options.length - 3) {
      return theMaterial; // Return, we are going to use the material with this index
    }
    else if (theMaterial == noOfMaterials) return newPaint;
    else if (theMaterial == noOfMaterials + 1) return newFlooring;
    else if (theMaterial == noOfMaterials + 2) return newWallpaper;
    return -1;  // Return (error?)
  }

  private int getSelectionAsInt(String[] options, String selectionDone) {
    for (int i = 0; i < options.length; i++) {
      if (options[i].equals(selectionDone)) return i;
    }
    return -1;
  }
}

class RenovationChap12 {
  public static void main(String[] args) {
    ProjectChap12 aProject = new ProjectChap12();
    aProject.showInstructions();

    int option = aProject.doAChoice();
    while (option != ProjectChap12.exit) {
      aProject.carryOutTheRightThing(option);
      option = aProject.doAChoice();
    }
    System.exit(0);
  }
}

/* Example Run:

Data:
Ceiling in the corridor: 6 x 1 sq.m, paint: Super,2 coats, 12 sq.m./l, 80 kr/l ==> 1.0 liter kr 80
Wall in children's room: 7.2 x 2.35 sq.m, wallpaper: Sungold, 0.54 m x 12.5 m, 100 kr/roll ==> 3 rolls, kr 300
Floor in the office: length 3 m, width 4 m, flooring: Tappa, kr 250/m, width 3 m ==> 4 meters, kr. 1000
Total price kr 1380.

In this example we use Norwegian crowns as currency. The program
will use the currency given by the regional settings.

The ouput window, see figure 12.7.
*/

⌨️ 快捷键说明

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