My zoo needs to add animals, for that the user writes:
- Number of different animals that the zoo will have. (ex: 3)
- Name of different animals that the zoo will have (ex: Elephant, gorilla, tiger)
- Amount of animals that will be added to the zoo (ex: 20 animals)
A number of objects (of the animal class) that the user wrote are created. (ex: 10 elephants, 5 gorillas, 5 tigers) and they are stored in an arraylist of objects.
public class Animal {
String nombre;
double tamaño;
}
This I have done, what I want to do is change the value of the size attribute of all animals that share the same name as the object arraylist.
Example: all objects with attribute name ="tiger", change the value of the attribute named size to 10. That is, all objects "tiger" will have size 10. All name "gorilla" objects will have size 15, for example.
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Escribe la cantidad de animales distintos que quieres agregar al zoologico: ");
int animalesDistintos = Integer.parseInt(br.readLine());
String[] nombresAnimales = new String[animalesDistintos];
System.out.println("Escribe el nombre de los animales que quieres agregar al zoologico: ");
for(int x=0; x<animalesDistintos; x++)
{
System.out.print(x+">> ");
String nombreAnimal = br.readLine();
nombresAnimales[x] = nombreAnimal;
}
System.out.println("Escribe la cantidad de animales que quieres agregar al zoologico: ");
int cantidadAnimales = Integer.parseInt(br.readLine());
ArrayList<Animal> Animales = new ArrayList<>();
for(int y=0; y<cantidadAnimales; y++)
{
Animal nuevoAnimal = new Animal(nombresAnimales[(int) (Math.random() * nombresAnimales.length)]);
Animales.add(nuevoAnimal);
}
}