Ver Mensaje Individual
  #1 (permalink)  
Antiguo 07/04/2013, 09:20
Avatar de Atorort
Atorort
 
Fecha de Ingreso: abril-2010
Ubicación: Valencia
Mensajes: 127
Antigüedad: 14 años
Puntos: 0
Acceder a Key del HashMap

Muy buenas, ando desesperado intentando resolver este ejercicio. Trata de un Taller mecánico, en el que puedo añadir partes, listar todos, mostrar partes asociados a matrícula, y eliminar partes.

La actividad la planteo con un HashMap<Matricula,ArrayList> (matrícula es un objeto de la clase Matricula, y el ArrayList contiene los partes asociados).


Mi problema llega en el método mostrar(), y lógicamente el eliminar() no lo tengo hecho porque no consigo acceder a la key matrícula de la manera adecuada.
El containsKey(m) siempre me devuelve False.
Dejo el código:

Código Java:
Ver original
  1. public class Gestion {
  2.     HashMap<Matricula,ArrayList<Partes>> HashPartes = new HashMap<Matricula,ArrayList<Partes>>();
  3.    
  4.     /*anadir Matricula ira asociado a anadirParte porque en el ejercicio especifica que lo que se dan de alta son partes,
  5.     por tanto debe haber una matricula asociada a los mismos. En caso de ya existir la matrícula como Key en el HashMap,
  6.     añadimos el parte al Array de partes asociados a esta.*/
  7.    
  8.    
  9.     //[0]Añadir un parte
  10.     public void anadirParte(){
  11.         Partes parte;
  12.        
  13.         System.out.println("Introduce la matricula:");
  14.         Matricula m = anadirMatricula();
  15.        
  16.         System.out.println("Introduce el código del parte:");
  17.         int codigo = Lector.leerInt();
  18.        
  19.         System.out.println("Introduce la descripción del parte:");
  20.         String descripcion = Lector.leerString();
  21.        
  22.         System.out.println("Introduce el importe del parte:");
  23.         double importe = Lector.leerDouble();
  24.        
  25.         parte = new Partes(m,codigo, descripcion,importe);
  26.         if (!HashPartes.containsKey(parte.getMatricula())){
  27.             ArrayList<Partes> ArrPartes = new ArrayList<Partes>();
  28.             HashPartes.put(parte.getMatricula(), ArrPartes);
  29.             HashPartes.get(parte.getMatricula()).add(parte);
  30.             } else
  31.             HashPartes.get(parte.getMatricula()).add(parte);
  32.     }
  33.  
  34.     private Matricula anadirMatricula() {
  35.         String provincia="";
  36.         int numero=0;
  37.         String letra="";
  38.        
  39.         System.out.println("Introduce la letra asociada a la provincia");
  40.         provincia = Lector.leerString();
  41.         System.out.println("Introduce el número de la matrícula");
  42.         numero = Lector.leerInt();
  43.         System.out.println("Introduce las letras del final de la matrícula");
  44.         letra=Lector.leerString();
  45.        
  46.         Matricula m = new Matricula(provincia,numero,letra);
  47.         return m;
  48.     }
  49.    
  50.     public void listar(){
  51.         //Primero comprobamos que el HashMap no está vacío.
  52.         System.out.println("LISTADO COMPLETO DE PARTES \n ========");
  53.         if(HashPartes.size()!= 0){
  54.            
  55.             //Recorremos las claves por matrícula
  56.             Set<Matricula> keysmatri = HashPartes.keySet();
  57.             Iterator<Matricula> itMatri = keysmatri.iterator();
  58.            
  59.             while (itMatri.hasNext()){
  60.                 itMatri.hasNext();
  61.                 Matricula m = itMatri.next();
  62.                
  63.                 //Y por cada matrícula, recorremos el array de partes
  64.                 ArrayList<Partes> ArrPartes = HashPartes.get(m);
  65.                 Iterator<Partes> itPart = ArrPartes.iterator();
  66.                
  67.                 while(itPart.hasNext()){
  68.                     Partes p = itPart.next();
  69.                     System.out.println("========");
  70.                     System.out.println("Matricula: " + p.getMatricula().getProvincia() +"-"+ p.getMatricula().getNumero()+"-"+p.getMatricula().getLetra() + "\n" +
  71.                                        "Codigo: " + p.getCodigo() + "\n" +
  72.                                        "Descripción: " + p.getDescripcion() + "\n" +
  73.                                        "Importe: " + p.getImporte() + "\n");
  74.                 }
  75.             }  
  76.         }else{
  77.                 System.out.println("No hay partes para esta matrícula o la matrícula no existe.");
  78.         }
  79.     }
  80.    
  81.     public void mostrar(){
  82.         Matricula m = anadirMatricula();
  83.  
  84.         if(HashPartes.containsKey(m)){
  85.             ArrayList<Partes> ArrPartes = HashPartes.get(m);
  86.             Iterator<Partes> itPart = ArrPartes.iterator();
  87.            
  88.             while (itPart.hasNext()){
  89.                 Partes p = itPart.next();
  90.                 System.out.println("========");
  91.                 System.out.println("Matricula: " + p.getMatricula().getProvincia() +"-"+ p.getMatricula().getNumero()+"-"+p.getMatricula().getLetra() + "\n" +
  92.                                    "Codigo: " + p.getCodigo() + "\n" +
  93.                                    "Descripción: " + p.getDescripcion() + "\n" +
  94.                                    "Importe: " + p.getImporte() + "\n");
  95.             }
  96.         } else
  97.             System.out.println("Objeto no localizado");
  98.     }
  99.    
  100.     public void eliminar(){
  101.        
  102.     }
  103. }

Muchas gracias!
__________________
Adrian Tornero Ortiz
Técnico Superior en Desarrollo de Aplicaciones Informáticas
https://www.linkedin.com/in/adriantornero