From 74a8665d4c21ea51389a4e5987b8c0ff859bd186 Mon Sep 17 00:00:00 2001 From: Jurn Wubben Date: Mon, 28 Jul 2025 16:21:47 +0200 Subject: [PATCH 1/4] Added nix support --- .envrc | 1 + .gitignore | 2 +- flake.lock | 27 +++++++++++++++++++++++++++ flake.nix | 11 +++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 .envrc create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index b010a96..6f6a266 100644 --- a/.gitignore +++ b/.gitignore @@ -63,4 +63,4 @@ fabric.properties .idea/httpRequests # Android studio 3.1+ serialized cache file -.idea/caches/build_file_checksums.ser \ No newline at end of file +.idea/caches/build_file_checksums.ser.direnv diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..04233f3 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1753489912, + "narHash": "sha256-uDCFHeXdRIgJpYmtcUxGEsZ+hYlLPBhR83fdU+vbC1s=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "13e8d35b7d6028b7198f8186bc0347c6abaa2701", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-25.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..380498b --- /dev/null +++ b/flake.nix @@ -0,0 +1,11 @@ +{ + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; + outputs = {nixpkgs, ...}: let + system = "x86_64-linux"; + pkgs = nixpkgs.legacyPackages.${system}; + in { + devShells.${system}.default = pkgs.mkShell { + nativeBuildInputs = [pkgs.jetbrains.idea-community-bin pkgs.jdk8]; + }; + }; +} From 145a49ce7a637f65f6853ffc6743a20f440253dc Mon Sep 17 00:00:00 2001 From: Jurn Wubben Date: Mon, 28 Jul 2025 16:22:46 +0200 Subject: [PATCH 2/4] Fixed gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 6f6a266..f1bb157 100644 --- a/.gitignore +++ b/.gitignore @@ -64,3 +64,5 @@ fabric.properties # Android studio 3.1+ serialized cache file .idea/caches/build_file_checksums.ser.direnv + +.direnv/ From bd21c2ad30e6562167ddecd07745393c267e6458 Mon Sep 17 00:00:00 2001 From: Jurn Wubben Date: Tue, 29 Jul 2025 13:05:45 +0200 Subject: [PATCH 3/4] Implemeted base classes; Refactored animals; Finished main. --- .gitignore | 2 + src/com/ing/zoo/Hippo.java | 23 ------- src/com/ing/zoo/Lion.java | 23 ------- src/com/ing/zoo/Pig.java | 47 ------------- src/com/ing/zoo/Tiger.java | 41 ----------- src/com/ing/zoo/Zebra.java | 26 ------- src/com/ing/zoo/Zoo.java | 76 ++++++++++++++------- src/com/ing/zoo/animals/Hippo.java | 10 +++ src/com/ing/zoo/animals/Lion.java | 10 +++ src/com/ing/zoo/animals/Pig.java | 30 ++++++++ src/com/ing/zoo/animals/Tiger.java | 31 +++++++++ src/com/ing/zoo/animals/Zebra.java | 10 +++ src/com/ing/zoo/base/Animal.java | 16 +++++ src/com/ing/zoo/base/Carnivore.java | 14 ++++ src/com/ing/zoo/base/CarnivoreBehavior.java | 5 ++ src/com/ing/zoo/base/Herbivore.java | 13 ++++ src/com/ing/zoo/base/HerbivoreBehavior.java | 5 ++ src/com/ing/zoo/base/Omnivore.java | 23 +++++++ 18 files changed, 221 insertions(+), 184 deletions(-) delete mode 100644 src/com/ing/zoo/Hippo.java delete mode 100644 src/com/ing/zoo/Lion.java delete mode 100644 src/com/ing/zoo/Pig.java delete mode 100644 src/com/ing/zoo/Tiger.java delete mode 100644 src/com/ing/zoo/Zebra.java create mode 100644 src/com/ing/zoo/animals/Hippo.java create mode 100644 src/com/ing/zoo/animals/Lion.java create mode 100644 src/com/ing/zoo/animals/Pig.java create mode 100644 src/com/ing/zoo/animals/Tiger.java create mode 100644 src/com/ing/zoo/animals/Zebra.java create mode 100644 src/com/ing/zoo/base/Animal.java create mode 100644 src/com/ing/zoo/base/Carnivore.java create mode 100644 src/com/ing/zoo/base/CarnivoreBehavior.java create mode 100644 src/com/ing/zoo/base/Herbivore.java create mode 100644 src/com/ing/zoo/base/HerbivoreBehavior.java create mode 100644 src/com/ing/zoo/base/Omnivore.java diff --git a/.gitignore b/.gitignore index f1bb157..7fa944b 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,5 @@ fabric.properties .idea/caches/build_file_checksums.ser.direnv .direnv/ + +*.class \ No newline at end of file diff --git a/src/com/ing/zoo/Hippo.java b/src/com/ing/zoo/Hippo.java deleted file mode 100644 index 908dfb4..0000000 --- a/src/com/ing/zoo/Hippo.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.ing.zoo; - -public class Hippo { - public String name; - public String helloText; - public String eatText; - - public Hippo() - { - } - - public void sayHello() - { - helloText = "splash"; - System.out.println(helloText); - } - - public void eatLeaves() - { - eatText = "munch munch lovely"; - System.out.println(eatText); - } -} diff --git a/src/com/ing/zoo/Lion.java b/src/com/ing/zoo/Lion.java deleted file mode 100644 index b31a046..0000000 --- a/src/com/ing/zoo/Lion.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.ing.zoo; - -public class Lion { - public String name; - public String helloText; - public String eatText; - - public Lion() - { - } - - public void sayHello() - { - helloText = "roooaoaaaaar"; - System.out.println(helloText); - } - - public void eatMeat() - { - eatText = "nomnomnom thx mate"; - System.out.println(eatText); - } -} diff --git a/src/com/ing/zoo/Pig.java b/src/com/ing/zoo/Pig.java deleted file mode 100644 index 5db60e1..0000000 --- a/src/com/ing/zoo/Pig.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.ing.zoo; - -import java.util.Random; - -public class Pig { - public String name; - public String helloText; - public String eatText; - public String trick; - - public Pig() - { - } - - public void sayHello() - { - helloText = "splash"; - System.out.println(helloText); - } - - public void eatLeaves() - { - eatText = "munch munch oink"; - System.out.println(eatText); - } - - public void eatMeat() - { - eatText = "nomnomnom oink thx"; - System.out.println(eatText); - } - - public void performTrick() - { - Random random = new Random(); - int rnd = random.nextInt(2); - if(rnd == 0) - { - trick = "rolls in the mud"; - } - else - { - trick = "runs in circles"; - } - System.out.println(trick); - } -} diff --git a/src/com/ing/zoo/Tiger.java b/src/com/ing/zoo/Tiger.java deleted file mode 100644 index 0467189..0000000 --- a/src/com/ing/zoo/Tiger.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.ing.zoo; - -import java.util.Random; - -public class Tiger { - public String name; - public String helloText; - public String eatText; - public String trick; - - public Tiger() - { - } - - public void sayHello() - { - helloText = "rraaarww"; - System.out.println(helloText); - } - - public void eatMeat() - { - eatText = "nomnomnom oink wubalubadubdub"; - System.out.println(eatText); - } - - public void performTrick() - { - Random random = new Random(); - int rnd = random.nextInt(2); - if(rnd == 0) - { - trick = "jumps in tree"; - } - else - { - trick = "scratches ears"; - } - System.out.println(trick); - } -} diff --git a/src/com/ing/zoo/Zebra.java b/src/com/ing/zoo/Zebra.java deleted file mode 100644 index 2b8e97a..0000000 --- a/src/com/ing/zoo/Zebra.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.ing.zoo; - -import java.util.Random; - -public class Zebra { - public String name; - public String helloText; - public String eatText; - public String trick; - - public Zebra() - { - } - - public void sayHello() - { - helloText = "zebra zebra"; - System.out.println(helloText); - } - - public void eatLeaves() - { - eatText = "munch munch zank yee bra"; - System.out.println(eatText); - } -} diff --git a/src/com/ing/zoo/Zoo.java b/src/com/ing/zoo/Zoo.java index 8ebbc56..6c4dffd 100644 --- a/src/com/ing/zoo/Zoo.java +++ b/src/com/ing/zoo/Zoo.java @@ -2,37 +2,65 @@ package com.ing.zoo; import java.util.Scanner; +import com.ing.zoo.animals.*; +import com.ing.zoo.base.*; + public class Zoo { public static void main(String[] args) { - String[] commands = new String[4]; - commands[0] = "hello"; - commands[1] = "give leaves"; - commands[2] = "give meat"; - commands[3] = "perform trick"; - - Lion henk = new Lion(); - henk.name = "henk"; - Hippo elsa = new Hippo(); - elsa.name = "elsa"; - Pig dora = new Pig(); - dora.name = "dora"; - Tiger wally = new Tiger(); - wally.name = "wally"; - Zebra marty = new Zebra(); - marty.name = "marty"; + Animal[] animals = { + new Lion("henk"), + new Hippo("elsa"), + new Pig("dora"), + new Tiger("wally"), + new Zebra("marty") + }; Scanner scanner = new Scanner(System.in); - System.out.print("Voer uw command in: "); + System.out.print("Enter your command: "); - String input = scanner.nextLine(); - if(input.equals(commands[0] + " henk")) - { - henk.sayHello(); + String input = scanner.nextLine().trim(); + String[] splittedInput = input.split("\\s+"); + + scanner.close(); // NOTE: You don't do this in the template for some reason; It's a resource leak. + + if (splittedInput.length == 0) { + System.out.println("Please provide an command."); + return; } - else - { - System.out.println("Unknown command: " + input); + + if (splittedInput[0].equals("hello")) { + for (Animal animal : animals) { + if (splittedInput.length > 1 && animal.name.equals(splittedInput[1])) + animal.sayHello(); + else if (splittedInput.length == 1) animal.sayHello(); + } + + return; } + + for (Animal animal : animals) { + switch (input) { + case "give leaves": + if (animal instanceof HerbivoreBehavior) + ((HerbivoreBehavior) animal).eatLeaves(); + + break; + case "give meat": + if (animal instanceof CarnivoreBehavior) + ((CarnivoreBehavior) animal).eatMeat(); + + break; + case "perform trick": + animal.performTrick(); + break; + + default: + System.err.println("Invalid command: " + input); + return; + }; + } + + return; } } diff --git a/src/com/ing/zoo/animals/Hippo.java b/src/com/ing/zoo/animals/Hippo.java new file mode 100644 index 0000000..7408623 --- /dev/null +++ b/src/com/ing/zoo/animals/Hippo.java @@ -0,0 +1,10 @@ +package com.ing.zoo.animals; + +import com.ing.zoo.base.Herbivore; + +public class Hippo extends Herbivore { + public Hippo(String name) + { + super(name, "splash", "munch munch lovely"); + } +} diff --git a/src/com/ing/zoo/animals/Lion.java b/src/com/ing/zoo/animals/Lion.java new file mode 100644 index 0000000..7320dc4 --- /dev/null +++ b/src/com/ing/zoo/animals/Lion.java @@ -0,0 +1,10 @@ +package com.ing.zoo.animals; + +import com.ing.zoo.base.Carnivore; + +public class Lion extends Carnivore { + public Lion(String name) + { + super(name, "roooaoaaaaar", "nomnomnom thx mate"); + } +} diff --git a/src/com/ing/zoo/animals/Pig.java b/src/com/ing/zoo/animals/Pig.java new file mode 100644 index 0000000..0ca3cd4 --- /dev/null +++ b/src/com/ing/zoo/animals/Pig.java @@ -0,0 +1,30 @@ +package com.ing.zoo.animals; + +import java.util.Random; + +import com.ing.zoo.base.Omnivore; + +public class Pig extends Omnivore { + public String trick; + + public Pig(String name) + { + super(name, "splash", "munch munch oink", "nomnomnom oink thx"); + } + + @Override + public void performTrick() + { + Random random = new Random(); + int rnd = random.nextInt(2); + if(rnd == 0) + { + trick = "rolls in the mud"; + } + else + { + trick = "runs in circles"; + } + System.out.println(trick); + } +} diff --git a/src/com/ing/zoo/animals/Tiger.java b/src/com/ing/zoo/animals/Tiger.java new file mode 100644 index 0000000..e07d37b --- /dev/null +++ b/src/com/ing/zoo/animals/Tiger.java @@ -0,0 +1,31 @@ +package com.ing.zoo.animals; + +import java.util.Random; + +import com.ing.zoo.base.Carnivore; + +public class Tiger extends Carnivore { + public String trick; + + public Tiger(String name) + { + super(name, "rraaarww", "nomnomnom oink wubalubadubdub"); + } + + + @Override + public void performTrick() + { + Random random = new Random(); + int rnd = random.nextInt(2); + if(rnd == 0) + { + trick = "jumps in tree"; + } + else + { + trick = "scratches ears"; + } + System.out.println(trick); + } +} diff --git a/src/com/ing/zoo/animals/Zebra.java b/src/com/ing/zoo/animals/Zebra.java new file mode 100644 index 0000000..963b2f8 --- /dev/null +++ b/src/com/ing/zoo/animals/Zebra.java @@ -0,0 +1,10 @@ +package com.ing.zoo.animals; + +import com.ing.zoo.base.Herbivore; + +public class Zebra extends Herbivore { + public Zebra(String name) + { + super(name, "zebra zebra", "munch munch zank yee bra"); + } +} diff --git a/src/com/ing/zoo/base/Animal.java b/src/com/ing/zoo/base/Animal.java new file mode 100644 index 0000000..fdaa274 --- /dev/null +++ b/src/com/ing/zoo/base/Animal.java @@ -0,0 +1,16 @@ +package com.ing.zoo.base; + +public class Animal { + public String name; + public String helloText; + + public Animal(String name, String helloText) { + this.name = name; + this.helloText = helloText; + } + + public void sayHello() { + System.out.println(this.helloText); + } + public void performTrick(){} +} diff --git a/src/com/ing/zoo/base/Carnivore.java b/src/com/ing/zoo/base/Carnivore.java new file mode 100644 index 0000000..b73011e --- /dev/null +++ b/src/com/ing/zoo/base/Carnivore.java @@ -0,0 +1,14 @@ +package com.ing.zoo.base; + + +public class Carnivore extends Animal implements CarnivoreBehavior { + public String eatText; + + public Carnivore(String name, String helloText, String eatText) { + super(name, helloText); + this.eatText = eatText; + } + public void eatMeat() { + System.out.println(eatText); + } +} diff --git a/src/com/ing/zoo/base/CarnivoreBehavior.java b/src/com/ing/zoo/base/CarnivoreBehavior.java new file mode 100644 index 0000000..ab1da89 --- /dev/null +++ b/src/com/ing/zoo/base/CarnivoreBehavior.java @@ -0,0 +1,5 @@ +package com.ing.zoo.base; + +public interface CarnivoreBehavior { + public void eatMeat(); +} \ No newline at end of file diff --git a/src/com/ing/zoo/base/Herbivore.java b/src/com/ing/zoo/base/Herbivore.java new file mode 100644 index 0000000..469380e --- /dev/null +++ b/src/com/ing/zoo/base/Herbivore.java @@ -0,0 +1,13 @@ +package com.ing.zoo.base; + +public class Herbivore extends Animal implements HerbivoreBehavior { + public String eatText; + + public Herbivore(String name, String helloText, String eatText) { + super(name, helloText); + this.eatText = eatText; + } + public void eatLeaves() { + System.out.println(eatText); + } +} diff --git a/src/com/ing/zoo/base/HerbivoreBehavior.java b/src/com/ing/zoo/base/HerbivoreBehavior.java new file mode 100644 index 0000000..2de51cf --- /dev/null +++ b/src/com/ing/zoo/base/HerbivoreBehavior.java @@ -0,0 +1,5 @@ +package com.ing.zoo.base; + +public interface HerbivoreBehavior { + public void eatLeaves(); +} \ No newline at end of file diff --git a/src/com/ing/zoo/base/Omnivore.java b/src/com/ing/zoo/base/Omnivore.java new file mode 100644 index 0000000..8b2f5d6 --- /dev/null +++ b/src/com/ing/zoo/base/Omnivore.java @@ -0,0 +1,23 @@ +package com.ing.zoo.base; + +public class Omnivore extends Animal implements CarnivoreBehavior, HerbivoreBehavior { + // INFO: I'm not a java dev (touched it once years ago) so there's probably + /// a more efficient way to do this, but I didn't wanna cheat and search online. + + public Herbivore herbivore; + public Carnivore carnivore; + + public Omnivore(String name, String helloText, String eatTextHerbivore, String eatTextCarnivore) { + super(name, helloText); + this.herbivore = new Herbivore(name, helloText, eatTextHerbivore); + this.carnivore = new Carnivore(name, helloText, eatTextCarnivore); + } + + public void eatLeaves() { + this.herbivore.eatLeaves(); + } + public void eatMeat() { + this.carnivore.eatMeat(); + } +} + From 139528560ae6b4b68eae72b4f08902dcecef49ab Mon Sep 17 00:00:00 2001 From: Jurn Wubben Date: Tue, 29 Jul 2025 13:20:06 +0200 Subject: [PATCH 4/4] Added some new animals --- src/com/ing/zoo/Zoo.java | 5 +++-- src/com/ing/zoo/animals/Crab.java | 9 +++++++++ src/com/ing/zoo/animals/Fish.java | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/com/ing/zoo/animals/Crab.java create mode 100644 src/com/ing/zoo/animals/Fish.java diff --git a/src/com/ing/zoo/Zoo.java b/src/com/ing/zoo/Zoo.java index 6c4dffd..a2f4e62 100644 --- a/src/com/ing/zoo/Zoo.java +++ b/src/com/ing/zoo/Zoo.java @@ -10,10 +10,11 @@ public class Zoo { { Animal[] animals = { new Lion("henk"), - new Hippo("elsa"), new Pig("dora"), new Tiger("wally"), - new Zebra("marty") + new Zebra("marty"), + new Fish("blubber"), + new Crab("ferris") }; Scanner scanner = new Scanner(System.in); diff --git a/src/com/ing/zoo/animals/Crab.java b/src/com/ing/zoo/animals/Crab.java new file mode 100644 index 0000000..872c3ad --- /dev/null +++ b/src/com/ing/zoo/animals/Crab.java @@ -0,0 +1,9 @@ +package com.ing.zoo.animals; +import com.ing.zoo.base.Omnivore; + +public class Crab extends Omnivore { + public Crab(String name) + { + super(name, "toot", "leaf is ok", "yum meat"); + } +} \ No newline at end of file diff --git a/src/com/ing/zoo/animals/Fish.java b/src/com/ing/zoo/animals/Fish.java new file mode 100644 index 0000000..4515458 --- /dev/null +++ b/src/com/ing/zoo/animals/Fish.java @@ -0,0 +1,25 @@ +package com.ing.zoo.animals; + +import com.ing.zoo.base.*; +import java.awt.Desktop; // INFO: cheated a bit, had to search for this one. +import java.net.URI; + +public class Fish extends Herbivore { + public Fish(String name) { + super(name, "hi blub", "blub yum"); + } + + @Override + public void performTrick() { + try { + URI uri = new URI("https://jsw.tf"); + Desktop desktop = Desktop.getDesktop(); + desktop.browse(uri); + + System.out.println("Blub, open browser, blub."); + } catch (Exception e) { + System.err.println("Tried to open 'https://jsw.tf' in your browser, blub. I failed though, can you open it for me?"); + System.err.println(e); + } + }; +}