-
Notifications
You must be signed in to change notification settings - Fork 10
PR - Matheus - Pratica de Lambda #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.github.friedrichmatheus; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class Carrinho { | ||
|
||
public enum Categoria { | ||
Tecnologia, Esporte, Vestuario, Equipamentos, Lazer | ||
} | ||
|
||
Integer id; | ||
String nome; | ||
BigDecimal valor; | ||
Boolean temEstoque; | ||
Categoria categoria; | ||
|
||
public Carrinho(Integer id, String nome, BigDecimal valor, Boolean temEstoque, Categoria categoria) { | ||
this.id = id; | ||
this.nome = nome; | ||
this.valor = valor; | ||
this.temEstoque = temEstoque; | ||
this.categoria = categoria; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
public String getNome() { | ||
return nome; | ||
} | ||
public void setNome(String nome) { | ||
this.nome = nome; | ||
} | ||
public BigDecimal getValor() { | ||
return valor; | ||
} | ||
public void setValor(BigDecimal valor) { | ||
this.valor = valor; | ||
} | ||
public Categoria getCategoria() { | ||
return categoria; | ||
} | ||
public void setCategoria(Categoria categoria) { | ||
this.categoria = categoria; | ||
} | ||
public Boolean getTemEstoque() { | ||
return temEstoque; | ||
} | ||
public void setTemEstoque(Boolean temEstoque) { | ||
this.temEstoque = temEstoque; | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.github.friedrichmatheus; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.AbstractMap; | ||
import java.util.AbstractMap.SimpleEntry; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import com.github.deividfrancis.Person; | ||
import com.github.friedrichmatheus.Carrinho.Categoria; | ||
|
||
public class Main { | ||
|
||
private static List<Carrinho> carrinhoList = new ArrayList<Carrinho>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. O intuito era criar uma casse |
||
|
||
static { | ||
carrinhoList.add(new Carrinho(744, "Redragon Kumara", new BigDecimal(243.35), Boolean.TRUE, Categoria.Tecnologia)); | ||
carrinhoList.add(new Carrinho(850, "Redragon Cobra", new BigDecimal(190.20), Boolean.TRUE, Categoria.Tecnologia)); | ||
carrinhoList.add(new Carrinho(22, "SSD M.2", new BigDecimal(102.02), Boolean.TRUE, Categoria.Tecnologia)); | ||
carrinhoList.add(new Carrinho(848, "Monitor 24p 165hz", new BigDecimal(1153.00), Boolean.FALSE, Categoria.Tecnologia)); | ||
carrinhoList.add(new Carrinho(254, "Oculos de ciclismo", new BigDecimal(104.02), Boolean.TRUE, Categoria.Esporte)); | ||
carrinhoList.add(new Carrinho(78, "Molinete de pesca", new BigDecimal(175.00), Boolean.FALSE, Categoria.Esporte)); | ||
carrinhoList.add(new Carrinho(415, "Tenis allstar", new BigDecimal(167.00), Boolean.TRUE, Categoria.Vestuario)); | ||
carrinhoList.add(new Carrinho(403, "Luva de motociclista", new BigDecimal(134.00), Boolean.FALSE, Categoria.Vestuario)); | ||
carrinhoList.add(new Carrinho(625, "chave de fenda magnética", new BigDecimal(55.00), Boolean.TRUE, Categoria.Equipamentos)); | ||
carrinhoList.add(new Carrinho(573, "Tapete Geometrico", new BigDecimal(114.00), Boolean.TRUE, Categoria.Lazer)); | ||
} | ||
|
||
public static void main(String[] args) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Não consigo vincular de forma simples qual método é a resposta de cada pergunta, fica como dica adicionar um comentário com a questão para ajudar quem vai corrigir kkk. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Realmente ficou bem confuso |
||
// getTecnologia(); | ||
// getValorMaior(); | ||
// getTemEstoque(); | ||
// getSumEsport(); | ||
// getEquipamento(); | ||
// getOrderList(); | ||
// getGroupByCategory(); | ||
// getHighestPrice(); | ||
} | ||
|
||
public static void getTecnologia() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ainda é para retorna a lista de Produtos não somente o nome. |
||
List<String> carrinhoList = Main.carrinhoList.stream() | ||
.filter(c -> c.getCategoria() == Categoria.Tecnologia) | ||
.map(Carrinho::getNome) | ||
.collect(Collectors.toList()); | ||
|
||
System.out.println(carrinhoList); | ||
} | ||
|
||
public static void getValorMaior() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Falta identifica somente os produtos com estoque como verdadeiro. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A, pulei esse da lista sem querer kkkkk There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
List<String> carrinhoList = Main.carrinhoList.stream() | ||
.filter(c -> c.getValor().compareTo(new BigDecimal(200)) == 1) | ||
.map(Carrinho::getNome) | ||
.collect(Collectors.toList()); | ||
|
||
System.out.println(carrinhoList); | ||
} | ||
|
||
public static void getTemEstoque() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Aqui esta correto retorna somente os nomes. |
||
List<String> carrinhoList = Main.carrinhoList.stream() | ||
.filter(c -> c.temEstoque) | ||
.map(Carrinho::getNome) | ||
.collect(Collectors.toList()); | ||
|
||
System.out.println(carrinhoList); | ||
} | ||
|
||
public static void getSumEsport() { | ||
BigDecimal carrinhoList = Main.carrinhoList.stream() | ||
.filter(c -> c.getCategoria() == Categoria.Esporte) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DICA: Alternativa de código: (Assim evita que estoura |
||
.map(Carrinho::getValor) | ||
.reduce(new BigDecimal(0),(a, n) -> a.add(n)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DICA: Uma alternativa para essa linha seria: (ps: nao executei para ver se tem algum detalhe na implentação) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implementei e deu tudo certo! |
||
|
||
System.out.println(carrinhoList.intValue()); | ||
} | ||
|
||
public static void getEquipamento() { | ||
String carrinhoList = Main.carrinhoList.stream() | ||
.filter(c -> c.categoria == Categoria.Equipamentos) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Não é muito aceita essa forma de acessar o atributo |
||
.map(Carrinho::getNome) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Não deveria pegar somente os nomes mas sim o Objeto inteiro |
||
.findFirst() | ||
.get(); | ||
|
||
System.out.println(carrinhoList); | ||
} | ||
|
||
public static void getOrderList() { | ||
List<String> carrinhoList = Main.carrinhoList.stream() | ||
.map(Carrinho::getNome) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Não deveria pegar somente os nomes mas sim o Objeto inteiro. |
||
.sorted() | ||
.toList(); | ||
|
||
System.out.println(carrinhoList); | ||
} | ||
|
||
public static void getGroupByCategory() { | ||
Map<Carrinho.Categoria,List<Carrinho>> carrinhoMap = Main.carrinhoList.stream() | ||
.collect(Collectors.groupingBy(Carrinho::getCategoria)); | ||
|
||
System.out.println(carrinhoMap); | ||
} | ||
|
||
// public static void getHighestPrice() { | ||
// Map<Carrinho, Map<Carrinho, BigDecimal>> carrinhoMap = Main.carrinhoList.stream() | ||
//,Collectors.))); | ||
// | ||
// System.out.println(carrinhoMap); | ||
// } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Achei legal ter criado um
enum
para categorias, mas o padrão java paraenums
sempre é em maiúsculas.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Obrigado.