-
-
Notifications
You must be signed in to change notification settings - Fork 25
Adding analyzer for need for speed concept exercise
#134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
manumafe98
merged 9 commits into
exercism:main
from
manumafe98:ImplementAnalyzerForNeedForSpeed
Feb 16, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a2d2aa4
Adding analyzer for need for speed concept exercise
manumafe98 1af9155
Adding new files
manumafe98 4b589cc
Applying suggestions
manumafe98 3de0c70
Adding renamed files
manumafe98 278670f
Fixing smoke tests expected_analysis and tags indentation
manumafe98 cbfed0b
Update src/main/java/analyzer/exercises/needforspeed/NeedForSpeedAnal…
manumafe98 c30dfa0
Fixing tests as well
manumafe98 b41507e
Fixing typo
manumafe98 92e12b1
Merge branch 'main' into ImplementAnalyzerForNeedForSpeed
manumafe98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/analyzer/exercises/needforspeed/AvoidConditionalLogic.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package analyzer.exercises.needforspeed; | ||
|
|
||
| import analyzer.Comment; | ||
|
|
||
| /** | ||
| * @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/need-for-speed/avoid_conditional_logic.md">Markdown Template</a> | ||
| */ | ||
| class AvoidConditionalLogic extends Comment { | ||
|
|
||
| @Override | ||
| public String getKey() { | ||
| return "java.need-for-speed.avoid_conditional_logic"; | ||
| } | ||
|
|
||
| @Override | ||
| public Type getType() { | ||
| return Type.ACTIONABLE; | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/analyzer/exercises/needforspeed/AvoidLoops.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package analyzer.exercises.needforspeed; | ||
|
|
||
| import analyzer.Comment; | ||
|
|
||
| /** | ||
| * @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/need-for-speed/avoid_loops.md">Markdown Template</a> | ||
| */ | ||
| class AvoidLoops extends Comment{ | ||
|
|
||
| @Override | ||
| public String getKey() { | ||
| return "java.need-for-speed.avoid_loops"; | ||
| } | ||
|
|
||
| @Override | ||
| public Type getType() { | ||
| return Type.ACTIONABLE; | ||
| } | ||
| } |
82 changes: 82 additions & 0 deletions
82
src/main/java/analyzer/exercises/needforspeed/NeedForSpeedAnalyzer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package analyzer.exercises.needforspeed; | ||
|
|
||
| import com.github.javaparser.ast.body.MethodDeclaration; | ||
| import com.github.javaparser.ast.expr.ConditionalExpr; | ||
| import com.github.javaparser.ast.visitor.VoidVisitorAdapter; | ||
| import com.github.javaparser.ast.stmt.IfStmt; | ||
| import com.github.javaparser.ast.stmt.Statement; | ||
|
|
||
| import analyzer.Analyzer; | ||
| import analyzer.OutputCollector; | ||
| import analyzer.Solution; | ||
| import analyzer.comments.ExemplarSolution; | ||
|
|
||
| /** | ||
| * The {@link NeedForSpeedAnalyzer} is the analyzer implementation for the {@code need-for-speed} practice exercise. | ||
| * It has two subclasses NeedForSpeedClassAnalyzer and RaceTrackClassAnalyzer that extend the {@link VoidVisitorAdapter} and use the visitor pattern to traverse each compilation unit. | ||
| * | ||
| * @see <a href="https://github.com/exercism/java/tree/main/exercises/concept/need-for-speed">The need-for-speed exercise on the Java track</a> | ||
| */ | ||
| public class NeedForSpeedAnalyzer implements Analyzer { | ||
| private static final String EXERCISE_NAME = "Need for Speed"; | ||
|
|
||
| @Override | ||
| public void analyze(Solution solution, OutputCollector output) { | ||
| var needForSpeedClassAnalyzer = new NeedForSpeedClassAnalyzer(); | ||
| var raceTrackClassAnalyzer = new RaceTrackClassAnalyzer(); | ||
|
|
||
| for (var compilationUnit : solution.getCompilationUnits()) { | ||
| compilationUnit.getClassByName("NeedForSpeed").ifPresent(c -> c.accept(needForSpeedClassAnalyzer, output)); | ||
| compilationUnit.getClassByName("RaceTrack").ifPresent(c -> c.accept(raceTrackClassAnalyzer, output)); | ||
| } | ||
|
|
||
| if (output.getComments().isEmpty()) { | ||
| output.addComment(new ExemplarSolution(EXERCISE_NAME)); | ||
| } | ||
| } | ||
|
|
||
| class NeedForSpeedClassAnalyzer extends VoidVisitorAdapter<OutputCollector> { | ||
|
|
||
| @Override | ||
| public void visit(MethodDeclaration node, OutputCollector output) { | ||
| if (node.getNameAsString().equals("batteryDrained") && hasConditional(node)) { | ||
| output.addComment(new AvoidConditionalLogic()); | ||
| } | ||
|
|
||
| super.visit(node, output); | ||
| } | ||
|
|
||
| private static boolean hasConditional(MethodDeclaration node) { | ||
| return node.getBody() | ||
| .map(body -> body.getStatements().stream() | ||
| .anyMatch(NeedForSpeedClassAnalyzer::isConditionalExpresion)) | ||
| .orElse(false); | ||
| } | ||
|
|
||
| private static boolean isConditionalExpresion(Statement statement) { | ||
| return !statement.findAll(IfStmt.class).isEmpty() || !statement.findAll(ConditionalExpr.class).isEmpty(); | ||
| } | ||
| } | ||
|
|
||
| class RaceTrackClassAnalyzer extends VoidVisitorAdapter<OutputCollector> { | ||
|
|
||
| @Override | ||
| public void visit(MethodDeclaration node, OutputCollector output) { | ||
| if (node.getNameAsString().equals("tryFinishTrack") && hasLoop(node)) { | ||
| output.addComment(new AvoidLoops()); | ||
| } | ||
|
|
||
| super.visit(node, output); | ||
| } | ||
|
|
||
| private static boolean hasLoop(MethodDeclaration node) { | ||
| return node.getBody() | ||
| .map(body -> body.getStatements().stream().anyMatch(RaceTrackClassAnalyzer::isLoopStatement)) | ||
| .orElse(false); | ||
| } | ||
|
|
||
| private static boolean isLoopStatement(Statement statement) { | ||
| return statement.isForStmt() || statement.isForEachStmt() || statement.isWhileStmt() || statement.isDoStmt(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...est/resources/analyzer/AnalyzerIntegrationTest.needforspeed.ExemplarSolution.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.general.exemplar", | ||
| "params": { | ||
| "exerciseName": "Need for Speed" | ||
| }, | ||
| "type": "celebratory" | ||
| } | ||
| ] | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/test/resources/analyzer/AnalyzerIntegrationTest.needforspeed.UsingForLoop.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.need-for-speed.avoid_loops", | ||
| "params": {}, | ||
| "type": "actionable" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
14 changes: 14 additions & 0 deletions
14
...est/resources/analyzer/AnalyzerIntegrationTest.needforspeed.UsingIfStatement.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.need-for-speed.avoid_conditional_logic", | ||
| "params": {}, | ||
| "type": "actionable" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/test/resources/analyzer/AnalyzerIntegrationTest.needforspeed.UsingTernary.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.need-for-speed.avoid_conditional_logic", | ||
| "params": {}, | ||
| "type": "actionable" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/test/resources/analyzer/AnalyzerIntegrationTest.needforspeed.UsingWhileLoop.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.need-for-speed.avoid_loops", | ||
| "params": {}, | ||
| "type": "actionable" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
56 changes: 56 additions & 0 deletions
56
src/test/resources/scenarios/need-for-speed/ExemplarSolution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package scenarios.neeedforspeed; | ||
|
|
||
| public class NeedForSpeed { | ||
| private int speed; | ||
| private int batteryDrain; | ||
| private int distance = 0; | ||
| private int battery = 100; | ||
|
|
||
| public NeedForSpeed(int speed, int batteryDrain) { | ||
| this.speed = speed; | ||
| this.batteryDrain = batteryDrain; | ||
| } | ||
|
|
||
| public static NeedForSpeed nitro() { | ||
| return new NeedForSpeed(50, 4); | ||
| } | ||
|
|
||
| public boolean batteryDrained() { | ||
| return battery < batteryDrain; | ||
| } | ||
|
|
||
| public int distanceDriven() { | ||
| return distance; | ||
| } | ||
|
|
||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
|
|
||
| public int getBatteryDrain() { | ||
| return batteryDrain; | ||
| } | ||
|
|
||
| public int getCurrentBattery() { | ||
| return battery; | ||
| } | ||
|
|
||
| public void drive() { | ||
| if (!batteryDrained()) { | ||
| battery -= batteryDrain; | ||
| distance += speed; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class RaceTrack { | ||
| private int distance; | ||
|
|
||
| RaceTrack(int distance) { | ||
| this.distance = distance; | ||
| } | ||
|
|
||
| public boolean tryFinishTrack(NeedForSpeed car) { | ||
| return ((double) distance / car.getSpeed()) <= (car.getCurrentBattery() / car.getBatteryDrain()); | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
src/test/resources/scenarios/need-for-speed/UsingForLoop.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package scenarios.neeedforspeed; | ||
|
|
||
| public class NeedForSpeed { | ||
| private int speed; | ||
| private int batteryDrain; | ||
| private int distance = 0; | ||
| private int battery = 100; | ||
|
|
||
| public NeedForSpeed(int speed, int batteryDrain) { | ||
| this.speed = speed; | ||
| this.batteryDrain = batteryDrain; | ||
| } | ||
|
|
||
| public static NeedForSpeed nitro() { | ||
| return new NeedForSpeed(50, 4); | ||
| } | ||
|
|
||
| public boolean batteryDrained() { | ||
| return battery < batteryDrain; | ||
| } | ||
|
|
||
| public int distanceDriven() { | ||
| return distance; | ||
| } | ||
|
|
||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
|
|
||
| public void drive() { | ||
| if (!batteryDrained()) { | ||
| battery -= batteryDrain; | ||
| distance += speed; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class RaceTrack { | ||
| private int distance; | ||
|
|
||
| RaceTrack(int distance) { | ||
| this.distance = distance; | ||
| } | ||
|
|
||
| public boolean tryFinishTrack(NeedForSpeed car) { | ||
| for (int i = 0; i < this.distance / car.getSpeed(); i++) { | ||
| car.drive(); | ||
| } | ||
|
|
||
| if (car.distanceDriven() >= distance) { | ||
| return true; | ||
| } else | ||
| return false; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.