forked from dangeabunea/pluralsight-refactoring-solid-java17
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from marcellodesales/feature/single-responsibil…
…ity-principle-solve-exercise-srp Feature: single responsibility principle solve exercise srp
- Loading branch information
Showing
6 changed files
with
104 additions
and
49 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
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
31 changes: 8 additions & 23 deletions
31
2-ocp/exercises/exercise1/before/src/main/java/PointInsideAreasCalculator.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 |
---|---|---|
@@ -1,30 +1,15 @@ | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class PointInsideAreasCalculator { | ||
public boolean isPointInsideAreas(int x, int y, List<Shape> shapes) { | ||
for (var s : shapes) { | ||
// For each new shape type we need to modify | ||
// this calculator | ||
|
||
if (s instanceof Rectangle r) { | ||
if (x <= r.getMaxX() && | ||
x >= r.getMinX() && | ||
y >= r.getMinY() && | ||
y <= r.getMaxY()) { | ||
return true; | ||
} | ||
} | ||
|
||
if (s instanceof Circle c) { | ||
var distToOrigin = Math.sqrt( | ||
(c.getyOrigin() - y) * (c.getyOrigin() - y) + | ||
(c.getxOrigin() - x) * (c.getxOrigin() - x) | ||
); | ||
if (distToOrigin <= c.getRadius()) { | ||
return true; | ||
} | ||
} | ||
public Map<Shape, Boolean> isPointInsideAreas(int x, int y, Set<Shape> shapes) { | ||
Map<Shape, Boolean> shapeAreas = new HashMap<>(shapes.size()); | ||
for (var s : shapes) { | ||
shapeAreas.put(s, s.isPointInsideArea(x, y)); | ||
} | ||
return false; | ||
return shapeAreas; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
public abstract class Shape { | ||
|
||
public abstract boolean isPointInsideArea(int x, int y); | ||
} |
56 changes: 30 additions & 26 deletions
56
2-ocp/exercises/exercise1/before/src/test/java/PointInsideAreasCalculatorTests.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 |
---|---|---|
@@ -1,64 +1,68 @@ | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import java.util.Map; | ||
|
||
public class PointInsideAreasCalculatorTests { | ||
|
||
public static void assertResults(Map<Shape, Boolean> shapesExpectedResults, Map<Shape, Boolean> shapesInput) { | ||
for (Map.Entry<Shape,Boolean> entry : shapesInput.entrySet()) { | ||
assert shapesExpectedResults.get(entry.getKey()).equals(entry.getValue()); | ||
} | ||
} | ||
|
||
@Test | ||
public void calculateMethodShouldReturnTrueIfPointInCircle() { | ||
List<Shape> shapes = List.of( | ||
new Circle(0,0,10) | ||
Map<Shape, Boolean> shapesExpectedResults = Map.of( | ||
new Circle(0,0,10), true | ||
); | ||
var calc = new PointInsideAreasCalculator(); | ||
|
||
var result = calc.isPointInsideAreas(2,2,shapes); | ||
assertTrue(result); | ||
var results = calc.isPointInsideAreas(2,2, shapesExpectedResults.keySet()); | ||
assertResults(shapesExpectedResults, results); | ||
} | ||
|
||
@Test | ||
public void calculateMethodShouldReturnFalseIfPointOutsideCircle() { | ||
List<Shape> shapes = List.of( | ||
new Circle(0,0,10) | ||
Map<Shape, Boolean> shapesExpectedResults = Map.of( | ||
new Circle(0,0,10), false | ||
); | ||
var calc = new PointInsideAreasCalculator(); | ||
|
||
var result = calc.isPointInsideAreas(11,11,shapes); | ||
assertFalse(result); | ||
var results = calc.isPointInsideAreas(11,11, shapesExpectedResults.keySet()); | ||
assertResults(shapesExpectedResults, results); | ||
} | ||
|
||
@Test | ||
public void calculateMethodShouldReturnTrueIfPointInRectangle() { | ||
List<Shape> shapes = List.of( | ||
new Rectangle(0,0,4,4) | ||
Map<Shape, Boolean> shapesExpectedResults = Map.of( | ||
new Rectangle(0,0,4,4), true | ||
); | ||
var calc = new PointInsideAreasCalculator(); | ||
|
||
var result = calc.isPointInsideAreas(3,3,shapes); | ||
assertTrue(result); | ||
var results = calc.isPointInsideAreas(3,3, shapesExpectedResults.keySet()); | ||
assertResults(shapesExpectedResults, results); | ||
} | ||
|
||
@Test | ||
public void calculateMethodShouldReturnFalseIfPointOutsideRectangle() { | ||
List<Shape> shapes = List.of( | ||
new Rectangle(0,0,4,4) | ||
Map<Shape, Boolean> shapesExpectedResults = Map.of( | ||
new Rectangle(0,0,4,4), false | ||
); | ||
var calc = new PointInsideAreasCalculator(); | ||
|
||
var result = calc.isPointInsideAreas(4,5,shapes); | ||
assertFalse(result); | ||
var results = calc.isPointInsideAreas(4,5, shapesExpectedResults.keySet()); | ||
assertResults(shapesExpectedResults, results); | ||
} | ||
|
||
@Test | ||
public void calculateMethodShouldReturnTrueIfPointInShapes() { | ||
List<Shape> shapes = List.of( | ||
new Rectangle(0,0,4,4), | ||
new Circle(10,10, 7) | ||
Map<Shape, Boolean> shapesExpectedResults = Map.of( | ||
new Rectangle(0,0,4,4), false, | ||
new Circle(10,10, 7), true | ||
); | ||
var calc = new PointInsideAreasCalculator(); | ||
|
||
var result = calc.isPointInsideAreas(6,6,shapes); | ||
assertTrue(result); | ||
var calc = new PointInsideAreasCalculator(); | ||
var results = calc.isPointInsideAreas(6, 6, shapesExpectedResults.keySet()); | ||
assertResults(shapesExpectedResults, results); | ||
} | ||
} |