📄 farm.java
字号:
package company;
import company.equipment.*;
import company.animal.*;
import company.animal.nofeet.*;
import company.animal.feet.*;
public class Farm extends Company {
private Animal[] animal = new Animal[3];
private Equipment[] equipment = new Equipment[2];
private int counter = 0;
private int counter1 = 0;
private int counter2 = 0;
public Farm(String name, String type){
super(name, type);
}
public void setAnimal(Animal animal){
int i = 0;
boolean chickenFound = false;
boolean cowFound = false;
boolean fishFound = false;
for (int counter1 = 0; counter1 < this.animal.length; counter1++) {
if (this.animal[counter1] != null){
if (this.animal[counter1] instanceof Chicken){
chickenFound = true;
}
else if (this.animal[counter1] instanceof Cow){
cowFound = true;
}
else if (this.animal[counter1] instanceof Fish){
fishFound = true;
}
}
}
while (this.animal[i] != null && i < this.animal.length){
i++;
}
if (animal instanceof Chicken && chickenFound == false){
this.animal[i] = animal;
}
if (animal instanceof Cow && cowFound == false){
this.animal[i] = animal;
}
if (animal instanceof Fish && fishFound == false){
this.animal[i] = animal;
}
}
public Animal[] getAnimal(){
return this.animal;
}
public Animal getChicken(){
int i;
for(i = 0; i < this.animal.length; i++){
if (this.animal[i] instanceof Chicken){
break;
}
}
return this.animal[i];
}
public Animal getCow(){
int i;
for(i = 0; i < this.animal.length; i++){
if (this.animal[i] instanceof Cow){
break;
}
}
return this.animal[i];
}
public Animal getFish(){
int i;
for(i = 0; i < this.animal.length; i++){
if (this.animal[i] instanceof Fish){
break;
}
}
return this.animal[i];
}
public void setEquipment (Equipment equipment) {
boolean equipmentFound = false;
if (this.equipment[0] == null) {
this.equipment[0] = equipment;
}
else{
for (int i = 0; i < this.equipment.length; i++) {
if (this.equipment[i] != null) {
if (this.equipment[i] instanceof Tractor && this.equipment[i].getBrand() != equipment.getBrand() || this.equipment[i].getType() != equipment.getType()) {
equipmentFound = true;
}
else if (this.equipment[i] instanceof FishingNet && this.equipment[i].getBrand() != equipment.getBrand() || this.equipment[i].getType() != equipment.getType()) {
equipmentFound = true;
}
}
}
if (equipmentFound == true) {
this.counter2++;
this.equipment[this.counter2] = equipment;
}
}
}
public Equipment getTractor(String type, String brand) {
int i;
for(i = 0; i < this.equipment.length; i++) {
if (this.equipment[i] instanceof Tractor && this.equipment[i].getType() == type && this.equipment[i].getBrand() == brand) {
break;
}
}
return this.equipment[i];
}
public Equipment getFishingNet(String type, String brand) {
int i;
for(i = 0; i < this.equipment.length; i++) {
if (this.equipment[i] instanceof FishingNet && this.equipment[i].getType() == type && this.equipment[i].getBrand() == brand) {
break;
}
}
return this.equipment[i];
}
public Equipment[] getEquipment(){
return this.equipment;
}
public String getAnimalInfo(){
String info = "";
for(int i = 0; i < this.animal.length; i++){
if (this.animal[i] != null){
if (this.animal[i] instanceof Chicken){
info = info + (((Chicken)this.animal[i]).getInfo());
}
else if (this.animal[i] instanceof Cow){
info = info + (((Cow)this.animal[i]).getInfo());
}
else {
info = info + (((Fish)this.animal[i]).getInfo());
}
}
}
return info;
}
public String getEquipmentInfo(){
String info = "";
for(int i = 0; i < this.equipment.length; i++){
if (this.equipment[i] != null){
if (this.equipment[i] instanceof Tractor){
info = info + (((Tractor)this.equipment[i]).getInfo());
}
else {
info = info + (((FishingNet)this.equipment[i]).getInfo());
}
}
}
return info;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -