Skip to content

Commit

Permalink
Merge pull request #3 from marcellodesales/feature/single-responsibil…
Browse files Browse the repository at this point in the history
…ity-principle-solve-exercise-srp

Feature: single responsibility principle solve exercise srp
  • Loading branch information
marcellodesales committed Feb 27, 2023
2 parents cdb3bd3 + 6c093ca commit bb48f56
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import java.util.List;

public class TaxCalculator {

public double calculateTotalTaxes(List<Employee> employees) {
var taxes = 0.0;

Expand All @@ -27,4 +28,21 @@ private double calculateTaxForEmployee(Employee e) {

return employeeTaxes;
}

public double calculateTotalTaxesWithNationalityDetails(List<Employee> employees){
var taxes = 0.0;

for (var e : employees) {
var tax = calculateTaxForEmployee(e);

// take nationality into consideration
if (!e.getNationality().equals("USA")){
tax += e.getIncome() * 0.1;
}

taxes += tax;
}

return taxes;
}
}
25 changes: 25 additions & 0 deletions 2-ocp/exercises/exercise1/before/src/main/java/Circle.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import java.util.Objects;

public class Circle extends Shape {

private int xOrigin;
private int yOrigin;
private int radius;
Expand All @@ -20,4 +23,26 @@ public int getyOrigin() {
public int getRadius() {
return radius;
}

@Override
public boolean isPointInsideArea(int x, int y) {
var distToOrigin = Math.sqrt(
(this.getyOrigin() - y) * (this.getyOrigin() - y) +
(this.getxOrigin() - x) * (this.getxOrigin() - x)
);
return distToOrigin <= this.getRadius();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Circle circle = (Circle) o;
return xOrigin == circle.xOrigin && yOrigin == circle.yOrigin && radius == circle.radius;
}

@Override
public int hashCode() {
return Objects.hash(xOrigin, yOrigin, radius);
}
}
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;
}
}
21 changes: 21 additions & 0 deletions 2-ocp/exercises/exercise1/before/src/main/java/Rectangle.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.Objects;

public class Rectangle extends Shape {
private int minX;
private int minY;
Expand Down Expand Up @@ -27,4 +29,23 @@ public int getMaxX() {
public int getMaxY() {
return maxY;
}

@Override
public boolean isPointInsideArea(int x, int y) {
return x <= this.getMaxX() && x >= this.getMinX() &&
y >= this.getMinY() && y <= this.getMaxY();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Rectangle rectangle = (Rectangle) o;
return minX == rectangle.minX && minY == rectangle.minY && maxX == rectangle.maxX && maxY == rectangle.maxY;
}

@Override
public int hashCode() {
return Objects.hash(minX, minY, maxX, maxY);
}
}
2 changes: 2 additions & 0 deletions 2-ocp/exercises/exercise1/before/src/main/java/Shape.java
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);
}
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);
}
}

0 comments on commit bb48f56

Please sign in to comment.