Return data from a subobject

2

Here's what I've implemented so far:

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 line System.out.println(temp.getTarefa()); I'm trying to return the job description but it brings me the object's address 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 do to return the attribute of the task object?

    
asked by anonymous 15.11.2018 / 15:45

2 answers

1

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

System.out.println(temp.getTarefa().getDesc_tarefa());
    
15.11.2018 / 16:05
2

Let's start by using the correct term: what you call the attribute actually calls the field . And you do not want either an attribute or a field, or you want to call a getter that gives you a value. There are many questions if it is correct to use a getter , but this is another subject (which has here on the site ).

getTarefa() returns an object of type Tarefa , and being an object if you send print will only show an address where it is (unless you have ToString() showing something different, but

15.11.2018 / 16:05