Problem to add content in Arraylist

Asked

Viewed 189 times

0

I have an abstract animal class, which feline and canine inherit from it, and I have a client class that owns 2 arrays, one to add felines and one for canines.

However, when adding the feline in the executable class array in a client object, marks error in the line.

Here is my code:

import java.util.ArrayList;
import java.util.Scanner;

public class App {

    Scanner entrada = new Scanner(System.in);
    public static ArrayList<Cliente> clientes;

    public static void main(String[] args) {

        System.out.println("|||||Clínica Veterinária 1.0|||||\n\n");

        int opcao = 0;
        switch(opcao){
        case 1:
            String nome = null;
                        String cpf = null;
            clientes.add(new Cliente(nome, cpf));
            break;
        case 2:
            String nomeCat = null; double pesoCat = 0.0; int idadeCat = 0;
            clientes.adicionaFelino(nomeCat, pesoCat, idadeCat);
            //a IDE marca erro nesta linha acima
                        break;
        default:    
        return null;
        }
}

Animal Class

public abstract class Animal {

    protected double peso;

    public double calculaAnestesia(){
        double ml = 0.0;
        ml = peso*0.3;
        return ml;
    }

}

Feline class

public class Felino extends Animal {

    private String nome;
    private int idade;

    public Felino(String nome, int idade, double peso){
        this.nome = nome;
        this.peso = peso;
        this.idade = idade;

    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }

    public double getPeso(){
        return peso;
    }

    public void setPeso(double peso){
        this.peso = peso;
    }

}

Canine Class

public class Canino extends Animal{

    private String nome;
    private int idade;

    public Canino(String nome, int idade, double peso){
        this.peso = peso;
        this.nome = nome;
        this.idade = idade;

    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }

    public double getPeso(){
        return peso;
    }

    public void setPeso(double peso){
        this.peso = peso;
    }
}

Customer class

import java.util.ArrayList;


public class Cliente {

    private String nome, cpf;
    private ArrayList<Animal> animais;

    public Cliente(String nome, String cpf){
        this.nome = nome;
        this.cpf = cpf;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public ArrayList<Felino> adicionaFelino(String nomeGato, int idade, double peso){
        animais.add(new Felino(nomeGato, idade, peso));
        return null;
    }

    public String adicionaCanino(String nomeCao, int idade, double peso){
        animais.add(new Canino(nomeCao, idade, peso));
        return("animal adicionado");
    }


}
  • 1

    Add the error and all classes informed in the question.

  • It seems to me that neither the problem nor the code presented make sense. The code we can help, but if the problem is all wrong, there’s not much to do.

  • The error is of type, you are passing a double where you get an int. adicionaFelino(String nomeGato, int idade, double peso) has to pass in this order, and you’re passing clientes.adicionaFelino(nomeCat, pesoCat, idadeCat); where weightCat is double. You have reversed weightCat with ageCat. The correct should be: clientes.adicionaFelino(nomeCat, idadeCat, pesoCat);

  • Not yet solved, what is underlined is the added String in the client line.addifeline(nameCat, dateCat, weightCat);

  • clientes is an Arraylist, not your Client object. Therefore, this method adicionaFelino does not exist in it.

  • Yeah, now that I’ve been calling myself I have to go through that do a "getCliente" now find him on the list and add the animal to the correct customer, thanks for the help diego.

