From e8574fd950569d0c2cfd7a4406a2e2dea7fb5227 Mon Sep 17 00:00:00 2001 From: okkhoy Date: Fri, 3 Feb 2023 14:49:34 +0800 Subject: [PATCH] Improve code create packages create Asset interface use Asset to print value update Circus class to relfect the changes. --- src/main/java/Animal.java | 6 ---- src/main/java/Bird.java | 5 ---- src/main/java/circus/Asset.java | 5 ++++ src/main/java/{ => circus}/Circus.java | 30 +++++++++++-------- src/main/java/{ => circus}/Trainer.java | 19 +++++++----- src/main/java/circus/animal/Animal.java | 8 +++++ src/main/java/circus/animal/Bird.java | 11 +++++++ src/main/java/{ => circus/animal}/Duck.java | 7 +++++ src/main/java/{ => circus/animal}/Parrot.java | 7 +++++ src/main/java/{ => circus/stuff}/Cannon.java | 2 ++ .../java/{ => circus/stuff}/Equipment.java | 10 +++---- src/main/java/{ => circus/stuff}/Ladder.java | 2 ++ text-ui-test/EXPECTED.txt | 3 ++ text-ui-test/runtest.bat | 4 +-- text-ui-test/runtest.sh | 4 +-- 15 files changed, 84 insertions(+), 39 deletions(-) delete mode 100644 src/main/java/Animal.java delete mode 100644 src/main/java/Bird.java create mode 100644 src/main/java/circus/Asset.java rename src/main/java/{ => circus}/Circus.java (55%) rename src/main/java/{ => circus}/Trainer.java (65%) create mode 100644 src/main/java/circus/animal/Animal.java create mode 100644 src/main/java/circus/animal/Bird.java rename src/main/java/{ => circus/animal}/Duck.java (74%) rename src/main/java/{ => circus/animal}/Parrot.java (69%) rename src/main/java/{ => circus/stuff}/Cannon.java (89%) rename src/main/java/{ => circus/stuff}/Equipment.java (56%) rename src/main/java/{ => circus/stuff}/Ladder.java (89%) diff --git a/src/main/java/Animal.java b/src/main/java/Animal.java deleted file mode 100644 index be3612b..0000000 --- a/src/main/java/Animal.java +++ /dev/null @@ -1,6 +0,0 @@ -public class Animal { - - public String speak() { - return null; - } -} diff --git a/src/main/java/Bird.java b/src/main/java/Bird.java deleted file mode 100644 index 1125ec1..0000000 --- a/src/main/java/Bird.java +++ /dev/null @@ -1,5 +0,0 @@ -public class Bird extends Animal { - public void fly() { - System.out.println("Whee ..."); - } -} diff --git a/src/main/java/circus/Asset.java b/src/main/java/circus/Asset.java new file mode 100644 index 0000000..2b1afd1 --- /dev/null +++ b/src/main/java/circus/Asset.java @@ -0,0 +1,5 @@ +package circus; + +public interface Asset { + int getValue(); +} diff --git a/src/main/java/Circus.java b/src/main/java/circus/Circus.java similarity index 55% rename from src/main/java/Circus.java rename to src/main/java/circus/Circus.java index 07a30dd..e29b8c3 100644 --- a/src/main/java/Circus.java +++ b/src/main/java/circus/Circus.java @@ -1,3 +1,12 @@ +package circus; + +import circus.animal.Animal; +import circus.animal.Duck; +import circus.animal.Parrot; +import circus.stuff.Cannon; +import circus.stuff.Equipment; +import circus.stuff.Ladder; + public class Circus { private static Animal[] animals = { new Duck(), @@ -16,25 +25,22 @@ private static void makeAnimalsTalk() { } } - private static int calculateValue(Equipment[] equipments) { + private static int calculateAssetValue(Asset[] assets) { int total = 0; - for (Equipment e : equipments) { - if (e.getValue() <= 5) { - System.out.println("Ignoring low value item: " + e.getValue()); - } else { - total += e.getValue(); - System.out.println("Adding item value: " + e.getValue()); - // some - // more - // code - // here ... + for (Asset a : assets) { + if (a.getValue() <= 5) { + System.out.println("Ignoring low value item: " + a.getValue()); + continue; } + total += a.getValue(); + System.out.println("Adding item value: " + a.getValue()); } return total; } public static void main(String[] args) { makeAnimalsTalk(); - System.out.println("Total value of equipments " + calculateValue(equipments)); + System.out.println("Total value of animals " + calculateAssetValue(animals)); + System.out.println("Total value of equipments " + calculateAssetValue(equipments)); } } diff --git a/src/main/java/Trainer.java b/src/main/java/circus/Trainer.java similarity index 65% rename from src/main/java/Trainer.java rename to src/main/java/circus/Trainer.java index 4ba15c2..eb3e711 100644 --- a/src/main/java/Trainer.java +++ b/src/main/java/circus/Trainer.java @@ -1,3 +1,10 @@ +package circus; + +import circus.animal.Animal; +import circus.animal.Bird; +import circus.animal.Duck; +import circus.animal.Parrot; + public class Trainer { public static void main(String[] args) { Duck d = new Duck(); @@ -13,11 +20,7 @@ public static void main(String[] args) { getToSpeak(d2); train(new Duck()); - // train(new Parrot()); - - Animal a2 = new Animal(); - Bird b2 = new Bird(); - + // train(new animal.Parrot()); } private static void getToSpeak(Animal animal) { @@ -25,7 +28,9 @@ private static void getToSpeak(Animal animal) { } private static void train(Bird bird) { - Duck d = (Duck) bird; - d.swim(); + if (bird instanceof Duck) { + Duck d = (Duck) bird; + d.swim(); + } } } diff --git a/src/main/java/circus/animal/Animal.java b/src/main/java/circus/animal/Animal.java new file mode 100644 index 0000000..dc2422d --- /dev/null +++ b/src/main/java/circus/animal/Animal.java @@ -0,0 +1,8 @@ +package circus.animal; + +import circus.Asset; + +public abstract class Animal implements Asset { + + public abstract String speak(); +} diff --git a/src/main/java/circus/animal/Bird.java b/src/main/java/circus/animal/Bird.java new file mode 100644 index 0000000..2a78487 --- /dev/null +++ b/src/main/java/circus/animal/Bird.java @@ -0,0 +1,11 @@ +package circus.animal; + +public abstract class Bird extends Animal { + public String speak() { + return "tweet tweet"; + } + + public void fly() { + System.out.println("Whee ..."); + } +} diff --git a/src/main/java/Duck.java b/src/main/java/circus/animal/Duck.java similarity index 74% rename from src/main/java/Duck.java rename to src/main/java/circus/animal/Duck.java index 7492ccf..edac6d2 100644 --- a/src/main/java/Duck.java +++ b/src/main/java/circus/animal/Duck.java @@ -1,9 +1,16 @@ +package circus.animal; + public class Duck extends Bird { @Override public String speak() { return "Quack Quack"; } + @Override + public int getValue() { + return 10; + } + @Override public String toString() { return "I'm a Duck"; diff --git a/src/main/java/Parrot.java b/src/main/java/circus/animal/Parrot.java similarity index 69% rename from src/main/java/Parrot.java rename to src/main/java/circus/animal/Parrot.java index 30695a5..6432b2a 100644 --- a/src/main/java/Parrot.java +++ b/src/main/java/circus/animal/Parrot.java @@ -1,9 +1,16 @@ +package circus.animal; + public class Parrot extends Bird { @Override public String speak() { return "Polly wants a cracker"; } + @Override + public int getValue() { + return 20; + } + @Override public String toString() { return "I'm a parrot"; diff --git a/src/main/java/Cannon.java b/src/main/java/circus/stuff/Cannon.java similarity index 89% rename from src/main/java/Cannon.java rename to src/main/java/circus/stuff/Cannon.java index 7b0b252..71baccf 100644 --- a/src/main/java/Cannon.java +++ b/src/main/java/circus/stuff/Cannon.java @@ -1,3 +1,5 @@ +package circus.stuff; + public class Cannon extends Equipment { public Cannon(int purchasePrice) { diff --git a/src/main/java/Equipment.java b/src/main/java/circus/stuff/Equipment.java similarity index 56% rename from src/main/java/Equipment.java rename to src/main/java/circus/stuff/Equipment.java index 6ef8ffc..917fcef 100644 --- a/src/main/java/Equipment.java +++ b/src/main/java/circus/stuff/Equipment.java @@ -1,11 +1,11 @@ -public abstract class Equipment { +package circus.stuff; + +import circus.Asset; + +public abstract class Equipment implements Asset { protected int purchasePrice; public Equipment(int purchasePrice) { this.purchasePrice = purchasePrice; } - - public int getValue() { - return purchasePrice; - } } diff --git a/src/main/java/Ladder.java b/src/main/java/circus/stuff/Ladder.java similarity index 89% rename from src/main/java/Ladder.java rename to src/main/java/circus/stuff/Ladder.java index d26ca7a..56403bf 100644 --- a/src/main/java/Ladder.java +++ b/src/main/java/circus/stuff/Ladder.java @@ -1,3 +1,5 @@ +package circus.stuff; + public class Ladder extends Equipment { public Ladder(int purchasePrice) { diff --git a/text-ui-test/EXPECTED.txt b/text-ui-test/EXPECTED.txt index b61832f..19da732 100644 --- a/text-ui-test/EXPECTED.txt +++ b/text-ui-test/EXPECTED.txt @@ -2,6 +2,9 @@ I'm a Duck Quack Quack I'm a parrot Polly wants a cracker +Adding item value: 10 +Adding item value: 20 +Total value of animals 30 Adding item value: 25 Ignoring low value item: 1 Adding item value: 33 diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index 1d6bed0..02858c7 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -7,7 +7,7 @@ REM delete output from previous run del ACTUAL.TXT REM compile the code into the bin folder -javac -cp ..\src -Xlint:none -d ..\bin ..\src\main\java\*.java +javac -cp ..\src -Xlint:none -d ..\bin ..\src\main\java\circus\*.java ..\src\main\java\circus\animal\*.java ..\src\main\java\circus\stuff\*.java IF ERRORLEVEL 1 ( echo ********** BUILD FAILURE ********** exit /b 1 @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 ( REM no error here, errorlevel == 0 REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\bin Circus > ACTUAL.TXT +java -classpath ..\bin circus.Circus > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT \ No newline at end of file diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh index 3d5c6bd..6221f11 100644 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -13,14 +13,14 @@ then fi # compile the code into the bin folder, terminates if error occurred -if ! javac -cp ../src -Xlint:none -d ../bin ../src/main/java/*.java +if ! javac -cp ../src -Xlint:none -d ../bin ../src/main/java/circus/*.java ../src/main/java/circus/animal/*.java ../src/main/java/circus/stuff/*.java then echo "********** BUILD FAILURE **********" exit 1 fi # run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ../bin Circus > ACTUAL.TXT +java -classpath ../bin circus.Circus > ACTUAL.TXT # compare the output to the expected output diff ACTUAL.TXT EXPECTED.TXT