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
25 changes: 25 additions & 0 deletions produto-do-array/MultiplyArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.ArrayList;
import java.util.List;

public class MultiplyArray {

public List<Long> multiply(List<Integer> numbers) {

if (numbers.isEmpty()) throw new IllegalArgumentException();
List<Long> result = new ArrayList<>();

Long prefix = 1L;
for (int i = 0; i < numbers.size(); i++) {
result.add(prefix);
prefix *= numbers.get(i);
}

Long suffix = 1L;
for (int i = numbers.size() - 1; i >= 0; i--) {
result.set(i, result.get(i) * suffix);
suffix *= numbers.get(i);
}

return result;
}
}
56 changes: 56 additions & 0 deletions produto-do-array/MultiplyArrayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class MultiplyArrayTest {

private MultiplyArray multiplyArray;

@BeforeEach
public void setUp() {
multiplyArray = new MultiplyArray();
}

@Test
public void shouldReturnNewArrayOfMultiplyAllElementsInArray() {
List<Long> expected = new ArrayList<>(List.of(120L, 60L, 40L, 30L, 24L));
List<Integer> request = new ArrayList<>(List.of(1, 2, 3, 4, 5));

List<Long> result = multiplyArray.multiply(request);

assertNotNull(result);
assertEquals(expected, result);
}

@Test
public void shouldReturnEmptyNewArrayWhenArrayEmpty() {
List<Integer> requestArray = new ArrayList<>();
assertThrows(IllegalArgumentException.class, () -> multiplyArray.multiply(requestArray));
}

@Test
public void shouldReturnNewArrayWhenContainOneZero() {
List<Long> expected = new ArrayList<>(List.of(0L, 8L, 0L));
List<Integer> request = new ArrayList<>(List.of(2, 0, 4));

List<Long> result = multiplyArray.multiply(request);

assertNotNull(result);
assertEquals(expected, result);
}

@Test
public void shouldReturnAllZerosNewArrayWhenContainTwoOrMoreZero() {
List<Long> expected = new ArrayList<>(List.of(0L, 0L, 0L, 0L));
List<Integer> request = new ArrayList<>(List.of(2, 0, 4, 0));

List<Long> result = multiplyArray.multiply(request);

assertNotNull(result);
assertEquals(expected, result);
}
}
43 changes: 43 additions & 0 deletions produto-do-array/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>produto-do-array</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>