  • Did the answer solve your problem? Do you think you can accept it now? @Miltonteixeira See [tour] to understand how it works. It would be helpful to indicate to everyone that the solution was useful and satisfactory for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

Show 2 more comments

2 answers

2

The code had several errors, some very basic syntactics (apart from the disorganization. I fixed to compile. But he still has other problems and he doesn’t make any sense. The classes are not very good, but nothing terrible, but the main() is very confused and only works under very specific circumstances. The solution I gave is not ideal, it was only to be able to compile.

import java.util.ArrayList;
import java.util.Scanner;
 
class App {
 
    Scanner entrada = new Scanner(System.in);
    public static ArrayList<Cliente> clientes;
 
    public static void main(String[] args) {
 
        System.out.println("|||||Clínica Veterinária 1.0|||||\n\n");
 
        int opcao = 0;
        switch(opcao){
        case 1:
            String nome = null;
            String cpf = null;
            clientes.add(new Cliente(nome, cpf));
            break;
        case 2:
            String nomeCat = null; double pesoCat = 0.0; int idadeCat = 0;
            clientes.get(0).adicionaFelino(nomeCat, idadeCat, pesoCat);
            break;
        default:    
            return;
        }
    }
}
 
abstract class Animal {
 
    protected double peso;
 
    public double calculaAnestesia(){
        double ml = 0.0;
        ml = peso*0.3;
        return ml;
    }
}
 
class Felino extends Animal {
 
    private String nome;
    private int idade;
 
    public Felino(String nome, int idade, double peso){
        this.nome = nome;
        this.peso = peso;
        this.idade = idade;
 
    }
 
    public String getNome() {
        return nome;
    }
 
    public void setNome(String nome) {
        this.nome = nome;
    }
 
    public int getIdade() {
        return idade;
    }
 
    public void setIdade(int idade) {
        this.idade = idade;
    }
 
    public double getPeso(){
        return peso;
    }
 
    public void setPeso(double peso){
        this.peso = peso;
    }
}
 
class Canino extends Animal{
 
    private String nome;
    private int idade;
 
    public Canino(String nome, int idade, double peso){
        this.peso = peso;
        this.nome = nome;
        this.idade = idade;
 
    }
 
    public String getNome() {
        return nome;
    }
 
    public void setNome(String nome) {
        this.nome = nome;
    }
 
    public int getIdade() {
        return idade;
    }
 
    public void setIdade(int idade) {
        this.idade = idade;
    }
 
    public double getPeso(){
        return peso;
    }
 
    public void setPeso(double peso){
        this.peso = peso;
    }
}
 
class Cliente {
 
    private String nome, cpf;
    private ArrayList<Animal> animais;
 
    public Cliente(String nome, String cpf){
        this.nome = nome;
        this.cpf = cpf;
    }
 
    public String getNome() {
        return nome;
    }
 
    public void setNome(String nome) {
        this.nome = nome;
    }
 
    public String getCpf() {
        return cpf;
    }
 
    public void setCpf(String cpf) {
        this.cpf = cpf;
    }
 
    public ArrayList<Felino> adicionaFelino(String nomeGato, int idade, double peso){
        animais.add(new Felino(nomeGato, idade, peso));
        return null;
    }
 
    public String adicionaCanino(String nomeCao, int idade, double peso){
        animais.add(new Canino(nomeCao, idade, peso));
        return("animal adicionado");
    }
}

Behold "working" in the ideone. And in the repl it.. Also put on the Github for future reference.

The call of the method adicionaFelino() was wrong because it reversed the arguments and because it was trying to do on the client list and not on the specific client. I used the get(0) to pick up the specific customer and add the feline. This is wrong, but so is the rest. Now you have a better base to fix the problems. You can still ask other, more specific questions.

  • is it took me a while to realize that I was trying to add the animal to the customer list and not to an object, I’m working on it now, and thank you very much friend, your help was WELL enlightening.

2

The error is in this section:

    switch(opcao){
        case 1:
            String nome = null;
            String cpf = null;
            clientes.add(new Cliente(nome, cpf));
            break;
        case 2:
            String nomeCat = null; double pesoCat = 0.0; int idadeCat = 0;
            clientes.adicionaFelino(nomeCat, pesoCat, idadeCat);
            //a IDE marca erro nesta linha acima
            break;
        default:    
            return null;
    }

Turns out you’re calling the method adicionaFelino in a list, but this method exists only within a client object. You will have to call this method in the client object. Example:

Cliente cliente = new CLiente(nome, cpf);

String nomeCat = null; 
double pesoCat = 0.0; 
int idadeCat = 0;

cliente.adicionaFelino(nomeCat, pesoCat, idadeCat);

clientes.add(cliente);

This would be the right way to add a feline to a customer and a customer to the customer list. I am not aware of the rule you are developing, but I believe you will have to change your main code because you are not logically in agreement.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.