renovationchap10.java
来自「Java the UML Way 书中所有源码」· Java 代码 · 共 164 行
JAVA
164 行
/*
* RenovationChap10.java E.L. 2001-08-15
*
*/
import java.text.*;
import javax.swing.JOptionPane;
import myLibrary.InputReader; // see chap. 6.8
class ProjectChap10 {
public static final int exit = -1;
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 paint several surfaces\n" +
"with different kinds of paint. You may choose to change the type of paint 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 " +
"paint 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 paint 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 paint
Paint thePaint = getPaint();
Surface theSurface = project.getSurface(option); // get a reference to the surface object
theSurface.setPaint(thePaint);
} else { // new surface is to be registered
Surface aSurface = details.readAndInstantiateSurface();
if (project.addNewSurface(aSurface) == aSurface) { // ok, the paint should be found
Paint thePaint = getPaint();
aSurface.setPaint(thePaint);
} else {
JOptionPane.showMessageDialog(null,
"Surface with this name is already registered.");
}
}
}
/*
* This method creates a String array to be presented to the user as a menu.
* The array will contain all registered surfaces with their paint 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);
Paint thePaint = thisSurface.getPaint();
if (thePaint != null) {
double noOfLiters = thePaint.getNoOfLiters(thisSurface);
String noOfLitersFormatted = numberFormat.format(noOfLiters);
double price = thePaint.getTotalPrice(thisSurface);
String priceFormatted = currencyFormat.format(price);
options[i] = thisSurface.getName() + ", paint: " + thePaint.getName() + ", "
+ noOfLitersFormatted + " liters, price: " + priceFormatted;
}
else options[i] = thisSurface.getName() + ", paint 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 paint, or registers a new one.
* In this case, the method returns null if a paint with the
* same name already is registered.
*/
private Paint getPaint() {
int paintIndex = getPaintIndex(); // the user choose the right paint
Paint aPaint;
if (paintIndex < 0) { // we are going to register a new paint
aPaint = details.readAndInstantiatePaint();
if (project.addNewPaint(aPaint) != aPaint) {
JOptionPane.showMessageDialog(null, "Paint with this name is already registered.");
}
}
else aPaint = project.getPaint(paintIndex);
return aPaint;
}
/*
* This method lets the user choose between all existing paints, or he may
* choose to register a new paint. In the first case the method returns
* the index of the chosen paint, in the second case the method returns -1.
*/
private int getPaintIndex() {
int noOfPaints = project.getNoOfPaints();
if (noOfPaints > 0) { // there are paints registered
int noOfOptions = noOfPaints + 1;
String[] options = new String[noOfOptions];
for (int i = 0; i < noOfPaints; i++) {
Paint thisPaint = project.getPaint(i);
options[i] = thisPaint.getName();
}
options[noOfPaints] = new String("New Paint");
/* 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 thePaint = getSelectionAsInt(options, selectionDone);
if (thePaint < options.length - 1) {
return thePaint; // Return, we are going to use the paint with this index
}
}
return -1; // Return, we are going to register a new paint
}
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 RenovationChap10 {
public static void main(String[] args) {
ProjectChap10 aProject = new ProjectChap10();
aProject.showInstructions();
int option = aProject.doAChoice();
while (option != ProjectChap10.exit) {
aProject.carryOutTheRightThing(option);
option = aProject.doAChoice();
}
System.exit(0);
}
}
/* Example Run:
Data:
Surfaces: A wall in Mary's room: 3 x 4 m, Ceiling in the corridor: 6 x 1 m,
Paints: Heimdal Extra: 3 coats, 10 kvm/l, 100 NOK/l,
Heimdal Super: 2 coats, 12 kvm/l, 80 NOK/l
In this example we use Norwegian crowns as currency. The program
will use the currency given by the regional settings.
The output window, see figure 10.6.
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?