Skip to content

Instantly share code, notes, and snippets.

@jeffque
Last active September 27, 2022 16:43
Show Gist options
  • Save jeffque/218febb91fe85894ee788b1d82718a3d to your computer and use it in GitHub Desktop.
Save jeffque/218febb91fe85894ee788b1d82718a3d to your computer and use it in GitHub Desktop.
Resolva a soma sem usar laços. Baseado no desafio do Zan
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;
}
}
}
@jeffque
Copy link
Author

jeffque commented Sep 22, 2022

Baseado no desafio do Zan, versão Java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment