I have a question with arrayList, in the following case it is a project of an academic center that has the student class and the subject class.
I need to register a student in a subject by means of the attribute 'file' of the class 'Student' and by means of the attribute 'code' of the subject class.
I do not see it clearly, I need another class to make a bridge between these two?
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
public class CentroAcademico {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
ArrayList <Alumno> Alumnos = new ArrayList <Alumno>();
ArrayList <Asignatura> Asignaturas = new ArrayList <Asignatura>();
boolean salir = false;
int opcion;
while (!salir) {
System.out.println("1. Crear alumno.");
System.out.println("2. Crear asignatura.");
System.out.println("3. Matricular alumno.");
System.out.println("4. Salir");
try {
System.out.println("Escribe una de las opciones");
opcion = sn.nextInt();
switch (opcion) {
case 1:
crearAlumno(Alumnos);
break;
case 2:
crearAsignatura(Asignaturas);
break;
case 3:
break;
case 4:
salir = true;
break;
default:
System.out.println("Solo números entre 1 y 4");
}
} catch (InputMismatchException e) {
System.out.println("Debes insertar un número");
sn.next();
}
}
}
public static void crearAlumno(ArrayList <Alumno> Alumnos){
Alumno Datos;
Datos=new Alumno();
Scanner leer = new Scanner(System.in);
System.out.println("Ingrese su Nombre: ");
Datos.nombre=leer.nextLine();
System.out.println("Ingrese su expediente: ");
Datos.expediente=leer.nextInt();
Alumnos.add(Datos);
}
public static void crearAsignatura(ArrayList <Asignatura> Asignaturas){
Asignatura Datos;
Datos=new Asignatura();
Scanner leer = new Scanner(System.in);
System.out.println("Ingrese su codigo: ");
Datos.codigo=leer.nextInt();
System.out.println("Ingrese su nombre: ");
Datos.nombre=leer.next();
System.out.println("Ingrese su creditos: ");
Datos.credito=leer.nextInt();
Asignaturas.add(Datos);
}
public static void matricular(){
}
}
Student Class
public class Alumno {
int expediente;
String nombre;
public Alumno(int expediente, String nombre) {
this.expediente = expediente;
this.nombre = nombre;
}
public Alumno() {
}
public int getExpediente() {
return expediente;
}
public void setNumExpediente(int numExpediente) {
this.expediente = numExpediente;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
@Override
public String toString() {
return "Alumno{" + "numExpediente=" + expediente + ", nombre=" + nombre + '}';
}
}
Subject Class
public class Asignatura {
int codigo;
String nombre;
int credito;
public Asignatura(int codigo, String nombre, int credito) {
this.codigo = codigo;
this.nombre = nombre;
this.credito = credito;
}
public Asignatura() {
}
public int getCodigo() {
return codigo;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public int getCredito() {
return credito;
}
public void setCredito(int credito) {
this.credito = credito;
}
@Override
public String toString() {
return "Asignatura{" + "codigo=" + codigo + ", nombre=" + nombre + ", credito=" + credito + '}';
}