Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
57 changes: 57 additions & 0 deletions src/com/github/friedrichmatheus/Carrinho.java
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 {

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 para enums sempre é em maiúsculas.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obrigado.

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;
}


}
111 changes: 111 additions & 0 deletions src/com/github/friedrichmatheus/Main.java
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>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O intuito era criar uma casse Produto mas faz sentido também ser Carrinho.


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) {

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ainda é para retorna a lista de Produtos não somente o nome.
Revise as perguntas e os métodos para identificar onde esta pedido o retorno da lista de produtos ou somente um atributo do objeto.

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() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Falta identifica somente os produtos com estoque como verdadeiro.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A, pulei esse da lista sem querer kkkkk

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

revisei o codigo e eu ja tinha feito o metodo

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() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crie uma lista somente com os nomes dos produto com estoque;

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DICA: Alternativa de código: (Assim evita que estoura NullPointerException caso o c.getCategoria() vem null)
.filter(c -> Categoria.Esporte.equals(c.getCategoria))

.map(Carrinho::getValor)
.reduce(new BigDecimal(0),(a, n) -> a.add(n));

Choose a reason for hiding this comment

The 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)
.reduce(BigDecimal.ZERO,BigDecimal::add);

Copy link
Author

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não é muito aceita essa forma de acessar o atributo c.categoria, poderia utilizar como no método acima:
.filter(c -> Categoria.Esporte.equals(c.getCategoria))

.map(Carrinho::getNome)

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The 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);
// }
}