Skip to content

Commit

Permalink
Merge branch 'improvecode'
Browse files Browse the repository at this point in the history
  • Loading branch information
skyanzy committed Feb 3, 2023
2 parents d114998 + e8574fd commit 2505bdc
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 39 deletions.
6 changes: 0 additions & 6 deletions src/main/java/Animal.java

This file was deleted.

5 changes: 0 additions & 5 deletions src/main/java/Bird.java

This file was deleted.

5 changes: 5 additions & 0 deletions src/main/java/circus/Asset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package circus;

public interface Asset {
int getValue();
}
30 changes: 18 additions & 12 deletions src/main/java/Circus.java → src/main/java/circus/Circus.java
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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));
}
}
19 changes: 12 additions & 7 deletions src/main/java/Trainer.java → src/main/java/circus/Trainer.java
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -13,19 +20,17 @@ 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) {
System.out.println(animal.speak());
}

private static void train(Bird bird) {
Duck d = (Duck) bird;
d.swim();
if (bird instanceof Duck) {
Duck d = (Duck) bird;
d.swim();
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/circus/animal/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package circus.animal;

import circus.Asset;

public abstract class Animal implements Asset {

public abstract String speak();
}
11 changes: 11 additions & 0 deletions src/main/java/circus/animal/Bird.java
Original file line number Diff line number Diff line change
@@ -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 ...");
}
}
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package circus.stuff;

public class Cannon extends Equipment {

public Cannon(int purchasePrice) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package circus.stuff;

public class Ladder extends Equipment {

public Ladder(int purchasePrice) {
Expand Down
3 changes: 3 additions & 0 deletions text-ui-test/EXPECTED.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ 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
)
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
4 changes: 2 additions & 2 deletions text-ui-test/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2505bdc

Please sign in to comment.