Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

# Ignore .DS_Store files
.DS_Store
/target/
/.idea/
36 changes: 36 additions & 0 deletions calcular-rendimento/IncomeCalculator.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
59 changes: 59 additions & 0 deletions calcular-rendimento/IncomeCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
52 changes: 52 additions & 0 deletions calcular-rendimento/IncomeReport.java
Original file line number Diff line number Diff line change
@@ -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<BigDecimal> incomeMonthly;
private List<BigDecimal> 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<BigDecimal> getIncomeMonthly() {
return incomeMonthly;
}

public List<BigDecimal> getAmountMonthly() {
return amountMonthly;
}
}
43 changes: 43 additions & 0 deletions calcular-rendimento/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.gabriel</groupId>
<artifactId>calcular-rendimento</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<!-- JUnit 5 (Jupiter) -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<!-- Definindo seus diretórios personalizados -->
<sourceDirectory>${project.basedir}</sourceDirectory>
<testSourceDirectory>${project.basedir}</testSourceDirectory>

<plugins>
<!-- Plugin para rodar os testes -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
</plugins>
</build>
</project>