programación i - Universidad ORT Uruguay
programación i - Universidad ORT Uruguay
programación i - Universidad ORT Uruguay
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
132 Programación I<br />
}<br />
public Paquete obtenerPaquetePorNumero(int unNumero) {<br />
Paquete retorno = null;<br />
Paquete auxBuscar = new Paquete();<br />
auxBuscar.setNumero(unNumero);<br />
int indice = this.getListaPaquetes().indexOf(auxBuscar);<br />
if (indice != -1) {<br />
retorno = (Paquete) this.getListaPaquetes().get(indice);<br />
}<br />
return retorno;<br />
}<br />
Supongamos que incorpora un requerimiento adicional: calcular el total de paquetes por cliente.<br />
Una forma de implementar este pedido es utilizando un HashMap, con clave el cliente y valor la<br />
cantidad de paquetes que tuvo. Así, en la clase Empresa se agrega:<br />
public HashMap totalPaquetesPorClienteTable() {<br />
HashMap map = new HashMap();<br />
Persona aux;<br />
for (int k=0; k < this.getListaPaquetes().size(); k++) {<br />
aux =(this.getListaPaquetes().get(k)).getCliente();<br />
if (!map.containsKey(aux)){<br />
// no esta el cliente, lo ingreso en el map<br />
map.put(aux,1); // utiliza "autoboxing"<br />
}<br />
else {<br />
// encontre el cliente, le sumo 1<br />
map.put(aux,map.get(aux)+1); // aquí hace el "autoboxing"<br />
}<br />
}<br />
return map;<br />
}