Last active
December 20, 2015 16:28
-
-
Save marciojrtorres/6161148 to your computer and use it in GitHub Desktop.
Comentários para tentar deixar o código mais inteligível (ou não), melhor criar métodos auxiliares
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
class PedidoService { | |
public Pedido fazPedido(Carrinho carrinho) { | |
// checar se o carrinho está vazio | |
if (carrinho.getItens().size() == 0) { | |
throw new IllegalArgumentException("Carrinho Vazio"); | |
} | |
// checar se a forma de pagamento foi selecionada | |
if (carrinho.getFormaPagamento() == null) { | |
throw new IllegalArgumentException("Forma Pagamento não selecionada"); | |
} | |
// ################ SE FOR CARTÃO DE CRÉDITO CHECAR SALDO | |
if (carrinho.getFormaPagamento() == CARTAO_CREDITO) { | |
Cliente cliente = carrinho.getCliente(); | |
SegurCredWS sc = new SegurCredWS(); | |
sc.checaCredito(cliente.getCPF() | |
.replace(".", "").replace("-",".")); | |
if (!sc.getSituacao().equals(Situacao.OK)) { | |
throw new NoCreditException("Cliente não tem crédito liberado"); | |
} | |
} | |
// CHECAR SE TODOS OS ITENS ESTÃO NO ESTOQUE | |
EstoqueService estoqueService = new EstoqueService(); | |
for (Item item : carrinho.getItens()) { | |
if (estoqueService.getQuantidadeEstoque( | |
item.getProduto().getCodigo()) < item.getQuantidade()) { | |
throw new SemEstoqueParaItem(item); | |
} | |
} | |
// ... mais 30 linhas de transação (E 10 DE COMENTÁRIOS!!!) | |
// CONSTRUIR O PEDIDO !!!! | |
Pedido pedido = new Pedido(); | |
pedido.setItens(carrinho.getItens()); | |
pedido.setCliente(carrinho.getCliente()); | |
pedido.setFormaPagamento(carrinho.getFormaPagamento()); | |
// SALVAR O PEDIDO NA BASE DE DADOS | |
PedidoDAO pedidoDAO = new PedidoDAO(); | |
pedidoDAO.salva(pedido); | |
} | |
} |
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
class PedidoService { | |
public Pedido fazPedido(Carrinho carrinho) { | |
checaValidadeCarrinho(carrinho); | |
if (carrinho.getFormaPagamento() == CARTAO_CREDITO) { | |
checaCreditoCliente(carrinho.getCliente()); | |
} | |
// ... restante do método chamando métodos menores | |
pedidoDAO.salva(pedido); | |
} | |
private void checaValidadeCarrinho(Carrinho carrinho) { | |
if (carrinho.getItens().size() == 0) { | |
throw new IllegalArgumentException("Carrinho Vazio"); | |
} | |
if (carrinho.getFormaPagamento() == null) { | |
throw new IllegalArgumentException("Forma Pagamento não selecionada"); | |
} | |
} | |
private void checaCreditoCliente(Cliente cliente) { | |
SegurCredWS sc = new SegurCredWS(); | |
sc.checaCredito(cliente.getCPF().replace(".", "").replace("-",".")); | |
if (!sc.getSituacao().equals(Situacao.OK)) { | |
throw new NoCreditException("Cliente não tem crédito liberado"); | |
} | |
} | |
// ... outros métodos menores | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment