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");
}
}
}