Last active
September 27, 2022 16:43
-
-
Save jeffque/218febb91fe85894ee788b1d82718a3d to your computer and use it in GitHub Desktop.
Resolva a soma sem usar laços. Baseado no desafio do Zan
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
import java.math.BigDecimal; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
public class DesafioSemLoop { | |
public static BigDecimal totalCompras(List<Compra> compras) { | |
//TODO implementar a soma sem usar loops while, for, do-while... | |
return BigDecimal.ZERO; // retorno mock só para compilar | |
} | |
// a partir daqui não pode mudar nada | |
public static void main(String... args) { | |
List<Compra> compras = Arrays.asList( | |
new Compra("2022-01-01", | |
Arrays.asList( | |
new ItemCompra("a", 2, new BigDecimal("12.34")), | |
new ItemCompra("b", 1, new BigDecimal("3.99")), | |
new ItemCompra("d", 3, new BigDecimal("98.14")) | |
)), | |
new Compra("2022-01-02", | |
Arrays.asList( | |
new ItemCompra("a", 6, new BigDecimal("12.34")), | |
new ItemCompra("b", 1, new BigDecimal("3.99")), | |
new ItemCompra("c", 1, new BigDecimal("34.02")) | |
)) | |
); | |
System.out.println("O total das compras foi de " + totalCompras(compras).toPlainString()); | |
} | |
public static class Compra { | |
public final String data; | |
public final List<ItemCompra> produtos; | |
public Compra(String data, List<ItemCompra> produtos) { | |
this.data = data; | |
this.produtos = Collections.unmodifiableList(new ArrayList<>(produtos)); | |
} | |
} | |
public static class ItemCompra { | |
public final String cod; | |
public final int qtd; | |
public final BigDecimal valorUnitario; | |
public ItemCompra(String cod, int qtd, BigDecimal valorUnitario) { | |
this.cod = cod; | |
this.qtd = qtd; | |
this.valorUnitario = valorUnitario; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Baseado no desafio do Zan, versão Java