diff --git a/src/main/java/analyzer/AnalyzerRoot.java b/src/main/java/analyzer/AnalyzerRoot.java index edcb41e..6e249ec 100644 --- a/src/main/java/analyzer/AnalyzerRoot.java +++ b/src/main/java/analyzer/AnalyzerRoot.java @@ -8,6 +8,7 @@ import analyzer.exercises.leap.LeapAnalyzer; import analyzer.exercises.loglevels.LogLevelsAnalyzer; import analyzer.exercises.needforspeed.NeedForSpeedAnalyzer; +import analyzer.exercises.salarycalculator.SalaryCalculatorAnalyzer; import analyzer.exercises.secrets.SecretsAnalyzer; import analyzer.exercises.twofer.TwoferAnalyzer; import analyzer.exercises.wizardsandwarriors.WizardsAndWarriorsAnalyzer; @@ -56,6 +57,7 @@ private static List createAnalyzers(String slug) { case "leap" -> analyzers.add(new LeapAnalyzer()); case "log-levels" -> analyzers.add(new LogLevelsAnalyzer()); case "need-for-speed" -> analyzers.add(new NeedForSpeedAnalyzer()); + case "salary-calculator" -> analyzers.add(new SalaryCalculatorAnalyzer()); case "secrets" -> analyzers.add(new SecretsAnalyzer()); case "two-fer" -> analyzers.add(new TwoferAnalyzer()); case "wizards-and-warriors" -> analyzers.add(new WizardsAndWarriorsAnalyzer()); diff --git a/src/main/java/analyzer/exercises/salarycalculator/SalaryCalculatorAnalyzer.java b/src/main/java/analyzer/exercises/salarycalculator/SalaryCalculatorAnalyzer.java new file mode 100644 index 0000000..539adc3 --- /dev/null +++ b/src/main/java/analyzer/exercises/salarycalculator/SalaryCalculatorAnalyzer.java @@ -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 The salary-calculator exercise on the Java track + */ +public class SalaryCalculatorAnalyzer extends VoidVisitorAdapter 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(); + } +} diff --git a/src/main/java/analyzer/exercises/salarycalculator/UseTernaryOperators.java b/src/main/java/analyzer/exercises/salarycalculator/UseTernaryOperators.java new file mode 100644 index 0000000..56f012c --- /dev/null +++ b/src/main/java/analyzer/exercises/salarycalculator/UseTernaryOperators.java @@ -0,0 +1,32 @@ +package analyzer.exercises.salarycalculator; + +import java.util.Map; + +import analyzer.Comment; + +/** + * @see Markdown Template + */ +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 getParameters() { + return Map.of( + "inMethod", this.inMethod); + } + + @Override + public Type getType() { + return Type.ESSENTIAL; + } +} diff --git a/src/test/java/analyzer/AnalyzerIntegrationTest.java b/src/test/java/analyzer/AnalyzerIntegrationTest.java index bdb15d7..110d059 100644 --- a/src/test/java/analyzer/AnalyzerIntegrationTest.java +++ b/src/test/java/analyzer/AnalyzerIntegrationTest.java @@ -184,4 +184,25 @@ void wizardsandwarriors(String scenario) throws IOException { Approvals.verify(serialize(output.analysis()), Approvals.NAMES.withParameters(scenario)); } + + @ParameterizedTest + @ValueSource(strings = { + "ExemplarSolution", + "NoReuseBonusForProductsSold", + "NoReuseBonusMultiplier", + "NoReuseSalaryMultiplier", + "NotReusingMethodsAtAll", + "NotUsingTernaryOperatorsAndNotReusingMethods", + "NotUsingTernaryOperatorsAtAll", + "NotUsingTernaryOperatorsOnBonusMultiplier", + "NotUsingTernaryOperatorsOnFinalSalary", + "NotUsingTernaryOperatorsOnSalaryMultiplier" + }) + void salarycalculator(String scenario) throws IOException { + var path = Path.of("salary-calculator", scenario + ".java"); + var solution = new SolutionFromFiles("salary-calculator", 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.salarycalculator.ExemplarSolution.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.ExemplarSolution.approved.txt new file mode 100644 index 0000000..dba7ced --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.ExemplarSolution.approved.txt @@ -0,0 +1,11 @@ +{ + "comments": [ + { + "comment": "java.general.exemplar", + "params": { + "exerciseName": "Salary Calculator" + }, + "type": "celebratory" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseBonusForProductsSold.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseBonusForProductsSold.approved.txt new file mode 100644 index 0000000..20831be --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseBonusForProductsSold.approved.txt @@ -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" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseBonusMultiplier.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseBonusMultiplier.approved.txt new file mode 100644 index 0000000..5f29948 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseBonusMultiplier.approved.txt @@ -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" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseSalaryMultiplier.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseSalaryMultiplier.approved.txt new file mode 100644 index 0000000..195e970 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NoReuseSalaryMultiplier.approved.txt @@ -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" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotReusingMethodsAtAll.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotReusingMethodsAtAll.approved.txt new file mode 100644 index 0000000..cdb0ad6 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotReusingMethodsAtAll.approved.txt @@ -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" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsAndNotReusingMethods.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsAndNotReusingMethods.approved.txt new file mode 100644 index 0000000..d4d3110 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsAndNotReusingMethods.approved.txt @@ -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" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsAtAll.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsAtAll.approved.txt new file mode 100644 index 0000000..d4d3110 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsAtAll.approved.txt @@ -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" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnBonusMultiplier.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnBonusMultiplier.approved.txt new file mode 100644 index 0000000..7d2fd3a --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnBonusMultiplier.approved.txt @@ -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" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnFinalSalary.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnFinalSalary.approved.txt new file mode 100644 index 0000000..9f626e3 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnFinalSalary.approved.txt @@ -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" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnSalaryMultiplier.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnSalaryMultiplier.approved.txt new file mode 100644 index 0000000..d4d3110 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.salarycalculator.NotUsingTernaryOperatorsOnSalaryMultiplier.approved.txt @@ -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" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/scenarios/salary-calculator/ExemplarSolution.java b/src/test/resources/scenarios/salary-calculator/ExemplarSolution.java new file mode 100644 index 0000000..92ffc94 --- /dev/null +++ b/src/test/resources/scenarios/salary-calculator/ExemplarSolution.java @@ -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; + } +} diff --git a/src/test/resources/scenarios/salary-calculator/NoReuseBonusForProductsSold.java b/src/test/resources/scenarios/salary-calculator/NoReuseBonusForProductsSold.java new file mode 100644 index 0000000..f3bca2b --- /dev/null +++ b/src/test/resources/scenarios/salary-calculator/NoReuseBonusForProductsSold.java @@ -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; + } +} diff --git a/src/test/resources/scenarios/salary-calculator/NoReuseBonusMultiplier.java b/src/test/resources/scenarios/salary-calculator/NoReuseBonusMultiplier.java new file mode 100644 index 0000000..ac8a0cc --- /dev/null +++ b/src/test/resources/scenarios/salary-calculator/NoReuseBonusMultiplier.java @@ -0,0 +1,22 @@ +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) { + int multiplier = productsSold < 20 ? 10 : 13; + return productsSold * multiplier; + } + + public double finalSalary (int daysSkipped, int productsSold) { + double finalSalary = 1000.0 * salaryMultiplier(daysSkipped) + bonusForProductsSold(productsSold); + return finalSalary > 2000.0 ? 2000.0 : finalSalary; + } +} diff --git a/src/test/resources/scenarios/salary-calculator/NoReuseSalaryMultiplier.java b/src/test/resources/scenarios/salary-calculator/NoReuseSalaryMultiplier.java new file mode 100644 index 0000000..8f8d8cb --- /dev/null +++ b/src/test/resources/scenarios/salary-calculator/NoReuseSalaryMultiplier.java @@ -0,0 +1,23 @@ +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 skipped = daysSkipped < 5 ? 1 : 0.85; + + double finalSalary = 1000.0 * skipped + bonusForProductsSold(productsSold); + return finalSalary > 2000.0 ? 2000.0 : finalSalary; + } +} diff --git a/src/test/resources/scenarios/salary-calculator/NotReusingMethodsAtAll.java b/src/test/resources/scenarios/salary-calculator/NotReusingMethodsAtAll.java new file mode 100644 index 0000000..ce16032 --- /dev/null +++ b/src/test/resources/scenarios/salary-calculator/NotReusingMethodsAtAll.java @@ -0,0 +1,26 @@ +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) { + int multiplier = productsSold < 20 ? 10 : 13; + + return productsSold * multiplier; + } + + public double finalSalary (int daysSkipped, int productsSold) { + int multiplier = productsSold < 20 ? 10 : 13; + double skipped = daysSkipped < 5 ? 1 : 0.85; + double finalSalary = 1000.0 * skipped + productsSold * multiplier; + + return finalSalary > 2000.0 ? 2000.0 : finalSalary; + } +} diff --git a/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsAndNotReusingMethods.java b/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsAndNotReusingMethods.java new file mode 100644 index 0000000..9637195 --- /dev/null +++ b/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsAndNotReusingMethods.java @@ -0,0 +1,27 @@ +package scenarios.salarycalculator; + +public class SalaryCalculator { + + public double salaryMultiplier(int daysSkipped) { + if (daysSkipped < 5) { + return 1; + } + + return 0.85; + } + + public int bonusMultiplier(int productsSold) { + return productsSold < 20 ? 10 : 13; + } + + public double bonusForProductsSold (int productsSold) { + int multiplier = productsSold < 20 ? 10 : 13; + + return productsSold * multiplier; + } + + public double finalSalary (int daysSkipped, int productsSold) { + double finalSalary = 1000.0 * salaryMultiplier(daysSkipped) + bonusForProductsSold(productsSold); + return finalSalary > 2000.0 ? 2000.0 : finalSalary; + } +} diff --git a/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsAtAll.java b/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsAtAll.java new file mode 100644 index 0000000..59a46b1 --- /dev/null +++ b/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsAtAll.java @@ -0,0 +1,34 @@ +package scenarios.salarycalculator; + +public class SalaryCalculator { + + public double salaryMultiplier(int daysSkipped) { + if (daysSkipped < 5) { + return 1; + } + + return 0.85; + } + + public int bonusMultiplier(int productsSold) { + if (productsSold < 20) { + return 10; + } + + return 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); + + if (finalSalary > 2000.0) { + return 2000.0; + } + + return finalSalary; + } +} diff --git a/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsOnBonusMultiplier.java b/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsOnBonusMultiplier.java new file mode 100644 index 0000000..6c2970a --- /dev/null +++ b/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsOnBonusMultiplier.java @@ -0,0 +1,25 @@ +package scenarios.salarycalculator; + +public class SalaryCalculator { + + public double salaryMultiplier(int daysSkipped) { + return daysSkipped < 5 ? 1 : 0.85; + } + + public int bonusMultiplier(int productsSold) { + if (productsSold < 20) { + return 10; + } + + return 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; + } +} diff --git a/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsOnFinalSalary.java b/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsOnFinalSalary.java new file mode 100644 index 0000000..3dfc9cb --- /dev/null +++ b/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsOnFinalSalary.java @@ -0,0 +1,26 @@ +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); + + if (finalSalary > 2000.0) { + return 2000.0; + } + + return finalSalary; + } +} diff --git a/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsOnSalaryMultiplier.java b/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsOnSalaryMultiplier.java new file mode 100644 index 0000000..dbeae62 --- /dev/null +++ b/src/test/resources/scenarios/salary-calculator/NotUsingTernaryOperatorsOnSalaryMultiplier.java @@ -0,0 +1,25 @@ +package scenarios.salarycalculator; + +public class SalaryCalculator { + + public double salaryMultiplier(int daysSkipped) { + if (daysSkipped < 5) { + return 1; + } + + return 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; + } +} diff --git a/tests/salary-calculator/exemplar-solution/.meta/config.json b/tests/salary-calculator/exemplar-solution/.meta/config.json new file mode 100644 index 0000000..eef2753 --- /dev/null +++ b/tests/salary-calculator/exemplar-solution/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "TalesDias" + ], + "contributors": [ + "smicaliz" + ], + "files": { + "solution": [ + "src/main/java/SalaryCalculator.java" + ], + "test": [ + "src/test/java/SalaryCalculatorTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/SalaryCalculator.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "blurb": "Learn about ternary operators by calculating salaries." +} \ No newline at end of file diff --git a/tests/salary-calculator/exemplar-solution/expected_analysis.json b/tests/salary-calculator/exemplar-solution/expected_analysis.json new file mode 100644 index 0000000..f98bd2a --- /dev/null +++ b/tests/salary-calculator/exemplar-solution/expected_analysis.json @@ -0,0 +1,11 @@ +{ + "comments": [ + { + "comment": "java.general.exemplar", + "params": { + "exerciseName": "Salary Calculator" + }, + "type": "celebratory" + } + ] +} \ No newline at end of file diff --git a/tests/salary-calculator/exemplar-solution/expected_tags.json b/tests/salary-calculator/exemplar-solution/expected_tags.json new file mode 100644 index 0000000..eb25b19 --- /dev/null +++ b/tests/salary-calculator/exemplar-solution/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/salary-calculator/exemplar-solution/src/main/java/SalaryCalculator.java b/tests/salary-calculator/exemplar-solution/src/main/java/SalaryCalculator.java new file mode 100644 index 0000000..f72ed84 --- /dev/null +++ b/tests/salary-calculator/exemplar-solution/src/main/java/SalaryCalculator.java @@ -0,0 +1,19 @@ +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; + } +} diff --git a/tests/salary-calculator/no-reuse-code/.meta/config.json b/tests/salary-calculator/no-reuse-code/.meta/config.json new file mode 100644 index 0000000..eef2753 --- /dev/null +++ b/tests/salary-calculator/no-reuse-code/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "TalesDias" + ], + "contributors": [ + "smicaliz" + ], + "files": { + "solution": [ + "src/main/java/SalaryCalculator.java" + ], + "test": [ + "src/test/java/SalaryCalculatorTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/SalaryCalculator.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "blurb": "Learn about ternary operators by calculating salaries." +} \ No newline at end of file diff --git a/tests/salary-calculator/no-reuse-code/expected_analysis.json b/tests/salary-calculator/no-reuse-code/expected_analysis.json new file mode 100644 index 0000000..62fbcc7 --- /dev/null +++ b/tests/salary-calculator/no-reuse-code/expected_analysis.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/tests/salary-calculator/no-reuse-code/expected_tags.json b/tests/salary-calculator/no-reuse-code/expected_tags.json new file mode 100644 index 0000000..eb25b19 --- /dev/null +++ b/tests/salary-calculator/no-reuse-code/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/salary-calculator/no-reuse-code/src/main/java/SalaryCalculator.java b/tests/salary-calculator/no-reuse-code/src/main/java/SalaryCalculator.java new file mode 100644 index 0000000..714c3f7 --- /dev/null +++ b/tests/salary-calculator/no-reuse-code/src/main/java/SalaryCalculator.java @@ -0,0 +1,21 @@ +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) { + int multiplier = productsSold < 20 ? 10 : 13; + + return productsSold * multiplier; + } + + public double finalSalary (int daysSkipped, int productsSold) { + double finalSalary = 1000.0 * salaryMultiplier(daysSkipped) + bonusForProductsSold(productsSold); + return finalSalary > 2000.0 ? 2000.0 : finalSalary; + } +} diff --git a/tests/salary-calculator/no-ternary-operators-used/.meta/config.json b/tests/salary-calculator/no-ternary-operators-used/.meta/config.json new file mode 100644 index 0000000..eef2753 --- /dev/null +++ b/tests/salary-calculator/no-ternary-operators-used/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "TalesDias" + ], + "contributors": [ + "smicaliz" + ], + "files": { + "solution": [ + "src/main/java/SalaryCalculator.java" + ], + "test": [ + "src/test/java/SalaryCalculatorTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/SalaryCalculator.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "blurb": "Learn about ternary operators by calculating salaries." +} \ No newline at end of file diff --git a/tests/salary-calculator/no-ternary-operators-used/expected_analysis.json b/tests/salary-calculator/no-ternary-operators-used/expected_analysis.json new file mode 100644 index 0000000..20e3da3 --- /dev/null +++ b/tests/salary-calculator/no-ternary-operators-used/expected_analysis.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/tests/salary-calculator/no-ternary-operators-used/expected_tags.json b/tests/salary-calculator/no-ternary-operators-used/expected_tags.json new file mode 100644 index 0000000..eb25b19 --- /dev/null +++ b/tests/salary-calculator/no-ternary-operators-used/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/salary-calculator/no-ternary-operators-used/src/main/java/SalaryCalculator.java b/tests/salary-calculator/no-ternary-operators-used/src/main/java/SalaryCalculator.java new file mode 100644 index 0000000..8014440 --- /dev/null +++ b/tests/salary-calculator/no-ternary-operators-used/src/main/java/SalaryCalculator.java @@ -0,0 +1,23 @@ +class SalaryCalculator { + + public double salaryMultiplier(int daysSkipped) { + if (daysSkipped < 5) { + return 1; + } + + return 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; + } +}