Basically you already have your program, you only need two things:
Assign data to a new vehicle.
Add the new vehicle to the list.
1. Assign data to a new vehicle:
In the code you published, you have all the necessary data to create a new vehicle, however, you have not created the instance as such of the vehicle, that is, there is no "physically" said vehicle. For this, you have created a Boolean variable, which checks whether the vehicle exists or not, what you must do is create the new instance if it does not exist:
for ( Vehiculo vehiculo : listaVehiculos2) {
if (vehiculo.getCodigo() == codigo); {
existe = true;
}
if (existe) {
System.out.println("ya existe");
} else {
System.out.println("Alta de vehiculo");
nombre = in.next(); // Ingresas el nombre del vehiculo
Vehiculo nuevo = new Vehiculo(codigo, nombre); // el codigo ya lo habias ingresado
System.out.println("Se ha dado de alta un vehiculo");
}
}
In this way, the vehicle would already exist "physically" however, you have not added it to the list yet.
2. Add the new vehicle to the list.
To finish, you only need to call the add
of your list:
listaVehiculos2.add(nuevo);
You can also add it more quickly as follows:
listaVehiculos2.add(new Vehiculo(codigo, nombre));
Your code should look like this:
for ( Vehiculo vehiculo : listaVehiculos2) {
if (vehiculo.getCodigo() == codigo); {
existe = true;
}
if (existe) {
System.out.println("ya existe");
} else {
System.out.println("Alta de vehiculo");
nombre = in.next(); // Ingresas el nombre del vehiculo
listaVehiculos2.add(new Vehiculo(codigo, nombre));
System.out.println("Se ha dado de alta un vehiculo");
}
}
WARNING
If you have already tried the code, you will notice that it is common for you to throw an exception, specifically ConcurrentModificationException , which indicates that it is not possible to modify (in this case, add a new element) to a collection (list) while that list is being iterated.
To avoid this, you have two options:
Take advantage of your Boolean variable to verify if the vehicle exists, and carry out operations out of for
.
Create a copy of your list when iterating.
I'll explain a little:
In case number 1, you could take advantage of your variable to only verify if it exists or not, something similar to this:
for ( Vehiculo vehiculo : listaVehiculos2) {
if (vehiculo.getCodigo() == codigo); {
existe = true;
break; // detiene la ejecución del bucle para continuar
}
}
if (existe) {
System.out.println("ya existe");
} else {
System.out.println("Alta de vehiculo");
nombre = in.next(); // Ingresas el nombre del vehiculo
listaVehiculos2.add(new Vehiculo(codigo, nombre));
System.out.println("Se ha dado de alta un vehiculo");
}
This way you get the expected result.
In case number 2, you could create a copy of your list at the time of iterating, however, this would not be recommended in case the list is too large, the advantage is that you could keep the same code without having to modify it a lot:
for ( Vehiculo vehiculo : new ArrayList<>(listaVehiculos2)) { // Aqui se crea la copia
if (vehiculo.getCodigo() == codigo); {
existe = true;
}
if (existe) {
System.out.println("ya existe");
} else {
System.out.println("Alta de vehiculo");
nombre = in.next(); // Ingresas el nombre del vehiculo
listaVehiculos2.add(new Vehiculo(codigo, nombre));
System.out.println("Se ha dado de alta un vehiculo");
}
}