Commit 6c947720 authored by Todor Mitrev's avatar Todor Mitrev
Browse files

changed structure, made egg class, fixed bugs

parent d2eec0cd
No related merge requests found
Showing with 99 additions and 74 deletions
+99 -74
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
\ No newline at end of file
File added
No preview for this file type
No preview for this file type
File added
File added
No preview for this file type
No preview for this file type
package barn;
public abstract class Ageing {
private int age;
public Ageing(int age) {
this.age = age;
}
public void incrementAge() {
this.age++;
}
public int getAge() {
return this.age;
}
}
package barn;
public class Chicken {
public class Chicken extends Ageing {
private final String name;
private int age;
int[] laidEggs;
Chicken parent;
private boolean isDead;
private static final int numberOfEggTypes = 2;
private static final int eggsToBeHatchedIndex = 0;
private static final int eggsToBeHatchedInAWeekIndex = 1;
private String getDisplayName() {
return new StringBuilder(name)
.append("{")
.append(isDead ? "x" : age)
.append(isDead ? "x" : super.getAge())
.append("}")
.toString();
}
public Chicken(String name, int age, Chicken parent) {
super(age);
this.name = name;
this.age = age;
this.laidEggs = new int[numberOfEggTypes];
this.parent = parent;
isDead = false;
}
......@@ -32,15 +26,7 @@ public class Chicken {
}
public boolean canLayEggs() {
return age >= 2 && age <= 4;
}
public int getAge() {
return age;
}
public void incrementAge() {
age++;
return super.getAge() >= 2 && super.getAge() <= 4;
}
public void die() {
......@@ -58,19 +44,4 @@ public class Chicken {
.toString();
}
public int getEggsToBeHatchedCount() {
return laidEggs[eggsToBeHatchedIndex];
}
//make sure that for every chicken the toBeHatched eggs are added to the barn as newborn chickens
//before this method is called.
public void layEggs(int numberOfEggsToBeLaid) {
ageUpEggs();
laidEggs[eggsToBeHatchedInAWeekIndex] = numberOfEggsToBeLaid;
}
public void ageUpEggs() {
laidEggs[eggsToBeHatchedIndex] = laidEggs[eggsToBeHatchedInAWeekIndex];
laidEggs[eggsToBeHatchedInAWeekIndex] = 0;
}
}
}
\ No newline at end of file
package barn;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public class ChickenBarn {
private final List<Chicken> allChickens;
private final Set<Chicken> recordOfDeadChickens;
private final List<Egg> eggs;
private int numberOfEggs;
private int lastAddedChickenNumber;
......@@ -18,61 +21,58 @@ public class ChickenBarn {
});
}
private void recordDead() {
List<Chicken> diedThisWeek = allChickens.stream()
.filter(chicken -> chicken.isDead())
.collect(Collectors.toList());
recordOfDeadChickens.addAll(diedThisWeek);
private void ageUpAllEggs() {
eggs.forEach(egg -> egg.incrementAge());
}
private void hatchEggs() {
List<Chicken> newbornsForCurrentChickens = new ArrayList<>();
allChickens.forEach(currentChicken -> {
int eggsToBeHatchedCount = currentChicken.getEggsToBeHatchedCount();
for (int i = 0; i < eggsToBeHatchedCount; i++) {
lastAddedChickenNumber++;
newbornsForCurrentChickens
.add(new Chicken("Chicken" + lastAddedChickenNumber, 0, currentChicken));
List<Egg> filtered = new ArrayList<>();
for (Egg egg: eggs) {
if(egg.getAge() == 2) {
filtered.add(egg);
}
numberOfEggs -= eggsToBeHatchedCount;
}
filtered.forEach(egg -> {
lastAddedChickenNumber++;
numberOfEggs--;
eggs.remove(egg);
allChickens.add(new Chicken("Chicken"+lastAddedChickenNumber,0,egg.getParent()));
});
allChickens.addAll(newbornsForCurrentChickens);
}
private void layEggs(int numberOfEggsToBeLaid) {
List<Chicken> chickensWhoLayEggs= allChickens.stream().filter(chicken -> chicken.canLayEggs()).collect(Collectors.toList());
chickensWhoLayEggs.forEach(chicken -> chicken.layEggs(numberOfEggsToBeLaid));
numberOfEggs += chickensWhoLayEggs.size() * numberOfEggsToBeLaid;
List<Chicken> chickensWhoLayEggs = allChickens.stream()
.filter(Chicken::canLayEggs)
.collect(Collectors.toList());
chickensWhoLayEggs.forEach(chicken -> {
List<Egg> laidEggs = new ArrayList<>();
for (int i = 0; i < numberOfEggsToBeLaid; i++) {
laidEggs.add(new Egg(chicken));
}
eggs.addAll(laidEggs);
numberOfEggs+=laidEggs.size();
});
}
public ChickenBarn(Collection<Chicken> firstBought) {
allChickens = new ArrayList<>(firstBought);
recordOfDeadChickens = new HashSet<>();
eggs = new ArrayList<>();
numberOfEggs = 0;
lastAddedChickenNumber = firstBought.size() - 1;
}
public void updateBarnAfterAWeekHasPassed(String weekType) {
int numberOfEggsToBeLaid = switch (weekType) {
case "bad" -> 0;
case "normal" -> 1;
case "good" -> 3;
default -> throw new IllegalStateException("Unexpected weekType value");
};
public void updateBarnAfterAWeekHasPassed(WeekType weekType) {
int numberOfEggsToBeLaid = weekType.getNumberOfChickensToHatch();
ageUpAllChickens();
recordDead();
for (Chicken recordOfDeadChicken : recordOfDeadChickens) {
recordOfDeadChicken.ageUpEggs();
}
ageUpAllEggs();
hatchEggs();
layEggs(numberOfEggsToBeLaid);
}
public List<Chicken> getAliveChickens() {
List<Chicken> aliveChickens = new ArrayList<>(allChickens);
aliveChickens.removeAll(recordOfDeadChickens);
return aliveChickens;
return allChickens.stream().filter(chicken -> !chicken.isDead()).toList();
}
public int getNumberOfEggs() {
......
package barn;
public class Egg extends Ageing{
private final Chicken parent;
public Egg(Chicken parent) {
super(0);
this.parent = parent;
}
public Chicken getParent() {
return parent;
}
}
package barn;
public enum WeekType {
GOOD(3),
NORMAL(1),
BAD(0);
private final int numberOfChickensToHatch;
WeekType(int numberOfChickensToHatch) { this.numberOfChickensToHatch = numberOfChickensToHatch; }
public int getNumberOfChickensToHatch() { return numberOfChickensToHatch; }
}
......@@ -2,6 +2,7 @@ package inputoutputhandler;
import barn.Chicken;
import barn.ChickenBarn;
import barn.WeekType;
import java.io.IOException;
import java.io.PrintWriter;
......@@ -15,8 +16,8 @@ public class InputOutputHandler {
private final ChickenBarn barn;
private final List<String> weeks;
private static final Path chickensBoughtFilePath = Path.of("ChickensBought.txt");
private static final Path WeekTypesFilePath = Path.of("WeekTypes.txt");
public static final Path chickensBoughtFilePath = Path.of("ChickensBought.txt");
public static final Path WeekTypesFilePath = Path.of("WeekTypes.txt");
private List<String> readFromFile(Path filePath) {
List<String> lines;
......@@ -33,12 +34,13 @@ public class InputOutputHandler {
private List<Chicken> readBoughtChickenInput() {
int[] chickenAges = readFromFile(chickensBoughtFilePath).stream().mapToInt(Integer::parseInt).toArray();
return IntStream.range(0, chickenAges.length)
.mapToObj(currentIndex -> new Chicken("Chicken" + currentIndex, chickenAges[currentIndex], null))
.mapToObj(currentIndex ->
new Chicken("Chicken" + currentIndex, chickenAges[currentIndex], null))
.collect(Collectors.toList());
}
private void simulateWeeks() {
weeks.stream().forEach(week -> barn.updateBarnAfterAWeekHasPassed(week));
weeks.stream().forEach(week -> barn.updateBarnAfterAWeekHasPassed(WeekType.valueOf(week.toUpperCase())));
}
private List<String> chickenNames() {
......
......@@ -7,6 +7,7 @@ import java.util.Scanner;
public class Main {
private static final Path outputFilePath = Path.of("OutputFile.txt");
public static void main(String[] args) {
InputOutputHandler handler = new InputOutputHandler();
System.out.println("Welcome! ");
......@@ -15,7 +16,7 @@ public class Main {
System.out.println("Would you like to save the output to a file?");
Scanner sc = new Scanner(System.in);
String reply = sc.nextLine();
if(reply.equals("yes")) {
if (reply.equals("yes")) {
handler.writeToFile(outputFilePath);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment