-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement analyzer for salary-calculator
- Loading branch information
1 parent
054f556
commit 9745415
Showing
36 changed files
with
738 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
73 changes: 73 additions & 0 deletions
73
src/main/java/analyzer/exercises/salarycalculator/SalaryCalculatorAnalyzer.java
This file contains 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,73 @@ | ||
package analyzer.exercises.salarycalculator; | ||
|
||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.body.MethodDeclaration; | ||
import com.github.javaparser.ast.expr.ConditionalExpr; | ||
import com.github.javaparser.ast.expr.MethodCallExpr; | ||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter; | ||
|
||
import analyzer.Analyzer; | ||
import analyzer.OutputCollector; | ||
import analyzer.Solution; | ||
import analyzer.comments.ExemplarSolution; | ||
import analyzer.comments.ReuseCode; | ||
|
||
/** | ||
* The {@link SalaryCalculatorAnalyzer} is the analyzer implementation for the {@code salary-calculator} practice exercise. | ||
* It extends from the {@link VoidVisitorAdapter} and uses the visitor pattern to traverse each compilation unit. | ||
* | ||
* @see <a href="https://github.com/exercism/java/tree/main/exercises/concept/salary-calculator">The salary-calculator exercise on the Java track</a> | ||
*/ | ||
public class SalaryCalculatorAnalyzer extends VoidVisitorAdapter<OutputCollector> implements Analyzer { | ||
private static final String EXERCISE_NAME = "Salary Calculator"; | ||
private static final String SALARY_MULTIPLIER = "salaryMultiplier"; | ||
private static final String BONUS_MULTIPLIER = "bonusMultiplier"; | ||
private static final String FINAL_SALARY = "finalSalary"; | ||
private static final String BONUS_FOR_PRODUCTS_SOLD = "bonusForProductsSold"; | ||
private boolean essentialCommentAdded = false; | ||
|
||
@Override | ||
public void analyze(Solution solution, OutputCollector output) { | ||
for (CompilationUnit compilationUnit : solution.getCompilationUnits()) { | ||
compilationUnit.accept(this, output); | ||
} | ||
|
||
if (output.getComments().isEmpty()) { | ||
output.addComment(new ExemplarSolution(EXERCISE_NAME)); | ||
} | ||
} | ||
|
||
@Override | ||
public void visit(MethodDeclaration node, OutputCollector output) { | ||
if (!essentialCommentAdded && isMethodThatMustContainTernaryOperators(node) && doesNotHasTernaryOperators(node)) { | ||
output.addComment(new UseTernaryOperators(node.getNameAsString())); | ||
essentialCommentAdded = true; | ||
} | ||
|
||
if (!essentialCommentAdded && node.getNameAsString().equals(BONUS_FOR_PRODUCTS_SOLD) && doesNotCallMethod(node, BONUS_MULTIPLIER)) { | ||
output.addComment(new ReuseCode(BONUS_FOR_PRODUCTS_SOLD, BONUS_MULTIPLIER)); | ||
} | ||
|
||
if (!essentialCommentAdded && node.getNameAsString().equals(FINAL_SALARY) && doesNotCallMethod(node, SALARY_MULTIPLIER)) { | ||
output.addComment(new ReuseCode(FINAL_SALARY, SALARY_MULTIPLIER)); | ||
} | ||
|
||
if (!essentialCommentAdded && node.getNameAsString().equals(FINAL_SALARY) && doesNotCallMethod(node, BONUS_FOR_PRODUCTS_SOLD)) { | ||
output.addComment(new ReuseCode(FINAL_SALARY, BONUS_FOR_PRODUCTS_SOLD)); | ||
} | ||
|
||
super.visit(node, output); | ||
} | ||
|
||
private static boolean isMethodThatMustContainTernaryOperators(MethodDeclaration node) { | ||
return node.getNameAsString().equals(SALARY_MULTIPLIER) || node.getNameAsString().equals(BONUS_MULTIPLIER) || node.getNameAsString().equals(FINAL_SALARY); | ||
} | ||
|
||
private static boolean doesNotHasTernaryOperators(MethodDeclaration node) { | ||
return node.findAll(ConditionalExpr.class).isEmpty(); | ||
} | ||
|
||
private static boolean doesNotCallMethod(MethodDeclaration node, String otherMethodName) { | ||
return node.findAll(MethodCallExpr.class, x -> x.getNameAsString().contains(otherMethodName)).isEmpty(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/analyzer/exercises/salarycalculator/UseTernaryOperators.java
This file contains 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,32 @@ | ||
package analyzer.exercises.salarycalculator; | ||
|
||
import java.util.Map; | ||
|
||
import analyzer.Comment; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/salary-calculator/use_ternary_operators.md">Markdown Template</a> | ||
*/ | ||
class UseTernaryOperators extends Comment { | ||
private final String inMethod; | ||
|
||
public UseTernaryOperators(String inMethod) { | ||
this.inMethod = inMethod; | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return "java.salary-calculator.use_ternary_operators"; | ||
} | ||
|
||
@Override | ||
public Map<String, String> getParameters() { | ||
return Map.of( | ||
"inMethod", this.inMethod); | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.ESSENTIAL; | ||
} | ||
} |
This file contains 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
...resources/analyzer/AnalyzerIntegrationTest.salarycalculator.ExemplarSolution.approved.txt
This file contains 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": "Salary Calculator" | ||
}, | ||
"type": "celebratory" | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
...nalyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseBonusForProductsSold.approved.txt
This file contains 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,17 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.general.reuse_code", | ||
"params": { | ||
"callingMethod": "finalSalary", | ||
"methodToCall": "bonusForProductsSold" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
...ces/analyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseBonusMultiplier.approved.txt
This file contains 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,17 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.general.reuse_code", | ||
"params": { | ||
"callingMethod": "bonusForProductsSold", | ||
"methodToCall": "bonusMultiplier" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
...es/analyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseSalaryMultiplier.approved.txt
This file contains 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,17 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.general.reuse_code", | ||
"params": { | ||
"callingMethod": "finalSalary", | ||
"methodToCall": "salaryMultiplier" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
33 changes: 33 additions & 0 deletions
33
...ces/analyzer/AnalyzerIntegrationTest.salarycalculator.NotReusingMethodsAtAll.approved.txt
This file contains 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,33 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.general.reuse_code", | ||
"params": { | ||
"callingMethod": "bonusForProductsSold", | ||
"methodToCall": "bonusMultiplier" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.reuse_code", | ||
"params": { | ||
"callingMethod": "finalSalary", | ||
"methodToCall": "salaryMultiplier" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.reuse_code", | ||
"params": { | ||
"callingMethod": "finalSalary", | ||
"methodToCall": "bonusForProductsSold" | ||
}, | ||
"type": "actionable" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
16 changes: 16 additions & 0 deletions
16
...ntegrationTest.salarycalculator.NotUsingTernaryOperatorsAndNotReusingMethods.approved.txt
This file contains 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,16 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.salary-calculator.use_ternary_operators", | ||
"params": { | ||
"inMethod": "salaryMultiplier" | ||
}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
16 changes: 16 additions & 0 deletions
16
...lyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsAtAll.approved.txt
This file contains 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,16 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.salary-calculator.use_ternary_operators", | ||
"params": { | ||
"inMethod": "salaryMultiplier" | ||
}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
16 changes: 16 additions & 0 deletions
16
...erIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnBonusMultiplier.approved.txt
This file contains 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,16 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.salary-calculator.use_ternary_operators", | ||
"params": { | ||
"inMethod": "bonusMultiplier" | ||
}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
16 changes: 16 additions & 0 deletions
16
...alyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnFinalSalary.approved.txt
This file contains 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,16 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.salary-calculator.use_ternary_operators", | ||
"params": { | ||
"inMethod": "finalSalary" | ||
}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
16 changes: 16 additions & 0 deletions
16
...rIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnSalaryMultiplier.approved.txt
This file contains 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,16 @@ | ||
{ | ||
"comments": [ | ||
{ | ||
"comment": "java.salary-calculator.use_ternary_operators", | ||
"params": { | ||
"inMethod": "salaryMultiplier" | ||
}, | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.feedback_request", | ||
"params": {}, | ||
"type": "informative" | ||
} | ||
] | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/resources/scenarios/salary-calculator/ExemplarSolution.java
This file contains 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,21 @@ | ||
package scenarios.salarycalculator; | ||
|
||
public class SalaryCalculator { | ||
|
||
public double salaryMultiplier(int daysSkipped) { | ||
return daysSkipped < 5 ? 1 : 0.85; | ||
} | ||
|
||
public int bonusMultiplier(int productsSold) { | ||
return productsSold < 20 ? 10 : 13; | ||
} | ||
|
||
public double bonusForProductsSold (int productsSold) { | ||
return productsSold * bonusMultiplier(productsSold); | ||
} | ||
|
||
public double finalSalary (int daysSkipped, int productsSold) { | ||
double finalSalary = 1000.0 * salaryMultiplier(daysSkipped) + bonusForProductsSold(productsSold); | ||
return finalSalary > 2000.0 ? 2000.0 : finalSalary; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/resources/scenarios/salary-calculator/NoReuseBonusForProductsSold.java
This file contains 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,21 @@ | ||
package scenarios.salarycalculator; | ||
|
||
public class SalaryCalculator { | ||
|
||
public double salaryMultiplier(int daysSkipped) { | ||
return daysSkipped < 5 ? 1 : 0.85; | ||
} | ||
|
||
public int bonusMultiplier(int productsSold) { | ||
return productsSold < 20 ? 10 : 13; | ||
} | ||
|
||
public double bonusForProductsSold (int productsSold) { | ||
return productsSold * bonusMultiplier(productsSold); | ||
} | ||
|
||
public double finalSalary (int daysSkipped, int productsSold) { | ||
double finalSalary = 1000.0 * salaryMultiplier(daysSkipped) + productsSold * bonusMultiplier(productsSold); | ||
return finalSalary > 2000.0 ? 2000.0 : finalSalary; | ||
} | ||
} |
Oops, something went wrong.