renovationchap7.java
来自「Java the UML Way 书中所有源码」· Java 代码 · 共 91 行
JAVA
91 行
/*
* RenovationChap7.java E.L. 2001-06-19
*
*/
import javax.swing.JOptionPane;
class ProjectChap7 {
public static String[] alternatives = {"Wallpaper", "Paint", "Flooring", "Exit"};
public static final int wallpaper = 0;
public static final int paint = 1;
public static final int flooring = 2;
public static final int exit = 3;
private ReaderRenovationCase details = new ReaderRenovationCase();
public void showInstructions() {
String instruction =
"The program calculates the price and amount needed to renovate surfaces,\n" +
"such as floors and walls. First, you are presented a menu where you\n" +
"choose between materials: wallpaper, paint, or flooring. Then you have to enter\n" +
"data about the surface and about the chosen material.\n" +
"The area of the surface, the price and amount of material are then output.\n" +
"After that you are back into the menu again.\n";
JOptionPane.showMessageDialog(null, instruction);
}
public int doAChoice() {
return JOptionPane.showOptionDialog(null, "What do you choose:", "Renovation",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
alternatives, alternatives[0]);
}
public void carryOutTheRightThing(int option) {
Surface aSurface = details.readAndInstantiateSurface();
String result =
"\nThe area of " + aSurface.getName() + " is: " + aSurface.getArea() + " sq m.\n";
switch (option) {
case wallpaper:
Wallpaper aWallpaper = details.readAndInstantiateWallpaper();
result += ("You will need " + aWallpaper.getNoOfRolls(aSurface) +
" rolls of wallpaper " + aWallpaper.getName() + ", price $" +
aWallpaper.getTotalPrice(aSurface));
break;
case paint:
Paint aPaint = details.readAndInstantiatePaint();
result += ("You will need " + aPaint.getNoOfLiters(aSurface) + " liters of paint " +
aPaint.getName() + ", price $" + aPaint.getTotalPrice(aSurface));
break;
case flooring:
Flooring aFlooring = details.readAndInstantiateFlooring();
result += ("You will need " + aFlooring.getNoOfMeters(aSurface) +
" meters of flooring " + aFlooring.getName() + ", price $" +
aFlooring.getTotalPrice(aSurface));
break;
default:
break;
}
JOptionPane.showMessageDialog(null, result);
}
}
class RenovationChap7 {
public static void main(String[] args) {
ProjectChap7 aProject = new ProjectChap7();
aProject.showInstructions();
int option = aProject.doAChoice();
while (option != ProjectChap7.exit) { // we have to qualify the name exit
aProject.carryOutTheRightThing(option);
option = aProject.doAChoice();
}
System.exit(0);
}
}
/* Sample data:
/* Sample data:
Input:
Margaret抯 wall: 7.2 m x 2.35 m,
Wallpaper Sungold: price $9.50, length: 12.5 m, width: 0.54 m.
Anne抯 wall: 7.2 m x 2.35 m, paint 056789: price $9.90, 3 coats, 10 sq m/l.
John抯 floor: 5 m x 4 m, Flooring Berger XX: price $34.00 , width: 5 m.
Output:
The area of Margaret抯 wall is: 16.92 sq m.
You will need 3 rolls of wallpaper Sungold, price $28.50
The area of Anne抯 wall is: 16.92 sq m.
You will need 5.5 liters of paint 056789, price $54.45
The area of John抯 wall is: 20.0 sq m.
You will need 4.0 meters of flooring Berger XX, price $136.0
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?