From 57443009f78e4e842669fe36f490cf47f3d4fac5 Mon Sep 17 00:00:00 2001 From: GabrielNNS Date: Mon, 13 Oct 2025 12:03:13 -0300 Subject: [PATCH] resolved --- .gitignore | 2 + calcular-rendimento/IncomeCalculator.java | 36 +++++++++++ calcular-rendimento/IncomeCalculatorTest.java | 59 +++++++++++++++++++ calcular-rendimento/IncomeReport.java | 52 ++++++++++++++++ calcular-rendimento/pom.xml | 43 ++++++++++++++ 5 files changed, 192 insertions(+) create mode 100644 calcular-rendimento/IncomeCalculator.java create mode 100644 calcular-rendimento/IncomeCalculatorTest.java create mode 100644 calcular-rendimento/IncomeReport.java create mode 100644 calcular-rendimento/pom.xml diff --git a/.gitignore b/.gitignore index 441ee49..6c95246 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ # Ignore .DS_Store files .DS_Store +/target/ +/.idea/ diff --git a/calcular-rendimento/IncomeCalculator.java b/calcular-rendimento/IncomeCalculator.java new file mode 100644 index 0000000..7eea9cf --- /dev/null +++ b/calcular-rendimento/IncomeCalculator.java @@ -0,0 +1,36 @@ +import java.math.BigDecimal; +import java.math.RoundingMode; + + +public class IncomeCalculator { + + private final Double selicRateLimit; + + public IncomeCalculator(Double selicRateLimit) { + this.selicRateLimit = selicRateLimit; + } + + public IncomeReport calcuteSavingsIncome(Double initialAmount, Integer months, Double selicRate, Double referenceRate) { + if (initialAmount <= 0 || months <= 0) throw new IllegalArgumentException("Initial Amount or Months invalid"); + Double monthlyYield = calculeMonthlyYield(selicRate, referenceRate); + IncomeReport incomeReport = new IncomeReport(new BigDecimal(initialAmount)); + BigDecimal currentAmount = new BigDecimal(initialAmount); + + for (int i = 1; i <= months; i++) { + BigDecimal monthlyResult = currentAmount; + monthlyResult = monthlyResult.multiply(BigDecimal.valueOf(1 + monthlyYield)).setScale(2, RoundingMode.HALF_UP); + BigDecimal monthlyIncome = monthlyResult.subtract(currentAmount); + incomeReport.addValues(monthlyIncome, monthlyResult); + currentAmount = monthlyResult; + } + + return incomeReport; + } + + private Double calculeMonthlyYield(Double selicRate, Double referenceRate) { + if (selicRate <= 0 && referenceRate <= 0) throw new IllegalArgumentException("Selic and Reference Rate are zero"); + return (selicRate <= selicRateLimit) + ? (selicRate * 0.7 / 12) / 100 + (referenceRate / 100) + : (0.5 / 100 + (referenceRate / 100)); + } +} diff --git a/calcular-rendimento/IncomeCalculatorTest.java b/calcular-rendimento/IncomeCalculatorTest.java new file mode 100644 index 0000000..1b187af --- /dev/null +++ b/calcular-rendimento/IncomeCalculatorTest.java @@ -0,0 +1,59 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + + +public class IncomeCalculatorTest { + + private IncomeCalculator calculator; + + private static final Double SELIC_RATE_LIMIT = 8.5; + private static final Double VALUE = 1000.00; + private static final Integer MONTHS = 12; + + @BeforeEach + public void setUp() { + calculator = new IncomeCalculator(SELIC_RATE_LIMIT); + } + + @Test + public void shouldReturnValueWhenSelicIsBigger85() { // $1061,69 - Selic 9.0 and 12 months + IncomeReport report = calculator.calcuteSavingsIncome(VALUE, MONTHS, 9.0, 0.0); + + assertNotNull(report); + assertEquals(VALUE, report.getInitialAmount().doubleValue()); + assertEquals(MONTHS, report.getAmountMonthly().size()); + assertEquals(1061.69, report.getFinalAmount().doubleValue()); + } + + @Test + public void shouldReturnValueWhenSelicIsSmaller85() { // $1057,46 - Selic 8.0 and 12 months + IncomeReport report = calculator.calcuteSavingsIncome(VALUE, MONTHS, 8.0, 0.0); + + assertNotNull(report); + assertEquals(VALUE, report.getInitialAmount().doubleValue()); + assertEquals(MONTHS, report.getAmountMonthly().size()); + assertEquals(1057.46, report.getFinalAmount().doubleValue()); + } + + @Test + public void shouldReturnValueWhenSelicIsEqual85() { // $1061,15 - Selic 8.5 and 12 months + IncomeReport report = calculator.calcuteSavingsIncome(VALUE, MONTHS, 8.5, 0.0); + + assertNotNull(report); + assertEquals(VALUE, report.getInitialAmount().doubleValue()); + assertEquals(MONTHS, report.getAmountMonthly().size()); + assertEquals(1061.15, report.getFinalAmount().doubleValue()); + } + + @Test + public void shouldReturnExceptionWhenSelicAndReferenceRateIsZero() { + assertThrows(IllegalArgumentException.class, () -> calculator.calcuteSavingsIncome(VALUE, MONTHS, 0.0, 0.0)); + } + + @Test + public void shouldReturnExceptionWhenAmountAndMonthsInvalid() { + assertThrows(IllegalArgumentException.class, () -> calculator.calcuteSavingsIncome(-10.00, -4, 8.0, 0.0)); + } +} diff --git a/calcular-rendimento/IncomeReport.java b/calcular-rendimento/IncomeReport.java new file mode 100644 index 0000000..23a819b --- /dev/null +++ b/calcular-rendimento/IncomeReport.java @@ -0,0 +1,52 @@ +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; + +public class IncomeReport { + + private BigDecimal initialAmount; + private BigDecimal finalAmount; + private List incomeMonthly; + private List amountMonthly; + + public IncomeReport(BigDecimal initialAmount) { + this.initialAmount = initialAmount; + this.finalAmount = new BigDecimal(0); + this.incomeMonthly = new ArrayList<>(); + this.amountMonthly = new ArrayList<>(); + } + + public void addValues(BigDecimal income, BigDecimal amount) { + incomeMonthly.add(income); + amountMonthly.add(amount); + finalAmount = amount; + } + + public StringBuilder generateImport() { + StringBuilder report = new StringBuilder(); + report.append(String.format("Initial Amount: $%.2f\n", initialAmount)); + for(int i = 0; i < incomeMonthly.size(); i++) { + report.append(String.format("Month %d - Income: $%.2f - Monthly Amount: $%.2f\n", + i + 1, incomeMonthly.get(i), amountMonthly.get(i))); + } + report.append(String.format("Final Amount: $%.2f\n", finalAmount)); + report.append("---"); + return report; + } + + public BigDecimal getInitialAmount() { + return initialAmount; + } + + public BigDecimal getFinalAmount() { + return finalAmount; + } + + public List getIncomeMonthly() { + return incomeMonthly; + } + + public List getAmountMonthly() { + return amountMonthly; + } +} diff --git a/calcular-rendimento/pom.xml b/calcular-rendimento/pom.xml new file mode 100644 index 0000000..eb5e38d --- /dev/null +++ b/calcular-rendimento/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + + com.gabriel + calcular-rendimento + 1.0-SNAPSHOT + + + 17 + 17 + + + + + + org.junit.jupiter + junit-jupiter + 5.10.2 + test + + + + + + ${project.basedir} + ${project.basedir} + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + + false + + + + +