diff --git a/Herd.java b/Herd.java index cd2b230..804caab 100755 --- a/Herd.java +++ b/Herd.java @@ -24,12 +24,12 @@ private Herd() { - herd = new ArrayListy(); + herd = new ArrayList(); parents = new Cow[] {null, null}; offspring = new Cow[] {null, null}; - for (i = 0; i < initialSize; i++) { + for (int i = 0; i < initialSize; i++) { String sex = "male"; - if (i %% 2 == 0) sex = "female"; + if ((i % 2) == 0) sex = "female"; herd.add(new Cow(sex)); } } @@ -42,19 +42,35 @@ return singleton; } + public Cow getCow(int id) + { + for (int c = 0; c < herd.size(); c++) { + if (herd.get(c).getID() == id) + return herd.get(c); + } + return null; + } + public void removeCow(int id) { - //TODO + Cow c = getCow(id); + if (c != null) herd.remove(c); } public void setParent(int id) { - //TODO + Cow p = getCow(id); + if (p == null) return; + if (p.getSex() == "male") parents[0] = p; + else if (p.getSex() == "female") parents[1] = p; } public void removeParent(int id) { - //TODO + if (parents[0] != null && parents[0].getID() == id) + parents[0] = null; + else if (parents[1] != null && parents[1].getID() == id) + parents[1] = null; } public String checkBreeding() @@ -66,16 +82,27 @@ public void breed() { //TODO + //randomly generate either one or two offspring + //breed the parents to create the offspring } public void keepChild(int id) { - //TODO + int o = -1; + if (offspring[0] != null && offspring[0].getID() == id) o = 0; + else if (offspring[1] != null && offspring[1].getID() == id) o = 1; + if (o >= 0) { + herd.add(offspring[o]); + offspring[o] = null; + } } public void discardChild(int id) { - //TODO + int o = -1; + if (offspring[0] != null && offspring[0].getID() == id) o = 0; + else if (offspring[1] != null && offspring[1].getID() == id) o = 1; + if (o >= 0) offspring[o] = null; } public void saveHerd(String filename)