in my attempt to make cleaner codes, I want to learn to work with methods, but I do not know how to keep the values of these when they are called in another class
Some of the methods I did are:
Note: I do not know if they are correct or the right way to do it, I only did them following my logic of how little and nothing I know
public void ObtenerNombre() {
Scanner sc = new Scanner(System.in);
String fakeNombre;
System.out.println("Ingrese un N O M B R E");
fakeNombre = sc.next();
if (fakeNombre.isEmpty()) {
System.out.println("No puede dejar el campo vacio");
} else {
if (fakeNombre.matches("[A-Za-z]")) {
System.out.println("Genial \n El nombre " + fakeNombre + "se a guardado");
Nombre = fakeNombre;
} else {
System.out.println("Solo se admiten letras");
}
}
}
public void ObtenerApellidos(){
Scanner sc = new Scanner(System.in);
System.out.println("Ingrese Apellido Paterno");
String fakeAP = sc.next();
if(fakeAP.isEmpty()){
System.out.println("No puede dejar el campo vacio");
}
else{
if (fakeAP.matches("[A-Za-z]")){
System.out.println("Ingrese Apellido Materno");
String fakeAM = sc.next();
if(fakeAM.matches("[A-Za-z]")){
System.out.println("Genial \n los apellidos " + fakeAP + fakeAM + "se han guardado");
Apellido = fakeAP + fakeAM;
}
else{
System.out.println("Solo se admiten letras");
}
}
else{
System.out.println("Solo se admiten letras");
}
}
}
public void EdadPasajero(){
Scanner sc = new Scanner(System.in);
int fakeEdad;
System.out.println("Ingrese la E D A D del pasajero");
fakeEdad = sc.nextInt();
if(fakeEdad > 0){
System.out.println("Edad Correcta");
pEdad = fakeEdad;
}
else{
System.out.println("E D A D no valida");
}
}
The problem is that I do not know how to save those values within a collection, specifically a HashMap
public void addPasajero(){
Pasajero nPasajero = new Pasajero();
nPasajero.ObtenerNombre();
nPasajero.ObtenerApellidos();
nPasajero.EdadPasajero();
nPasajero.EdadPasajero();
agregarPasajero(Rut, new Pasajero(Pasaje, pEdad, Nombre, Apellido, Rut), Pasajeros);
}
public void agregarPasajero(String Rut, Pasajero Pasajero, HashMap<String, Pasajero> Pasajeros) {
if (Pasajeros.containsKey(Rut)) {
System.out.println("No es posible agregar este R U T. El código esta repetido.");
} else {
Pasajeros.put(Rut, Pasajero);
}
}