question:农场的母牛寿命是5年,母牛第二年和第四年会繁殖母牛一只,第五年死去。现假设农场第一年有一岁母牛一只,问第五年农场有几只牛。
answer:
用面向对象的角度解下。我这里理解的死去那年没有繁殖能力且对配置文件输入的输入没有验证。
母牛类:
package com.baixing.shanghai.interview;
/**
* 母牛
* @author zyj
*
*/
public class Cow {
private int age;// 年龄,从1开始
private int lifespan;// 寿命
private String fertilizeAges;// 繁殖年龄点,表示方式:",2,4,"
private boolean fertilize;// 繁殖能力
public Cow() {
super();
}
public Cow(int lifespan, String fertilizeAges) {
super();
this.age = 1;
this.lifespan = lifespan;
this.fertilizeAges = fertilizeAges;
judgeFertilize(age);
}
public Cow(int age, int lifespan, String fertilizeAges) {
super();
this.age = age;
this.lifespan = lifespan;
this.fertilizeAges = fertilizeAges;
judgeFertilize(age);
}
/**
* 年龄加一
*
* @return
*/
public int moveTONextyear() {
age++;
if (age == lifespan) {
return lifespan;
}
judgeFertilize(age);
return age;
}
/**
* 设置繁殖能力
*
* @param age
*/
public void judgeFertilize(int age) {
String strAge = "," + String.valueOf(age) + ",";
if (fertilizeAges.indexOf(strAge) == -1) {
fertilize = false;
} else {
fertilize = true;
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
judgeFertilize(age);
}
public String getFertilizeAges() {
return fertilizeAges;
}
public void setFertilizeAges(String fertilizeAges) {
this.fertilizeAges = fertilizeAges;
}
public boolean getFertilize() {
return fertilize;
}
public void setFertilize(boolean fertilize) {
this.fertilize = fertilize;
}
public int getLifespan() {
return lifespan;
}
public void setLifespan(int lifespan) {
this.lifespan = lifespan;
}
}
工厂类:
package com.baixing.shanghai.interview;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 母牛繁殖类
* @author zyj
*
*/
public class BreedFactury {
private List<Cow> cows = new ArrayList<Cow>();// 所以母牛
private BigInteger cowNUM = BigInteger.ZERO; // 母牛数量
private int years ; // 时间:年限
private int lifespan; // 寿命
private String fertilizeAges;// 繁殖年龄点,表示方式:",2,4,"
public BreedFactury() {
super();
}
public BreedFactury(int years, int lifespan, String fertilizeAges) {
super();
this.years = years;
this.lifespan = lifespan;
this.fertilizeAges = fertilizeAges;
}
/**
* 规定的年限里繁殖母牛
*
* @return
*/
public List<Cow> produce() {
// 初始化第一年一头母牛
cows.add(new Cow(5, ",2,4,"));
cowNUM = cowNUM.add(BigInteger.valueOf(1));
for (int i = 2; i <= years; ++i) {
// System.out.print("第"+i+"年:");
doyear(cows); // 今年要做的事情
}
return cows;
}
/**
* 今年要做的事情:年龄加一,删除到寿命的牛,繁殖母牛
*
* @param cows
* @return
*/
public List<Cow> doyear(List<Cow> cows) {
// 所有母牛年龄加一
for (Cow cow : cows) {
cow.moveTONextyear();
}
// 删除lifespan岁数的母牛
Iterator<Cow> iterator = cows.iterator();
while (iterator.hasNext()) {
Cow cow = (Cow) iterator.next();
if (cow.getAge() == 5) {
iterator.remove();
cowNUM = cowNUM.subtract(BigInteger.valueOf(1));
}
;
}
// 母牛繁殖
int currentAmount = cows.size();
for (int i = 0; i < currentAmount; ++i) {
if (cows.get(i).getFertilize()) {// 判断有繁殖能力
cows.add(new Cow(5, ",2,4,"));
cowNUM = cowNUM.add(BigInteger.valueOf(1));
}
}
// 遍历看下每年怒牛数量和年龄
// System.out.print("共"+cowNUM.toString()+"只母牛。");
// for (Cow cow : cows) {
// System.out.print(cow.getAge()+",");
// }
// System.out.println();
return cows;
}
public List<Cow> getCows() {
return cows;
}
public void setCows(List<Cow> cows) {
this.cows = cows;
}
public BigInteger getCowNUM() {
return cowNUM;
}
public void setCowNUM(BigInteger cowNUM) {
this.cowNUM = cowNUM;
}
public int getYears() {
return years;
}
public void setYears(int years) {
this.years = years;
}
public int getLifespan() {
return lifespan;
}
public void setLifespan(int lifespan) {
this.lifespan = lifespan;
}
public String getFertilizeAges() {
return fertilizeAges;
}
public void setFertilizeAges(String fertilizeAges) {
this.fertilizeAges = fertilizeAges;
}
}
Junit4类:
package com.baixing.shanghai.interview;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import org.junit.Test;
public class ClassTest {
@Test
public void test() throws Exception {
InputStream inputStream = ClassTest.class.getResourceAsStream("config.properties");
Properties properties = new Properties();
properties.load(inputStream);
int years = Integer.parseInt(properties.getProperty("years")); // 时间:年限,第几年
int lifespan = Integer.parseInt(properties.getProperty("lifespan")); // 寿命
String fertilizeAges = properties.getProperty("fertilizeAges");// 繁殖年龄点,表示方式:",2,4,"
BreedFactury breedFactury = new BreedFactury(years, lifespan,fertilizeAges);
List<Cow> cows = breedFactury.produce();
System.out.println(years+"年母牛总数:" + breedFactury.getCowNUM().toString());
}
}
同一包下的config.properties配置文件
years=5
lifespan=5
fertilizeAges=,2,4,
输出结果:
第2年:共2只母牛。2,1,
第3年:共3只母牛。3,2,1,
第4年:共5只母牛。4,3,2,1,1,
第5年:共7只母牛。4,3,2,2,1,1,1,
5年母牛总数:7