Error Passing an Object to the Other [closed]

-4

Hey guys. I am new to java and this is an error where I want to pass an object from one class to another object of another class and it gives the following error.

Incompatible types: Patient can not be converted to String . I know what it means but I do not know how to fix it. My test class is as follows.

import java.util.ArrayList;
import model.Hospital;
import model.Medico;
import model.Paciente;
import model.Visitante;
import model.Doenca;


public class Teste {

public static void main(String[] args){


   Paciente novoP = new Paciente("Joao da Silva", "174526", "Jardin das   flores", "1745181");
   novoP.setAlergia("Alergia a agulha");

   Visitante novoV = new Visitante("Maria do Rosario", "14751", "Jardin das Flores", "1475145");
   novoV.setEntrada("12:30");
   novoV.setSaida("2:00");

   Visitante novoV2 = new Visitante("Marta do Rosario", "14758", "Jardin das Flores", "1475125");
   novoV2.setEntrada("12:30");
   novoV2.setSaida("2:00");

   ArrayList<Visitante>visitantes = new ArrayList<>();
   visitantes.add(novoV);
   visitantes.add(novoV2);

   novoP.setVisitas(visitantes);



   Doenca novaD = new Doenca();
   novaD.setNome("Tubercolose");
   novaD.setInternado(novoP); //aqui é onde ocorre o erro, "internado" é o atributo que eu passo o objeto "novoP".



hospital(novo);
hospital(mNovo);

}    
public static void hospital(Hospital hos){
  System.out.println(hos);

}   


}

Class Patient

package model;

import java.util.ArrayList;



public class Paciente extends Pessoa {
private String alergia;
private String idade;
private ArrayList<Visitante>visitas;

public Paciente(String nome, String cpf, String endereco, String  telefone) {
    super(nome, cpf, endereco, telefone);
}


public String getAlergia() {
    return alergia;
}

public void setAlergia(String alergia) {
    this.alergia = alergia;
}

public String getIdade() {
    return idade;
}

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

public ArrayList<Visitante> getVisitas() {
    return visitas;
}

public void setVisitas(ArrayList<Visitante> visitas) {
    this.visitas = visitas;
}

@Override
public String toString() {
    return "Paciente: "+super.toString()+"{" + "alergia:" + alergia + ", idade:" + idade + ", visitas:" + visitas + '}';
}







}

Class Disease

package model;

 import java.util.ArrayList;


 public class Doenca {
 private String nome;
 private String internado;
 private String medicoNome;

public String getNome() {
    return nome;
}

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

public String getInternado() {
    return internado;
}

public void setInternado(String internado) {
    this.internado = internado;
}

public String getMedicoNome() {
    return medicoNome;
}

public void setMedicoNome(String medicoNome) {
    this.medicoNome = medicoNome;
}



@Override
public String toString() {
    return "Doenca{" + "nome:" + getNome() + ", internado:" + getInternado() + ", medicoNome:" + getMedicoNome() + '}';
}






}
    
asked by anonymous 02.11.2018 / 23:06

1 answer

1

The error is quite clear, as you yourself found:

Here is the method that directs the variable internado :

public void setInternado(String internado) {...}

It gets a String . Now, what you are passing to this method is Paciente :

novaD.setInternado(novoP);

novoP , in this case, is type Paciente , as it happens here: Paciente novoP = new Paciente(...);

To solve this error, it would be best to modify your Doenca class to deal directly with an object of type Paciente , which I imagine is your intention (to have a patient associated with the disease), correct? >

1) Instead of String internado , have Paciente internado .

2) The setInternado() method will then receive a Paciente .

3) The getInternado() method returns a Paciente .

    
03.11.2018 / 18:18