From 7d47bfa94b0e93abd014d74ef0270f2fff7eeefa Mon Sep 17 00:00:00 2001 From: Oscar Schwarz Date: Mon, 17 Jun 2024 13:36:50 +0200 Subject: [PATCH] feat(FishCalculator): add first iteration of recursion --- .../java/fish_variety/FishCalculator.java | 37 +++++++++++++++++++ src/main/java/fish_variety/Main.java | 3 +- 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/main/java/fish_variety/FishCalculator.java diff --git a/src/main/java/fish_variety/FishCalculator.java b/src/main/java/fish_variety/FishCalculator.java new file mode 100644 index 0000000..c9da80c --- /dev/null +++ b/src/main/java/fish_variety/FishCalculator.java @@ -0,0 +1,37 @@ +package fish_variety; + +import java.util.ArrayList; + +public class FishCalculator { + + public static FishCollection mostCompatibleFishTypesOfCollection(int price, FishCollection initialCollection) { + FishConstraints constraints = initialCollection.getConstraints(); + + ArrayList variants = new ArrayList<>(); + + for (Fish fish : constraints.getAllFish()) { + FishCollection collectionVariant = new FishCollection(constraints, initialCollection.getFishes()); + + collectionVariant.add(fish); + + if (collectionVariant.isValid() && collectionVariant.getPrice() <= price) { + variants.add(FishCalculator.mostCompatibleFishTypesOfCollection(price, collectionVariant)); + } + } + + if (variants.size() == 0) { + return initialCollection; + } + + // Get largets collection in variants + FishCollection largestCollectionVariant = variants.get(0); + + for (FishCollection fishCollection : variants) { + if (fishCollection.getFishes().size() > largestCollectionVariant.getFishes().size()) { + largestCollectionVariant = fishCollection; + } + } + + return largestCollectionVariant; + } +} diff --git a/src/main/java/fish_variety/Main.java b/src/main/java/fish_variety/Main.java index 752af4c..306f374 100644 --- a/src/main/java/fish_variety/Main.java +++ b/src/main/java/fish_variety/Main.java @@ -53,6 +53,7 @@ public static void main( String[] args ) grueneMigraene, prachtpiranha, zitterling }); - FishCollection collection = new FishCollection(constraints); + FishCollection bestVariant = FishCalculator.mostCompatibleFishTypesOfCollection(170, new FishCollection(constraints)); + System.out.println(bestVariant); } }