Percorrendo SetList por Cliente

2 respostas
G

Olá, estou fazendo um trabalho onde tenho as classes ItemPedido, Pedido, Cliente.
Preciso criar um metodo de busca que retorne um Set ,que busque em um SetList de clientes, o cliente dado pelo usuario e o seu pedido.
Estou tendo dificuldades nesse código, alguém por favor pode me ajudar?

get e set ocultos.

public class Pedido {

    private long pedidoId;
    private Date dataEmissao;

    private Set<ItemPedido> itens;
    private Set<Cliente> clientes;

public Pedido() {
        super();
        this.itens = new HashSet<ItemPedido>();
        this.clientes = new HashSet<Cliente>();
    }

    public Pedido(long pedidoId) {
        this.pedidoId = pedidoId;
        
   
    }

    @Override
    public String toString() {
        return "Pedido{" + "pedidoId=" + pedidoId + "dataEmissao=" + dataEmissao +'}';
    }

    public void cadastrar(Cliente cliente, ItemPedido itempedido){

        this.getClientes().add(cliente);
        this.getItens().add(itempedido);

    }

    public Set buscarPorCliente(Cliente cliente, String nomeConsulta) {
        for(int i=0; i<cliente.pedido.clientes.size(); i++){
           cliente.pedido.clientes.contains(cliente.getNome());
        }
        return clientes;
    }
}

Clientes

public class Cliente {

    private String nome;
    private long clienteId;
    private Pedido pedido;

public Cliente() {
    }

    public Cliente(String nome, long clienteId) {
        this.nome = nome;
        this.clienteId = clienteId;
    }

    @Override
    public String toString() {
        return "Cliente{" + "nome=" + nome + "clienteId=" + clienteId + '}';
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Cliente other = (Cliente) obj;
        if ((this.nome == null) ? (other.nome != null) : !this.nome.equals(other.nome)) {
            return false;
        }
        if (this.clienteId != other.clienteId) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + (this.nome != null ? this.nome.hashCode() : 0);
        hash = 47 * hash + (int) (this.clienteId ^ (this.clienteId >>> 32));
        return hash;
    }
}

item pedido

public class ItemPedido {

    private String item;
    private int quantidade;
    private float precoUn;


    public ItemPedido() {
    }

    public ItemPedido(String item, int quantidade, float precoUn) {
        this.item = item;
        this.quantidade = quantidade;
        this.precoUn = precoUn;
    }

    @Override
    public String toString() {
        return "ItemPedido{" + "item=" + item + "quantidade=" + quantidade + "precoUn=" + precoUn + '}';
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ItemPedido other = (ItemPedido) obj;
        if ((this.item == null) ? (other.item != null) : !this.item.equals(other.item)) {
            return false;
        }
        if (this.quantidade != other.quantidade) {
            return false;
        }
        if (Float.floatToIntBits(this.precoUn) != Float.floatToIntBits(other.precoUn)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 29 * hash + (this.item != null ? this.item.hashCode() : 0);
        hash = 29 * hash + this.quantidade;
        hash = 29 * hash + Float.floatToIntBits(this.precoUn);
        return hash;
    }
}

main

public class Main {

    public static void main(String[] args){

        Scanner entrada = new Scanner(System.in);

        int opcao = 0;
        do{
           System.out.println(" \n1 - Cadastrar Pedido\n"
                             +" 2 - Consultar por cliente\n"
                             +" 3 - Remover por cliente  \n"
                             +" 4 - Atualizar Itens \n"
                             +" 5 - Carregar Itens  \n"
                             +" 6 - Remover Itens   \n"
                             +" 7 - Salvar Itens    \n"
                             +" 8 - Sair            \n"
                             

                             +"\nDigite a opção desejada:");

           opcao = entrada.nextInt();

           switch (opcao){
               case 1:
                   System.out.println("Digite o nome do cliente:");
                   String nome = entrada.nextLine();
                   entrada.nextLine();
                   System.out.println("Digite o ID do cliente:");
                   long clienteId = entrada.nextLong();
                   Cliente cliente = new Cliente(nome, clienteId);
                   
                   System.out.println("Digite o nome do Item:");
                   String item = entrada.nextLine();
                   entrada.nextLine();
                   System.out.println("Digite a quantidade de itens:");
                   int quantidade = entrada.nextInt();
                   System.out.println("Digite o preço do item:");
                   float precoUn = entrada.nextFloat();
                   ItemPedido itempedido = new ItemPedido(item, quantidade, precoUn);
                   
                   System.out.println("Digite o ID do pedido:");
                   long pedidoId = entrada.nextLong();
                   Pedido pedido = new Pedido(pedidoId);
                   pedido.cadastrar(cliente, itempedido);
                   
                   break;

               case 2:
                   
                   

                   break;
               case 3:
                   

                   break;
               case 4:
                   
                   break;
               case 5:
                   ;
                   break;
               case 6:

                   break;
               case 7:
                   
                   break;



           }
    }while(opcao!=8);


    }

}

2 Respostas

dyeimys

Boa tarde Gabriel,

Use um iterador para percorrer seu HashSet

Exemplo aqui encontrado na net.

public class MyClass
{
   public static void main()
   {
      HashSet hSet = new HashSet();

      // Adicionar alguns elementos no HashSte
      hSet.add("This");
      hSet.add(" is");
      hSet.add(" a");
      hSet.add(" test.");

      // Recuperar um Interator para o &lt;Set&gt;
      Iterator iter = hSet.iterator();
 
      //Extrair elementos de iterador.
      while(iter.hasNext()) 
      {
         System.out.print(iter.next());
         //Neste caso voce pode usar outros metodos, como get
         iter.remove();
      }
   }
}
G

Mas essa solução vai puxar pra mim o cliente que eu quero buscar junto com seus pedidos?
Não entendi muito bem!

Criado 26 de setembro de 2012
Ultima resposta 26 de set. de 2012
Respostas 2
Participantes 2