今天重写了很多方法。只是因为一些细节当初没考虑到。今天使用了这么一种流程图,倒是可以比较清晰的展示一个功能的实现过程,发现功能的不完善的地方。如下图右边的小图:
今天重写的方法主要是跟种植这个功能有关,包括种植方法,Farm里增加了SeedIntoSoil()方法,Plants对象增加了ID可以和Soil对应了。主要完成的类有农场类和土壤类,以及增加了PlantsStorage这个新类,用于展示和管理Plants对象。另外优化了Storage类,在其addStorage()和minusStorage()方法里直接使用一个新的判断方式【利用&&判断机制:如果第一个提交为false时不会执行第二个条件判断,规避了出现空指针异常的情况】,使得程序初始化数组时不必再写一堆new Plants()数组元素。
package prostor;
import plants.Plants;
public class PlantsStorage {
private Plants[] plantsArray = new Plants[20];
public PlantsStorage() {
}
public void demonstratePlants(Plants[] plts) {
// 显示有多少作物以及状态。
int j = plantsArray.length;
for (int i = 0; i < j; i++) {
if (plts[i] == null)
continue;
System.out.println(plts[i].getName() + " 状态:" + plts[i].getStatus());
}
}
public void addStorage(Plants plts) {
// 收获增加库存
int i = plantsArray.length;
int j = 0;
for (; j < i; j++) {
if (plantsArray[j] != null) {
plantsArray[j] = plts;
System.out.println("列入作物系列");
break;
}
}
}
public boolean minusStorage(Plants plts) {
// 收获,减少库存。
int i = plantsArray.length;
boolean bl = true;
int id = plts.getId();
for (int j = 0; j < i; j++) {
if (plantsArray[j] != null && plantsArray[j].getId() == id) {
plantsArray[j] = null;
System.out.println("排出作物系列");
break;
} else
bl = false;
}
return bl;
}
public Plants[] getPlantsArray() {
return