Records with Arraylist java

0

I have to create a program which registers students and instructors with arraylist

How can I use the instructor values to register it in the same course arraylist to register students to the class

package examen;
import java.util.ArrayList;
import java.util.Scanner;

public class Examen {
    public static void main(String[] args) {
      int opc;
           boolean banInstructor = false;
           boolean banAlumno = false;
           boolean banCurso = false;
      Scanner lector=new Scanner(System.in);
       ArrayList<Instructores> inst=new ArrayList<>();
      Instructores instructor;
       ArrayList<Alumnos> al=new ArrayList<>();
       Alumnos alumno;
         ArrayList<Cursos> curs=new ArrayList<>();
       Cursos curso ;

       do{
       System.out.println("Menu Principal");
        System.out.println("0: Salir");
        System.out.println("1: Instructores");
        System.out.println("2: Alumnos");
        System.out.println("3: Cursos");
        System.out.println("4: Registro");
        System.out.println("5: Calificaciones");        
        opc=lector.nextInt();
    switch(opc){

        case 1:
        for (int i=1; i<=3; i++){
        System.out.println("Dame el Nombre Instructor "+i);
        String nombre = lector.next();
         System.out.println("Dame la edad del Instructor "+i);
        int edad = lector.nextInt();
        instructor =new Instructores(nombre,edad);
        inst.add(instructor);
        }
        for(Instructores aux:inst){
        System.out.println("Instructor: "+aux.getNombre()+" edad: "+aux.getEdad());
     }
          banInstructor = true;
        break;
           case 2:
        if(banInstructor == true){
               for (int i=1; i<=2; i++){
        System.out.println("Dame el Nombre Alumno "+i);
           String nombrea = lector.next();
         System.out.println("Dame la edad del Alumno "+i);
           int edada = lector.nextInt();

           alumno =new Alumnos(nombrea,edada,0);
        al.add(alumno);
         }
         for(Alumnos aux:al){
        System.out.println("Alumno: "+aux.getNombre()+" edad: "+aux.getEdad());
          }
        }
        else{
         System.out.println("Ingrese primero los instructores ");
        }
        banAlumno = true;
         break;

         case 3:
        if(banInstructor == true&& banAlumno == true){
             for (int i=1; i<=2; i++){
        System.out.println("Dame el Nombre Curso "+i);
       String nombre =lector.next();
         curso =new Cursos(nombre);


        for(Instructores aux:inst){
        System.out.println("Instructor: "+aux.getNombre());
     }
        System.out.println("Elige Un Instructor:"); 
        String elegido=lector.next();
           for(Instructores aux:inst){
              if( elegido.equals(aux.getNombre())){
               curso.setInstructor(aux);
           }
              else{
                  System.out.println("Instructor no encontrado");
              }
           }

        curs.add(curso);
         }   
         }

        else{
         System.out.println("Ingrese primero los Instructores y Alumnos ");
        }

         break;
         case 4:
             System.out.println(" Resgistro de Alumnos a Cursos");







              break;

         case 5:

             for(Alumnos aux:al){

            System.out.println("Alumno: "+aux.getNombre()+" edad: "+aux.getEdad());
             }
            for (int i=1; i<=2; i++){


            System.out.println("Dame la Calificacion del Alumno "+i);
        int calificacion = lector.nextInt();



            }
             for(Alumnos aux:al){
        System.out.println("Alumno: "+aux.getNombre()+" edad: "+aux.getEdad()+"Calificacion: "+aux.getCalificacion());
          }    

             break;


    }



    }while(opc!=0);

}
}
    
asked by Lorenzo 13.03.2018 в 07:12
source

1 answer

2

From what I see you have a variable instructor within the course, so to assign it simply take the corresponding course and assign it to the instructor:

// Coger el curso de la lista (o creas uno nuevo con new)
Cursos curso = curs.get(i);
// Le asignas el instructor
curso.setInstructor(instructor);

Therefore to assign students to a course you have two options, if you want to continue using List , you can create a variable students within the course that is a list and you are assigning students:

// Dentro de cursos
List<Alumnos> alumnos;
    // En el constructor
    alumnos = new ArrayList<Alumnos>();

    // Creas el método addAlumno
    public addAlumno(Alumnos alumno){
        alumnos.add(alumno);
    }

// En el main cuando quieras asignarlos
curso.addAlumno(alumno);

Or else you use a HashMap to assign them directly to the main:

// Creas el HashMap
HashMap<Cursos, Alumnos> alumnosCursos = new HashMap<Cursos, Alumnos>();
// Asignas la relación
alumnosCursos.put(curso, alumno);
    
answered by 13.03.2018 в 09:03