📄 renovationproject.java
字号:
/*
* RenovationProject.java E.L. 2001-08-15
*
* This class maintains a register with surfaces and different types of paint.
* 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 allPaints = 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 exists, 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);
Paint thisPaint = newSurface.getPaint();
if (thisPaint != null) addNewPaint(thisPaint);
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 nameOfSurface) {
for (int i = 0; i < allSurfaces.size(); i++) {
Surface thisSurface = (Surface) allSurfaces.get(i);
if ((thisSurface.getName()).equals(nameOfSurface)) return thisSurface;
}
return null;
}
/* This method is constructed in the same way as addNewSurface() */
public Paint addNewPaint(Paint newPaint) {
Paint thisPaint = getPaint(newPaint.getName());
if (thisPaint == null) {
allPaints.add(newPaint);
return newPaint;
}
else return thisPaint;
}
/* This method is constructed in the same way as getSurface() */
public Paint getPaint(String nameOfPaint) {
for (int i = 0; i < allPaints.size(); i++) {
Paint thisPaint = (Paint) allPaints.get(i);
if ((thisPaint.getName()).equals(nameOfPaint)) return thisPaint;
}
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);
Paint paint = theSurface.getPaint();
if (paint != null) totalPrice += paint.getTotalPrice(theSurface);
}
return totalPrice;
}
/* The following methods may be used to run through all the paints
* or all the surfaces in a sequential way.
*/
public int getNoOfPaints() {
return allPaints.size();
}
public Paint getPaint(int index){
if (index >= 0 && index < allPaints.size()) {
return (Paint) allPaints.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 + -