Return data from a subobject

Asked

Viewed 66 times

1

Below is what I implemented until then:

public class Interface
{    
    private List<Usuario> usuarios = new ArrayList();   
    private List<Tarefa> tarefas = new ArrayList(); 
    Menu menu = new Menu();

    public void program()
    {
        Scanner entrada = new Scanner(System.in);
        Menu.apresentaMenu();
        int opcao = entrada.nextInt();
        while(opcao!=5)
        {
            switch(opcao)
            {
                case 1:
                System.out.println("Cadastro de usuario");
                System.out.println("Forneça o nome");
                entrada.nextLine();
                String nome = entrada.nextLine();
                System.out.println("Forneça o cpf");
                String cpf = entrada.nextLine();
                Usuario umUsuario = new Usuario();
                umUsuario.setNome(nome);
                umUsuario.setCpf(cpf);

                usuarios.add(umUsuario);
                break;

                case 2 :
                entrada.nextLine();
                System.out.println("Busca de usuario");
                System.out.println("informe o cpf do usuario ");
                cpf = entrada.nextLine();
                System.out.println(usuarios.size());


                for (int i = 0;i<usuarios.size();i++)
                {   if(usuarios.get(i).getCpf().equals(cpf)){
                        Usuario temp = usuarios.get(i);
                        System.out.println(temp.getNome());

                        while(opcao!=0)
                        {
                           switch(opcao)
                            { 
                                case 1:
                                System.out.println("1.Inserir Tarefa");
                                System.out.println("Forneça a descriçao");
                                entrada.nextLine();
                                String desc_tarefa = entrada.nextLine();
                                System.out.println("Forneça o prazo");
                                String prazo_tarefa = entrada.nextLine();
                                Tarefa umaTarefa = new Tarefa();
                                umaTarefa.setDesc_tarefa(desc_tarefa);
                                umaTarefa.setPrazo_tarefa(prazo_tarefa);

                                tarefas.add(umaTarefa);
                                temp.setTarefa(umaTarefa);

                                break;

                                case 2:
                                System.out.println("2.Listar Tarefas");
                                System.out.println(tarefas.size());
                                System.out.println(temp.getTarefa());
                                break;

On the line System.out.println(temp.getTarefa()); I am trying to return the description of the task but it brings me the address of the object in memory.

public class Usuario
{
    private String nome;
    private String cpf;
    private Tarefa tarefa;

    public void setTarefa(Tarefa tarefa)
    {
        this.tarefa = tarefa;
    }

    public Tarefa getTarefa()
    {
        return this.tarefa;
    }

and

public class Tarefa
{
    String desc_tarefa;
    String status_tarefa;
    String prazo_tarefa;

     public void setDesc_tarefa(String desc_tarefa)
    {
        this.desc_tarefa = desc_tarefa ;
    }


    public void setPrazo_tarefa(String prazo_tarefa)
    {
        this.prazo_tarefa = prazo_tarefa ;
    }

     public String getDesc_tarefa()
    {
        return this.desc_tarefa;
    }

    public String getPrazo_tarefa()
    {
        return this.prazo_tarefa;
    }

}

How can I return the task object attribute?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

2 answers

2

Let’s start with the correct term: what you call an attribute actually calls a field. And you want neither an attribute nor a field, you want to call a getter that gives you a value. There are many questions whether it is correct to use a getter, but this is another matter (which has here on the website).

getTarefa() returns an object of type Tarefa, and being an object if you print it will only show an address of where it is (unless you have a ToString() showing something different, but almost always this is wrong, and as in the case of getter, almost every programmer does wrong). To call the description of the object you must pick it up and call the method that gives you the description (getDesc_tarefa()). There is no way he can guess what you want. In programming you have to say in detail everything you want. In the text of your question you say you want the description, in your code you don’t say, you’re just saying to print the object, not the description. So correctly describes:

System.out.println(temp.getTarefa().getDesc_tarefa());

I put in the Github for future reference.

Method and field names are different from the standard used in Java. There are other possible improvements to this code.

1


If you want to access an attribute of Tarefa, not only access the object itself, but the field within it. In your case, the getters of Task return a String. Thus, a solution to your problem may be:

System.out.println(temp.getTarefa().getDesc_tarefa());

Browser other questions tagged

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