diff --git a/src/main/java/analyzer/AnalyzerRoot.java b/src/main/java/analyzer/AnalyzerRoot.java index 6e249ec3..c7bf9c8d 100644 --- a/src/main/java/analyzer/AnalyzerRoot.java +++ b/src/main/java/analyzer/AnalyzerRoot.java @@ -3,6 +3,7 @@ import analyzer.comments.FeedbackRequest; import analyzer.exercises.GlobalAnalyzer; import analyzer.exercises.annalynsinfiltration.AnnalynsInfiltrationAnalyzer; +import analyzer.exercises.carsassemble.CarsAssembleAnalyzer; import analyzer.exercises.hamming.HammingAnalyzer; import analyzer.exercises.lasagna.LasagnaAnalyzer; import analyzer.exercises.leap.LeapAnalyzer; @@ -52,6 +53,7 @@ private static List createAnalyzers(String slug) { switch (slug) { case "annalyns-infiltration" -> analyzers.add(new AnnalynsInfiltrationAnalyzer()); + case "cars-assemble" -> analyzers.add(new CarsAssembleAnalyzer()); case "hamming" -> analyzers.add(new HammingAnalyzer()); case "lasagna" -> analyzers.add(new LasagnaAnalyzer()); case "leap" -> analyzers.add(new LeapAnalyzer()); diff --git a/src/main/java/analyzer/exercises/carsassemble/AvoidUsingReturnInElseStatement.java b/src/main/java/analyzer/exercises/carsassemble/AvoidUsingReturnInElseStatement.java new file mode 100644 index 00000000..d327c62e --- /dev/null +++ b/src/main/java/analyzer/exercises/carsassemble/AvoidUsingReturnInElseStatement.java @@ -0,0 +1,18 @@ +package analyzer.exercises.carsassemble; + +import analyzer.Comment; + +/** + * @see Markdown Template + */ +public class AvoidUsingReturnInElseStatement extends Comment { + @Override + public String getKey() { + return "java.cars-assemble.avoid_using_return_in_else_statement"; + } + + @Override + public Type getType() { + return Type.INFORMATIVE; + } +} diff --git a/src/main/java/analyzer/exercises/carsassemble/CarsAssembleAnalyzer.java b/src/main/java/analyzer/exercises/carsassemble/CarsAssembleAnalyzer.java new file mode 100644 index 00000000..a7d3fcfc --- /dev/null +++ b/src/main/java/analyzer/exercises/carsassemble/CarsAssembleAnalyzer.java @@ -0,0 +1,101 @@ +package analyzer.exercises.carsassemble; + +import analyzer.Analyzer; +import analyzer.OutputCollector; +import analyzer.Solution; +import analyzer.comments.ExemplarSolution; +import analyzer.comments.MethodTooLong; +import analyzer.comments.ReuseCode; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.expr.MethodCallExpr; +import com.github.javaparser.ast.stmt.BlockStmt; +import com.github.javaparser.ast.stmt.IfStmt; +import com.github.javaparser.ast.stmt.Statement; +import com.github.javaparser.ast.visitor.VoidVisitorAdapter; + +import java.util.List; + +/** + * The {@link CarsAssembleAnalyzer} is the analyzer implementation for the {@code cars-assemble} concept exercise. + * + * @see The cars-assemble exercise on the Java track + */ +public class CarsAssembleAnalyzer extends VoidVisitorAdapter implements Analyzer { + private static final String EXERCISE_NAME = "CarsAssemble"; + private static final String MAGIC_NUMBER = "221"; + private static final String PRODUCTION_RATE_PER_HOUR_METHOD = "productionRatePerHour"; + private static final String WORKING_ITEMS_PER_MINUTE_METHOD = "workingItemsPerMinute"; + private static final String RETURN = "return"; + + @Override + public void analyze(Solution solution, OutputCollector output) { + + for (var compilationUnit : solution.getCompilationUnits()) { + compilationUnit.getClassByName(EXERCISE_NAME).ifPresent(c -> c.accept(this, output)); + } + + if (output.getComments().isEmpty()) { + output.addComment(new ExemplarSolution(EXERCISE_NAME)); + } + } + + @Override + public void visit(MethodDeclaration n, OutputCollector output) { + + if(n.getNameAsString().equals(PRODUCTION_RATE_PER_HOUR_METHOD) && !hasHelperMethod(n)){ + output.addComment(new MethodTooLong(PRODUCTION_RATE_PER_HOUR_METHOD)); + } + + if(n.getNameAsString().equals(WORKING_ITEMS_PER_MINUTE_METHOD) && !reuseMethod(n)){ + output.addComment(new ReuseCode(WORKING_ITEMS_PER_MINUTE_METHOD, PRODUCTION_RATE_PER_HOUR_METHOD)); + } + + if(useMagicNumber(n)){ + output.addComment(new PreferStoringConstantInField()); + } + + super.visit(n, output); + + } + + @Override + public void visit(IfStmt node, OutputCollector output){ + + if(node.getThenStmt().toString().contains(RETURN) && node.hasElseBlock()){ + output.addComment(new AvoidUsingReturnInElseStatement()); + } + + super.visit(node, output); + } + + private boolean hasHelperMethod(MethodDeclaration n){ + + if(n.getBody().isEmpty()){ + return true; + } + + BlockStmt stmt = n.getBody().get(); + List IfStmts = stmt.getStatements().stream().filter(Statement::isIfStmt).toList(); + for(Statement s : IfStmts){ + return ((IfStmt) s).hasElseBlock(); + } + return true; + } + + private boolean reuseMethod(MethodDeclaration n){ + return !n.findAll(MethodCallExpr.class).stream().filter(m -> m.getNameAsString().equals(PRODUCTION_RATE_PER_HOUR_METHOD)).toList().isEmpty(); + } + + private boolean useMagicNumber(MethodDeclaration n){ + + if(n.getBody().isEmpty()){ + return false; + } + + BlockStmt stmt = n.getBody().get(); + return stmt.toString().contains(MAGIC_NUMBER); + + } + +} + diff --git a/src/main/java/analyzer/exercises/carsassemble/PreferStoringConstantInField.java b/src/main/java/analyzer/exercises/carsassemble/PreferStoringConstantInField.java new file mode 100644 index 00000000..7a24dc9c --- /dev/null +++ b/src/main/java/analyzer/exercises/carsassemble/PreferStoringConstantInField.java @@ -0,0 +1,18 @@ +package analyzer.exercises.carsassemble; + +import analyzer.Comment; + +/** + * @see Markdown Template + */ +public class PreferStoringConstantInField extends Comment { + @Override + public String getKey() { + return "java.cars-assemble.prefer_storing_constant_in_field"; + } + + @Override + public Type getType() { + return Type.INFORMATIVE; + } +} diff --git a/src/test/java/analyzer/AnalyzerIntegrationTest.java b/src/test/java/analyzer/AnalyzerIntegrationTest.java index 4b8cccde..66d870c1 100644 --- a/src/test/java/analyzer/AnalyzerIntegrationTest.java +++ b/src/test/java/analyzer/AnalyzerIntegrationTest.java @@ -26,6 +26,22 @@ void global(String scenario) throws IOException { Approvals.verify(serialize(output.analysis()), Approvals.NAMES.withParameters(scenario)); } + @ParameterizedTest + @ValueSource(strings = { + "ExemplarSolution", + "NotReusingMethod", + "NotUsingHelperMethod", + "UsingMagicNumber", + "UsingReturnInElseStatement", + }) + void carsassemble(String scenario) throws IOException { + var path = Path.of("cars-assemble", scenario + ".java"); + var solution = new SolutionFromFiles("cars-assemble", SCENARIOS.resolve(path)); + var output = AnalyzerRoot.analyze(solution); + + Approvals.verify(serialize(output.analysis()), Approvals.NAMES.withParameters(scenario)); + } + @ParameterizedTest @ValueSource(strings = { "ConstructorTooLong", diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.ExemplarSolution.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.ExemplarSolution.approved.txt new file mode 100644 index 00000000..e206be46 --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.ExemplarSolution.approved.txt @@ -0,0 +1,11 @@ +{ + "comments": [ + { + "comment": "java.general.exemplar", + "params": { + "exerciseName": "CarsAssemble" + }, + "type": "celebratory" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.NotReusingMethod.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.NotReusingMethod.approved.txt new file mode 100644 index 00000000..6e5db4dc --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.NotReusingMethod.approved.txt @@ -0,0 +1,17 @@ +{ + "comments": [ + { + "comment": "java.general.reuse_code", + "params": { + "callingMethod": "workingItemsPerMinute", + "methodToCall": "productionRatePerHour" + }, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.NotUsingHelperMethod.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.NotUsingHelperMethod.approved.txt new file mode 100644 index 00000000..b39b410b --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.NotUsingHelperMethod.approved.txt @@ -0,0 +1,16 @@ +{ + "comments": [ + { + "comment": "java.general.method_too_long", + "params": { + "methodNames": "productionRatePerHour" + }, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.UsingMagicNumber.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.UsingMagicNumber.approved.txt new file mode 100644 index 00000000..2f1bde6a --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.UsingMagicNumber.approved.txt @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.cars-assemble.prefer_storing_constant_in_field", + "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.carsassemble.UsingReturnInElseStatement.approved.txt b/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.UsingReturnInElseStatement.approved.txt new file mode 100644 index 00000000..7ebb5eca --- /dev/null +++ b/src/test/resources/analyzer/AnalyzerIntegrationTest.carsassemble.UsingReturnInElseStatement.approved.txt @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.cars-assemble.avoid_using_return_in_else_statement", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/src/test/resources/scenarios/cars-assemble/ExemplarSolution.java b/src/test/resources/scenarios/cars-assemble/ExemplarSolution.java new file mode 100644 index 00000000..2650b9e4 --- /dev/null +++ b/src/test/resources/scenarios/cars-assemble/ExemplarSolution.java @@ -0,0 +1,29 @@ + +public class CarsAssemble { + + private final int defaultProductionRate = 221; + + public double productionRatePerHour(int speed) { + return defaultProductionRate * speed * successRate(speed); + } + + public int workingItemsPerMinute(int speed) { + return (int) (productionRatePerHour(speed) / 60); + } + + private double successRate(int speed) { + if (speed == 10) { + return 0.77; + } + + if (speed == 9) { + return 0.8; + } + + if (speed >= 5) { + return 0.9; + } + + return 1; + } +} diff --git a/src/test/resources/scenarios/cars-assemble/NotReusingMethod.java b/src/test/resources/scenarios/cars-assemble/NotReusingMethod.java new file mode 100644 index 00000000..87f1fd27 --- /dev/null +++ b/src/test/resources/scenarios/cars-assemble/NotReusingMethod.java @@ -0,0 +1,29 @@ + +public class CarsAssemble { + + private final int defaultProductionRate = 221; + + public double productionRatePerHour(int speed) { + return defaultProductionRate * speed * successRate(speed); + } + + public int workingItemsPerMinute(int speed) { + return (int) defaultProductionRate * speed * successRate(speed) / 60; + } + + private double successRate(int speed) { + if (speed == 10) { + return 0.77; + } + + if (speed == 9) { + return 0.8; + } + + if (speed >= 5) { + return 0.9; + } + + return 1; + } +} diff --git a/src/test/resources/scenarios/cars-assemble/NotUsingHelperMethod.java b/src/test/resources/scenarios/cars-assemble/NotUsingHelperMethod.java new file mode 100644 index 00000000..456c1742 --- /dev/null +++ b/src/test/resources/scenarios/cars-assemble/NotUsingHelperMethod.java @@ -0,0 +1,26 @@ + +public class CarsAssemble { + + private final int defaultProductionRate = 221; + + public double productionRatePerHour(int speed) { + double rate = null; + + if (speed == 10) { + rate = 0.77; + }else if (speed == 9) { + rate = 0.8; + } else if (speed >= 5) { + rate = 0.9; + } else { + rate = 1.0; + } + + return defaultProductionRate * speed * rate; + } + + public int workingItemsPerMinute(int speed) { + return (int) productionRatePerHour(speed) / 60; + } + +} diff --git a/src/test/resources/scenarios/cars-assemble/UsingMagicNumber.java b/src/test/resources/scenarios/cars-assemble/UsingMagicNumber.java new file mode 100644 index 00000000..2f23e126 --- /dev/null +++ b/src/test/resources/scenarios/cars-assemble/UsingMagicNumber.java @@ -0,0 +1,27 @@ + +public class CarsAssemble { + + public double productionRatePerHour(int speed) { + return 221 * speed * successRate(speed); + } + + public int workingItemsPerMinute(int speed) { + return (int) productionRatePerHour(speed) / 60; + } + + private double successRate(int speed) { + if (speed == 10) { + return 0.77; + } + + if (speed == 9) { + return 0.8; + } + + if (speed >= 5) { + return 0.9; + } + + return 1; + } +} diff --git a/src/test/resources/scenarios/cars-assemble/UsingReturnInElseStatement.java b/src/test/resources/scenarios/cars-assemble/UsingReturnInElseStatement.java new file mode 100644 index 00000000..525d1609 --- /dev/null +++ b/src/test/resources/scenarios/cars-assemble/UsingReturnInElseStatement.java @@ -0,0 +1,27 @@ + +public class CarsAssemble { + + private final int defaultProductionRate = 221; + + public double productionRatePerHour(int speed) { + return defaultProductionRate * speed * successRate(speed); + } + + public int workingItemsPerMinute(int speed) { + return (int) (productionRatePerHour(speed) / 60); + } + + private double successRate(int speed) { + + if (speed == 10) { + return 0.77; + } else if (speed == 9) { + return 0.8; + } else if (speed >= 5) { + return 0.9; + } else { + return 1.0; + } + + } +} diff --git a/tests/cars-assemble/exemplar-solution/.meta/config.json b/tests/cars-assemble/exemplar-solution/.meta/config.json new file mode 100644 index 00000000..5a323d49 --- /dev/null +++ b/tests/cars-assemble/exemplar-solution/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "TalesDias" + ], + "files": { + "solution": [ + "src/main/java/CarsAssemble.java" + ], + "test": [ + "src/test/java/CarsAssembleTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/CarsAssemble.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/cars-assemble" + ], + "blurb": "Learn about numbers by analyzing the production of an assembly line." +} diff --git a/tests/cars-assemble/exemplar-solution/expected_analysis.json b/tests/cars-assemble/exemplar-solution/expected_analysis.json new file mode 100644 index 00000000..1eac5243 --- /dev/null +++ b/tests/cars-assemble/exemplar-solution/expected_analysis.json @@ -0,0 +1,11 @@ +{ + "comments": [ + { + "comment": "java.general.exemplar", + "params": { + "exerciseName": "CarsAssemble" + }, + "type": "celebratory" + } + ] +} \ No newline at end of file diff --git a/tests/cars-assemble/exemplar-solution/expected_tags.json b/tests/cars-assemble/exemplar-solution/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/cars-assemble/exemplar-solution/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/cars-assemble/exemplar-solution/src/main/java/CarsAssemble.java b/tests/cars-assemble/exemplar-solution/src/main/java/CarsAssemble.java new file mode 100644 index 00000000..69d6ecb3 --- /dev/null +++ b/tests/cars-assemble/exemplar-solution/src/main/java/CarsAssemble.java @@ -0,0 +1,28 @@ +public class CarsAssemble { + + private final int defaultProductionRate = 221; + + public double productionRatePerHour(int speed) { + return defaultProductionRate * speed * successRate(speed); + } + + public int workingItemsPerMinute(int speed) { + return (int) (productionRatePerHour(speed) / 60); + } + + private double successRate(int speed) { + if (speed == 10) { + return 0.77; + } + + if (speed == 9) { + return 0.8; + } + + if (speed >= 5) { + return 0.9; + } + + return 1; + } +} diff --git a/tests/cars-assemble/not-reusing-method/.meta/config.json b/tests/cars-assemble/not-reusing-method/.meta/config.json new file mode 100644 index 00000000..5a323d49 --- /dev/null +++ b/tests/cars-assemble/not-reusing-method/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "TalesDias" + ], + "files": { + "solution": [ + "src/main/java/CarsAssemble.java" + ], + "test": [ + "src/test/java/CarsAssembleTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/CarsAssemble.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/cars-assemble" + ], + "blurb": "Learn about numbers by analyzing the production of an assembly line." +} diff --git a/tests/cars-assemble/not-reusing-method/expected_analysis.json b/tests/cars-assemble/not-reusing-method/expected_analysis.json new file mode 100644 index 00000000..34c82b47 --- /dev/null +++ b/tests/cars-assemble/not-reusing-method/expected_analysis.json @@ -0,0 +1,17 @@ +{ + "comments": [ + { + "comment": "java.general.reuse_code", + "params": { + "callingMethod": "workingItemsPerMinute", + "methodToCall": "productionRatePerHour" + }, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/tests/cars-assemble/not-reusing-method/expected_tags.json b/tests/cars-assemble/not-reusing-method/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/cars-assemble/not-reusing-method/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/cars-assemble/not-reusing-method/src/main/java/CarsAssemble.java b/tests/cars-assemble/not-reusing-method/src/main/java/CarsAssemble.java new file mode 100644 index 00000000..bb787ed5 --- /dev/null +++ b/tests/cars-assemble/not-reusing-method/src/main/java/CarsAssemble.java @@ -0,0 +1,28 @@ +public class CarsAssemble { + + private final int defaultProductionRate = 221; + + public double productionRatePerHour(int speed) { + return defaultProductionRate * speed * successRate(speed); + } + + public int workingItemsPerMinute(int speed) { + return (int) defaultProductionRate * speed * successRate(speed) / 60; + } + + private double successRate(int speed) { + if (speed == 10) { + return 0.77; + } + + if (speed == 9) { + return 0.8; + } + + if (speed >= 5) { + return 0.9; + } + + return 1; + } +} diff --git a/tests/cars-assemble/not-using-helper-method/.meta/config.json b/tests/cars-assemble/not-using-helper-method/.meta/config.json new file mode 100644 index 00000000..5a323d49 --- /dev/null +++ b/tests/cars-assemble/not-using-helper-method/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "TalesDias" + ], + "files": { + "solution": [ + "src/main/java/CarsAssemble.java" + ], + "test": [ + "src/test/java/CarsAssembleTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/CarsAssemble.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/cars-assemble" + ], + "blurb": "Learn about numbers by analyzing the production of an assembly line." +} diff --git a/tests/cars-assemble/not-using-helper-method/expected_analysis.json b/tests/cars-assemble/not-using-helper-method/expected_analysis.json new file mode 100644 index 00000000..97992ad8 --- /dev/null +++ b/tests/cars-assemble/not-using-helper-method/expected_analysis.json @@ -0,0 +1,16 @@ +{ + "comments": [ + { + "comment": "java.general.method_too_long", + "params": { + "methodNames": "productionRatePerHour" + }, + "type": "actionable" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/tests/cars-assemble/not-using-helper-method/expected_tags.json b/tests/cars-assemble/not-using-helper-method/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/cars-assemble/not-using-helper-method/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/cars-assemble/not-using-helper-method/src/main/java/CarsAssemble.java b/tests/cars-assemble/not-using-helper-method/src/main/java/CarsAssemble.java new file mode 100644 index 00000000..7bd708fa --- /dev/null +++ b/tests/cars-assemble/not-using-helper-method/src/main/java/CarsAssemble.java @@ -0,0 +1,25 @@ +public class CarsAssemble { + + private final int defaultProductionRate = 221; + + public double productionRatePerHour(int speed) { + double rate = null; + + if (speed == 10) { + rate = 0.77; + }else if (speed == 9) { + rate = 0.8; + } else if (speed >= 5) { + rate = 0.9; + } else { + rate = 1.0; + } + + return defaultProductionRate * speed * rate; + } + + public int workingItemsPerMinute(int speed) { + return (int) productionRatePerHour(speed) / 60; + } + +} diff --git a/tests/cars-assemble/using-magic-number/.meta/config.json b/tests/cars-assemble/using-magic-number/.meta/config.json new file mode 100644 index 00000000..5a323d49 --- /dev/null +++ b/tests/cars-assemble/using-magic-number/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "TalesDias" + ], + "files": { + "solution": [ + "src/main/java/CarsAssemble.java" + ], + "test": [ + "src/test/java/CarsAssembleTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/CarsAssemble.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/cars-assemble" + ], + "blurb": "Learn about numbers by analyzing the production of an assembly line." +} diff --git a/tests/cars-assemble/using-magic-number/expected_analysis.json b/tests/cars-assemble/using-magic-number/expected_analysis.json new file mode 100644 index 00000000..b1450604 --- /dev/null +++ b/tests/cars-assemble/using-magic-number/expected_analysis.json @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.cars-assemble.prefer_storing_constant_in_field", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/tests/cars-assemble/using-magic-number/expected_tags.json b/tests/cars-assemble/using-magic-number/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/cars-assemble/using-magic-number/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/cars-assemble/using-magic-number/src/main/java/CarsAssemble.java b/tests/cars-assemble/using-magic-number/src/main/java/CarsAssemble.java new file mode 100644 index 00000000..0b5672b3 --- /dev/null +++ b/tests/cars-assemble/using-magic-number/src/main/java/CarsAssemble.java @@ -0,0 +1,26 @@ +public class CarsAssemble { + + public double productionRatePerHour(int speed) { + return 221 * speed * successRate(speed); + } + + public int workingItemsPerMinute(int speed) { + return (int) productionRatePerHour(speed) / 60; + } + + private double successRate(int speed) { + if (speed == 10) { + return 0.77; + } + + if (speed == 9) { + return 0.8; + } + + if (speed >= 5) { + return 0.9; + } + + return 1; + } +} diff --git a/tests/cars-assemble/using-return-in-else-statement/.meta/config.json b/tests/cars-assemble/using-return-in-else-statement/.meta/config.json new file mode 100644 index 00000000..5a323d49 --- /dev/null +++ b/tests/cars-assemble/using-return-in-else-statement/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "TalesDias" + ], + "files": { + "solution": [ + "src/main/java/CarsAssemble.java" + ], + "test": [ + "src/test/java/CarsAssembleTest.java" + ], + "exemplar": [ + ".meta/src/reference/java/CarsAssemble.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "forked_from": [ + "csharp/cars-assemble" + ], + "blurb": "Learn about numbers by analyzing the production of an assembly line." +} diff --git a/tests/cars-assemble/using-return-in-else-statement/expected_analysis.json b/tests/cars-assemble/using-return-in-else-statement/expected_analysis.json new file mode 100644 index 00000000..8a1490e3 --- /dev/null +++ b/tests/cars-assemble/using-return-in-else-statement/expected_analysis.json @@ -0,0 +1,14 @@ +{ + "comments": [ + { + "comment": "java.cars-assemble.avoid_using_return_in_else_statement", + "params": {}, + "type": "informative" + }, + { + "comment": "java.general.feedback_request", + "params": {}, + "type": "informative" + } + ] +} \ No newline at end of file diff --git a/tests/cars-assemble/using-return-in-else-statement/expected_tags.json b/tests/cars-assemble/using-return-in-else-statement/expected_tags.json new file mode 100644 index 00000000..eb25b190 --- /dev/null +++ b/tests/cars-assemble/using-return-in-else-statement/expected_tags.json @@ -0,0 +1,3 @@ +{ + "tags": [] +} \ No newline at end of file diff --git a/tests/cars-assemble/using-return-in-else-statement/src/main/java/CarsAssemble.java b/tests/cars-assemble/using-return-in-else-statement/src/main/java/CarsAssemble.java new file mode 100644 index 00000000..82ae8ff3 --- /dev/null +++ b/tests/cars-assemble/using-return-in-else-statement/src/main/java/CarsAssemble.java @@ -0,0 +1,26 @@ +public class CarsAssemble { + + private final int defaultProductionRate = 221; + + public double productionRatePerHour(int speed) { + return defaultProductionRate * speed * successRate(speed); + } + + public int workingItemsPerMinute(int speed) { + return (int) (productionRatePerHour(speed) / 60); + } + + private double successRate(int speed) { + + if (speed == 10) { + return 0.77; + } else if (speed == 9) { + return 0.8; + } else if (speed >= 5) { + return 0.9; + } else { + return 1.0; + } + + } +}