Modify Map Java

0

I have a problem to modify a HashMap, I enter the Rut data (as a key) and its values, but when I want to modify the key it replaces the values, I do not know if I understand.

For example, the values entered are Rut: 11, Name: aa, Position bb, when modifying it should (I want to do it) replace the Rut with a new one, example 12, but on the list it returns '11 - 12 ' replacing the Name and Cargo values with the new Rut

I do not know if I'm OK with the fact that you can modify the key and keep its values, or if you can give me a suggestion to modify (since they ask me to modify, but you can modify anything)

Here's the code link

import java.util.*;

/**
*
* @author Fabio
*/
public class Menu {

HashMap<String, worker> Trabajadores = new HashMap<>();

Scanner sc = new Scanner(System.in);

public void Menu(){
    boolean salir = false;
    int opcion;
    String Cargo, Nombre, Rut;

    while(!salir) {      
        System.out.println("Menu Opciones");
        System.out.println("[1] D A T O S   T R A B A J A D O R E S");
        System.out.println("[2] L I S T A R");
        System.out.println("[3] Salir");

        try{
            System.out.println("=== Ingrese Numero de Opcion ===");
            opcion=sc.nextInt();

            switch (opcion){
                case 1:
                    System.out.println("Eliga que desea H A C E R");
                    System.out.println("[1] A G R E G A R");
                    System.out.println("[2] M O D I F I C A R");
                    System.out.println("[3] E L I M I N A R");
                    opcion=sc.nextInt();
                    switch (opcion){
                        case 1:
                            System.out.println("Introduce el R U T del trabajador:");
                            Rut = sc.next();
                            System.out.println("introduce el N O M B R E de trabajador:");
                            Nombre = sc.next();
                            System.out.println("Introduce el C A R G O de trabajador");
                            Cargo = sc.next();
                            guardarTrabajador(Rut, new worker(Nombre, Cargo), Trabajadores);
                            break;


                        case 2:
                            System.out.println("R U T de Trabajador que desea M O D I F I C A R: ");
                            Rut = sc.next();
                            modificaTrabajador(Rut, Trabajadores);
                            break;

                        case 3:
                            System.out.println("Introduce el R U T del Trabajador que deseas E L I M I N A R: ");
                            Rut = sc.next();
                            eliminaTrabajador(Rut, Trabajadores);
                            break;


                        default:
                            System.out.println("Opcion no valida");
                    }
                    break;

                case 2:
                    mostrarTrabajador(Trabajadores);
                    break;
                case 3:
                    System.out.println("Usted a decidido salir1");
                    salir = true;
                    break;
                default:
                    System.out.println("Opcion debe ser entre 1 y 5");

        }

        }catch(InputMismatchException e){
            System.out.println("Debe introducir un numero");
            sc.next();
        }
    }
    System.out.println("Fin del menu");
}

public static void guardarTrabajador(String Rut, worker worker, HashMap 
<String,worker> Trabajadores){
if (Trabajadores.containsKey(Rut)) {
    System.out.println("No es posible agregar este R U T. El código esta repetido.");
} else {
    Trabajadores.put(Rut, worker);              
    }
}    

public static void modificaTrabajador(String Rut, HashMap Trabajadores){/////////////////////////////////////////////////////////////////////////////
Scanner sc = new Scanner(System.in);
if (Trabajadores.containsKey(Rut)) {
    System.out.println("Nuevo R U T de Trabajador: ");
    Trabajadores.put(Rut, sc.next());            
 } else {
    System.out.println("No hay trabajadores con este R U T");  
     }
  }

 public static void mostrarTrabajador(HashMap<String, worker> Trabajadores) {
String search;
Iterator<String> work = Trabajadores.keySet().iterator();
System.out.println("Los trabajadores registrados son: ");
while(work.hasNext()){
    search = work.next();
    System.out.println(search + " - " + Trabajadores.get(search));
    }        
}  

public static void eliminaTrabajador(String Rut, HashMap<String,worker> Trabajadores) {
if (Trabajadores.containsKey(Rut)) {
    Trabajadores.remove(Rut);
 } else {
    System.out.println("No hay trabajadores con este R U T");  
    }      
  }    

}
    
asked by iFabio 22.11.2017 в 04:49
source

1 answer

0

The error is that you are modifying the value of the key Rut , by the entry of scanner

Trabajadores.put(Rut, sc.next());
//               11  ,  12

That's why the key is not modified (in this line) , that's why you get the results of 11 - 12 , if you want to modify not only the value but the key, the option would be to first remove the value with the key to modify, and then add a new key and value,

  

since according to the implementation of class HashMap , the attribute    key is declared as final K key; so it can not   be overwritten.

public static void modificaTrabajador(String Rut, HashMap Trabajadores){
    Scanner sc = new Scanner(System.in);
    if (Trabajadores.containsKey(Rut)) {

        System.out.println("Nuevo R U T de Trabajador: ");
        String rutx = sc.next();
        // Removemos del HashMap la antigua clave - valor
        Trabajadores.remove(Rut);
        System.out.println("Nuevo Nombre de Trabajador: ");
        String name = sc.next();
        System.out.println("Nuevo Cargo de Trabajador: ");
        String cargo = sc.next();
        //Agregamos Nuevo Elemento con clave rutx
        Trabajadores.put(rutx, new worker(name,cargo) );            
     } else {
        System.out.println("No hay trabajadores con este R U T");  
     }
}

The scenario in this example is that the main keys are not usually modified, the RUT is one of them, so there would be no reason to require the new key, only the new values for nombre and cargo .

if (Trabajadores.containsKey(Rut)) {
    System.out.println("Nuevo Nombre de Trabajador: ");
    String name = sc.next();
    System.out.println("Nuevo Cargo de Trabajador: ");
    String cargo = sc.next();
    //Agregamos  Elemento con clave Rut(Sin modificarla)
    Trabajadores.put(Rut, new worker(name,cargo) );            
 }
    
answered by 22.11.2017 / 05:43
source