diff --git a/src/main/java/analyzer/AnalyzerRoot.java b/src/main/java/analyzer/AnalyzerRoot.java index 6e249ec3..b7cb7a2f 100644 --- a/src/main/java/analyzer/AnalyzerRoot.java +++ b/src/main/java/analyzer/AnalyzerRoot.java @@ -12,6 +12,7 @@ import analyzer.exercises.secrets.SecretsAnalyzer; import analyzer.exercises.twofer.TwoferAnalyzer; import analyzer.exercises.wizardsandwarriors.WizardsAndWarriorsAnalyzer; +import analyzer.exercises.wizardsandwarriors2.WizardsAndWarriors2Analyzer; import java.util.ArrayList; import java.util.List; @@ -33,7 +34,6 @@ private AnalyzerRoot() { */ public static Output analyze(Solution solution) { var outputBuilder = new OutputBuilder(); - for (Analyzer analyzer : createAnalyzers(solution.getSlug())) { analyzer.analyze(solution, outputBuilder); } @@ -49,7 +49,6 @@ private static List createAnalyzers(String slug) { var analyzers = new ArrayList(); analyzers.add(new GlobalAnalyzer()); - switch (slug) { case "annalyns-infiltration" -> analyzers.add(new AnnalynsInfiltrationAnalyzer()); case "hamming" -> analyzers.add(new HammingAnalyzer()); @@ -61,6 +60,7 @@ private static List createAnalyzers(String slug) { case "secrets" -> analyzers.add(new SecretsAnalyzer()); case "two-fer" -> analyzers.add(new TwoferAnalyzer()); case "wizards-and-warriors" -> analyzers.add(new WizardsAndWarriorsAnalyzer()); + case "wizards-and-warriors-2" -> analyzers.add(new WizardsAndWarriors2Analyzer()); } return List.copyOf(analyzers); diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedThreeParameters.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedThreeParameters.java new file mode 100644 index 00000000..73e327bd --- /dev/null +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedThreeParameters.java @@ -0,0 +1,16 @@ +package analyzer.exercises.wizardsandwarriors2; + +import analyzer.Comment; + +public class ReuseCodeHardcodedThreeParameters extends Comment { + + @Override + public String getKey() { + return "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters"; + } + + @Override + public Type getType() { + return Type.ACTIONABLE; + } +} diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedTwoParameters.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedTwoParameters.java new file mode 100644 index 00000000..878946a9 --- /dev/null +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/ReuseCodeHardcodedTwoParameters.java @@ -0,0 +1,16 @@ +package analyzer.exercises.wizardsandwarriors2; + +import analyzer.Comment; + +public class ReuseCodeHardcodedTwoParameters extends Comment { + + @Override + public String getKey() { + return "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters"; + } + + @Override + public Type getType() { + return Type.ACTIONABLE; + } +} diff --git a/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java new file mode 100644 index 00000000..af860661 --- /dev/null +++ b/src/main/java/analyzer/exercises/wizardsandwarriors2/WizardsAndWarriors2Analyzer.java @@ -0,0 +1,87 @@ +package analyzer.exercises.wizardsandwarriors2; + +import analyzer.Analyzer; +import analyzer.OutputCollector; +import analyzer.Solution; +import analyzer.comments.ExemplarSolution; +import analyzer.comments.PreferStringConcatenation; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.body.Parameter; +import com.github.javaparser.ast.expr.MethodCallExpr; +import com.github.javaparser.ast.visitor.VoidVisitorAdapter; + +import java.util.List; + +/** + * The {@link WizardsAndWarriors2Analyzer} is the analyzer implementation for the {@code wizards-and-warriors-2} concept exercise. + * + * @see The wizards-and-warriors exercise on the Java track + */ +public class WizardsAndWarriors2Analyzer extends VoidVisitorAdapter implements Analyzer { + + private static final String EXERCISE_NAME = "Wizards and Warriors 2"; + private static final String GAME_MASTER = "GameMaster"; + private static final String DESCRIBE = "describe"; + private static final String FORMAT = "format"; + private static final String DESTINATION = "Destination"; + private static final String TRAVEL_METHOD = "TravelMethod"; + private static final String CHARACTER = "Character"; + + @Override + public void analyze(Solution solution, OutputCollector output) { + + for (var compilationUnit : solution.getCompilationUnits()) { + compilationUnit.getClassByName(GAME_MASTER).ifPresent(c -> c.accept(this, output)); + } + + if (output.getComments().isEmpty()) { + output.addComment(new ExemplarSolution(EXERCISE_NAME)); + } + } + + @Override + public void visit(MethodDeclaration node, OutputCollector output) { + + if(!node.getNameAsString().equals(DESCRIBE)) { + return; + } + + if(node.getParameters().size() == 2 && !reuseMethod(node, 2)) { + output.addComment(new ReuseCodeHardcodedTwoParameters()); + } + + if(node.getParameters().size() == 3 && !reuseMethod(node, 3)) { + output.addComment(new ReuseCodeHardcodedThreeParameters()); + } + + if(useStringFormat(node)) { + output.addComment(new PreferStringConcatenation()); + } + + super.visit(node, output); + } + + private static boolean reuseMethod(MethodDeclaration node, int paramCount) { + + List params = node.getParameters().stream().map(Parameter::getTypeAsString).toList(); + List describeCalls = node.findAll(MethodCallExpr.class).stream() + .filter(m -> m.getNameAsString().equals(DESCRIBE)) + .toList(); + + if (paramCount == 2 && params.contains(DESTINATION) && params.contains(CHARACTER)) { + return describeCalls.size() == 1 || describeCalls.size() == 3; + } + + if (paramCount == 3 && params.contains(DESTINATION) && params.contains(TRAVEL_METHOD) && params.contains(CHARACTER)) { + return describeCalls.size() == 3; + } + + return false; + } + + private static boolean useStringFormat(MethodDeclaration node) { + return node.findAll(MethodCallExpr.class).stream() + .anyMatch(m -> m.getNameAsString().contains(FORMAT)); + } + +} diff --git a/src/test/java/analyzer/AnalyzerIntegrationTest.java b/src/test/java/analyzer/AnalyzerIntegrationTest.java index 26315c72..5f193f3c 100644 --- a/src/test/java/analyzer/AnalyzerIntegrationTest.java +++ b/src/test/java/analyzer/AnalyzerIntegrationTest.java @@ -205,4 +205,20 @@ void salarycalculator(String scenario) throws IOException { Approvals.verify(serialize(output.analysis()), Approvals.NAMES.withParameters(scenario)); } + + @ParameterizedTest + @ValueSource(strings = { + "ExemplarSolution", + "NotReuseMethod", + "PartialReuseOfMethod", + "UseStringFormat", + "UseStringFormatAndNotReuseMethod", + }) + void wizardsandwarriors2(String scenario) throws IOException { + var path = Path.of("wizards-and-warriors-2", scenario + ".java"); + var solution = new SolutionFromFiles("wizards-and-warriors-2", SCENARIOS.resolve(path)); + var output = AnalyzerRoot.analyze(solution); + + Approvals.verify(serialize(output.analysis()), Approvals.NAMES.withParameters(scenario)); + } } diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.ExemplarSolution.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.ExemplarSolution.approved.txt new file mode 100644 index 00000000..a9ee72ff --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.ExemplarSolution.approved.txt @@ -0,0 +1,11 @@ +{ + "comments": [ + { + "comment": "java.general.exemplar", + "params": { + "exerciseName": "Wizards and Warriors 2" + }, + "type": "celebratory" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotReuseMethod.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotReuseMethod.approved.txt new file mode 100644 index 00000000..cc5cfba4 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.NotReuseMethod.approved.txt @@ -0,0 +1,19 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialReuseOfMethod.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialReuseOfMethod.approved.txt new file mode 100644 index 00000000..234e21fe --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.PartialReuseOfMethod.approved.txt @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormat.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormat.approved.txt new file mode 100644 index 00000000..b01ad7f7 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormat.approved.txt @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.general.prefer_string_concatenation", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotReuseMethod.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotReuseMethod.approved.txt new file mode 100644 index 00000000..fac41fcc --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.wizardsandwarriors2.UseStringFormatAndNotReuseMethod.approved.txt @@ -0,0 +1,24 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.prefer_string_concatenation", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/ExemplarSolution.java b/src/test/resources/scenarios/wizards-and-warriors-2/ExemplarSolution.java new file mode 100644 index 00000000..e5b69038 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/ExemplarSolution.java @@ -0,0 +1,25 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return describe(character) + " " + describe(travelMethod) + " " + describe(destination); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } + } \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/NotReuseMethod.java b/src/test/resources/scenarios/wizards-and-warriors-2/NotReuseMethod.java new file mode 100644 index 00000000..97c00b5b --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/NotReuseMethod.java @@ -0,0 +1,29 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + (travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. " : "You're traveling to your destination on horseback. ") + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(Character character, Destination destination) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + "You're traveling to your destination by walking. " + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } +} \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/PartialReuseOfMethod.java b/src/test/resources/scenarios/wizards-and-warriors-2/PartialReuseOfMethod.java new file mode 100644 index 00000000..6e9efe84 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/PartialReuseOfMethod.java @@ -0,0 +1,25 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return describe(character) + " " + describe(travelMethod) + " " + describe(destination); + } + + public String describe(Character character, Destination destination) { + return describe(character) + " " + describe(destination) + " You're traveling to your destination by walking."; + } +} \ No newline at end of file diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormat.java b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormat.java new file mode 100644 index 00000000..e32fd7d4 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormat.java @@ -0,0 +1,28 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level %d %s with %d hit points.".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()); + } + + public String describe(Destination destination) { + return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "%s %s %s".formatted(describe(character), describe(travelMethod), describe(destination)); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } +} diff --git a/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotReuseMethod.java b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotReuseMethod.java new file mode 100644 index 00000000..f3fc84e3 --- /dev/null +++ b/src/test/resources/scenarios/wizards-and-warriors-2/UseStringFormatAndNotReuseMethod.java @@ -0,0 +1,35 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level %d %s with %d hit points.".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()); + } + + public String describe(Destination destination) { + return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "You're a level %d %s with %d hit points. ".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()) + + (travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. " + : "You're traveling to your destination on horseback. ") + + "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(Character character, Destination destination) { + return "You're a level %d %s with %d hit points. You're traveling to your destination by walking. You've arrived at %s, which has %d inhabitants." + .formatted(character.getLevel(), character.getCharacterClass(), character.getHitPoints(), + destination.getName(), destination.getInhabitants()); + } +} diff --git a/tests/wizards-and-warriors-2/exemplar-solution/.meta/config.json b/tests/wizards-and-warriors-2/exemplar-solution/.meta/config.json new file mode 100644 index 00000000..decfa680 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/.meta/config.json @@ -0,0 +1,32 @@ +{ + "authors": [ + "sougat818" + ], + "contributors": [ + "jagdish-15", + "sanderploegsma" + ], + "files": { + "solution": [ + "src/main/java/GameMaster.java" + ], + "test": [ + "src/test/java/GameMasterTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/GameMaster.java" + ], + "editor": [ + "src/main/java/Character.java", + "src/main/java/Destination.java", + "src/main/java/TravelMethod.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/wizards-and-warriors-2" + ], + "blurb": "Learn about method overloading by extending your favorite RPG." +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/exemplar-solution/expected_analysis.json b/tests/wizards-and-warriors-2/exemplar-solution/expected_analysis.json new file mode 100644 index 00000000..a9b48769 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/expected_analysis.json @@ -0,0 +1,11 @@ +{ + "comments": [ + { + "comment": "java.general.exemplar", + "params": { + "exerciseName": "Wizards and Warriors 2" + }, + "type": "celebratory" + } + ] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/exemplar-solution/expected_tags.json b/tests/wizards-and-warriors-2/exemplar-solution/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/exemplar-solution/src/main/java/GameMaster.java b/tests/wizards-and-warriors-2/exemplar-solution/src/main/java/GameMaster.java new file mode 100644 index 00000000..8a513f51 --- /dev/null +++ b/tests/wizards-and-warriors-2/exemplar-solution/src/main/java/GameMaster.java @@ -0,0 +1,25 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return describe(character) + " " + describe(travelMethod) + " " + describe(destination); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-reuse-method/.meta/config.json b/tests/wizards-and-warriors-2/not-reuse-method/.meta/config.json new file mode 100644 index 00000000..decfa680 --- /dev/null +++ b/tests/wizards-and-warriors-2/not-reuse-method/.meta/config.json @@ -0,0 +1,32 @@ +{ + "authors": [ + "sougat818" + ], + "contributors": [ + "jagdish-15", + "sanderploegsma" + ], + "files": { + "solution": [ + "src/main/java/GameMaster.java" + ], + "test": [ + "src/test/java/GameMasterTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/GameMaster.java" + ], + "editor": [ + "src/main/java/Character.java", + "src/main/java/Destination.java", + "src/main/java/TravelMethod.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/wizards-and-warriors-2" + ], + "blurb": "Learn about method overloading by extending your favorite RPG." +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-reuse-method/expected_analysis.json b/tests/wizards-and-warriors-2/not-reuse-method/expected_analysis.json new file mode 100644 index 00000000..94c8a656 --- /dev/null +++ b/tests/wizards-and-warriors-2/not-reuse-method/expected_analysis.json @@ -0,0 +1,19 @@ +{ + "comments": [ + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_three_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.wizards-and-warriors-2.reuse_code_hardcoded_two_parameters", + "params": {}, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-reuse-method/expected_tags.json b/tests/wizards-and-warriors-2/not-reuse-method/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/wizards-and-warriors-2/not-reuse-method/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/not-reuse-method/src/main/java/GameMaster.java b/tests/wizards-and-warriors-2/not-reuse-method/src/main/java/GameMaster.java new file mode 100644 index 00000000..97c00b5b --- /dev/null +++ b/tests/wizards-and-warriors-2/not-reuse-method/src/main/java/GameMaster.java @@ -0,0 +1,29 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points."; + } + + public String describe(Destination destination) { + return "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + (travelMethod == TravelMethod.WALKING ? "You're traveling to your destination by walking. " : "You're traveling to your destination on horseback. ") + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } + + public String describe(Character character, Destination destination) { + return "You're a level " + character.getLevel() + " " + character.getCharacterClass() + " with " + character.getHitPoints() + " hit points. " + + "You're traveling to your destination by walking. " + + "You've arrived at " + destination.getName() + ", which has " + destination.getInhabitants() + " inhabitants."; + } +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/.meta/config.json b/tests/wizards-and-warriors-2/use-string-format/.meta/config.json new file mode 100644 index 00000000..decfa680 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/.meta/config.json @@ -0,0 +1,32 @@ +{ + "authors": [ + "sougat818" + ], + "contributors": [ + "jagdish-15", + "sanderploegsma" + ], + "files": { + "solution": [ + "src/main/java/GameMaster.java" + ], + "test": [ + "src/test/java/GameMasterTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/GameMaster.java" + ], + "editor": [ + "src/main/java/Character.java", + "src/main/java/Destination.java", + "src/main/java/TravelMethod.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/wizards-and-warriors-2" + ], + "blurb": "Learn about method overloading by extending your favorite RPG." +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/expected_analysis.json b/tests/wizards-and-warriors-2/use-string-format/expected_analysis.json new file mode 100644 index 00000000..2b150682 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/expected_analysis.json @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.general.prefer_string_concatenation", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/expected_tags.json b/tests/wizards-and-warriors-2/use-string-format/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/wizards-and-warriors-2/use-string-format/src/main/java/GameMaster.java b/tests/wizards-and-warriors-2/use-string-format/src/main/java/GameMaster.java new file mode 100644 index 00000000..e32fd7d4 --- /dev/null +++ b/tests/wizards-and-warriors-2/use-string-format/src/main/java/GameMaster.java @@ -0,0 +1,28 @@ +public class GameMaster { + + public String describe(Character character) { + return "You're a level %d %s with %d hit points.".formatted(character.getLevel(), + character.getCharacterClass(), character.getHitPoints()); + } + + public String describe(Destination destination) { + return "You've arrived at %s, which has %d inhabitants.".formatted(destination.getName(), + destination.getInhabitants()); + } + + public String describe(TravelMethod travelMethod) { + if (travelMethod == TravelMethod.WALKING) { + return "You're traveling to your destination by walking."; + } + return "You're traveling to your destination on horseback."; + + } + + public String describe(Character character, Destination destination, TravelMethod travelMethod) { + return "%s %s %s".formatted(describe(character), describe(travelMethod), describe(destination)); + } + + public String describe(Character character, Destination destination) { + return describe(character, destination, TravelMethod.WALKING); + } +}