ArrayList Enter people data by screen and show the people Introduced

1

Hello very good I have a problem with my code and it took 2 days without being able to finish it my problem is that I have a scanner where I am entering the data of the people (Name, last name, age, etc ...) these data are lost in sets that I have previously created in the People class and doing an extended of another class that is called rich people, I create the array list adding the previously entered data name last name etc ... and when I go to register 2 people only me the person that I have discharged for the last time does not leave the two people discharged I send the code:

public class Main {



    public static void main(String[] args) {

        ArrayList <PersonaRica> gente = new ArrayList<PersonaRica>();
         boolean salir = false;
         Scanner Scan = new Scanner(System.in);
         PersonaRica p1 = new PersonaRica();
         int opcion;




 while (!salir){ 
             System.out.println("1.- Insertar una Persona");   
             System.out.println("2.- Eliminar una Persona"); 
             System.out.println("3.- Mostrar Personas ");
             System.out.println("4.- Salir ");



             System.out.println("[Indicame la Funcion]:");
             opcion = Scan.nextInt();

                 switch (opcion) {       
                     case 1:
                         // Nombre de la persona
                          System.out.println("Nombre del alumno: ");
                          String nombre = Scan.next();

                          System.out.println("Apellido: ");
                          String apellidos = Scan.next();


                          System.out.println("Año de nacimiento: ");
                          int anynacimiento = Scan.nextInt();


                          System.out.println("Edad: ");
                          int edad = Scan.nextInt();


                          System.out.println("Dinero: ");
                          int dinero = Scan.nextInt();


                          System.out.println("Provincia: ");
                          String provincia = Scan.next();


                          System.out.println("Ciudad: ");
                          String ciudad = Scan.next();


                          System.out.println("DNI: ");
                          String dni = Scan.next();



                          //Llamamos a a los sets para ir introduciendo los valores.
                          p1.setNombre(nombre);
                          p1.setApellidos(apellidos);
                          p1.setAnyoNacimiento(anynacimiento);
                          p1.setEdad(edad);
                          p1.setDinero(dinero);
                          p1.setProvincia(provincia);
                          p1.setCiudad(ciudad);
                          p1.setDni(dni);
                          gente.add(p1);

                          System.out.println("Se ha dado de alta correctamente!");
                          break;

                     case 2:
                         String nombre1;


                         System.out.println("El Nombre ha eliminar: ");
                         nombre1 = Scan.next();

                         for (Persona persona: gente) {
                             if(p1.getNombre().equals(nombre1)){             
                                     gente.remove(p1);
                                     System.out.println("Se ha eliminado correctamente!");

                                  }else {

                                      System.out.print("No se ha encontrado la persona.....");                                
                                  }
                             }
                         break;
                     case 3:

                        //Para mostrar la Persona que deseas.

                         for (int i = 0; i < gente.size(); i++) {
                                 System.out.println("Persona: " + gente.get(i).getNombre());
                                 break;
                             }

                          break;

                      case 4:
                        salir = true;
                        System.out.println("3[31mGracias por utilizar el programa!");
                        break;
                 }  
                 }



    }



}
    
asked by KratosDofus 15.11.2018 в 10:55
source

1 answer

2

Explanation

It does not appear to you since in the for you have to show people (within the case 3 ):

 //Para mostrar la Persona que deseas.
     case 3:
     for (int i = 0; i < gente.size(); i++) {
          System.out.println("Persona: " + gente.get(i).getNombre());
          break;
     }

     break;

You are doing break (inside the loop), so that your for loop is only executed once (performs a single iteration maximum, and none as a minimum).

Why?

When the ArrayList (people) has one or more data, the for loop will be performed only once ( always ), because when you reach break , the loop for and continue with the rest of the code. When the ArrayList (people) does not have data, the loop for will not occur at any time (which is correct).

Error adding new 'PersonaRica' to the Arraylist

Also note that on the line where you are defining PersonaRica (p1) and add it to the ArrayList:

p1.setNombre(nombre);
p1.setApellidos(apellidos);
p1.setAnyoNacimiento(anynacimiento);
p1.setEdad(edad);
p1.setDinero(dinero);
p1.setProvincia(provincia);
p1.setCiudad(ciudad);
p1.setDni(dni);
gente.add(p1);

You are always adding p1 (which is a global variable of your class), with which, you are always adding the same person, you can think that you are changing the characteristics to p1 and then adding it to ArrayList (people), but by modifying p1 , you are modifying all the people that your ArrayList (people) contain, since they all point to the same PersonaRica .

How do you solve this problem?

Before adding a PersonaRica to your ArrayList (people), you specify that p1 is a new person, that is:

p1 = new PersonaRica(); //Creas una nueva instancia de PersonaRica
p1.setNombre(nombre);
p1.setApellidos(apellidos);
p1.setAnyoNacimiento(anynacimiento);
p1.setEdad(edad);
p1.setDinero(dinero);
p1.setProvincia(provincia);
p1.setCiudad(ciudad);
p1.setDni(dni);
gente.add(p1);

Error deleting people

You can not delete PersonaRicas of your ArrayList (agent) because when you iterate over the ArrayList (agent), in the for loop you specify that the local variable to be created is persona : for (Persona persona: gente) , however, when making comparisons and removing a person, you use p1 .

The code would go from:

case 2:
String nombre1;
System.out.println("El Nombre ha eliminar: ");
nombre1 = Scan.next();

for (Persona persona: gente) {
    if(p1.getNombre().equals(nombre1)){             
          gente.remove(p1);
          System.out.println("Se ha eliminado correctamente!");
    }else {
          System.out.print("No se ha encontrado la persona.....");                                
    }
}
break;

a:

case 2:
String nombre1;
System.out.println("El Nombre ha eliminar: ");
nombre1 = Scan.next();

for (Persona persona: gente) {
    if(persona.getNombre().equals(nombre1)){             
          gente.remove(persona);
          System.out.println("Se ha eliminado correctamente!");
    }else {
          System.out.print("No se ha encontrado la persona.....");                                
    }
}
break;
    
answered by 15.11.2018 / 11:43
source