How do I set contacts for a Phone object?

Asked

Viewed 95 times

3

In class Pessoa declared a array of the kind Telefone which stores 3 phone contacts in the set referred to below it for each contact assigns a type and a number.

I have a problem where I want to assign a new type to a person and número using the setContactos() referred to in the test class below.

    public class Pessoa{
        private Telefone[] contactos;
        //....
        public void setContactos(Telefone[] cont) {
        for (int i = 0; i < contactos.length; i++) {
            contactos[i].setNumero(cont[i].getNumero());
            contactos[i].setTipo(cont[i].getTipo());
         }
       }
       //....
    }

    public class Teste {

        public static void main(String[] args) {
            Telefone t1 = new Telefone("Casa", 123456);
            Telefone t2 = new Telefone("telemovel", 123456);
            Pessoa p1 = new Pessoa("Marta Pereira");

            p1.setNIF(3454543);
            p1.setContactos(t1); //erro incompatible types: Telefone cannot be converted to Telefone[];  

            System.out.println(p1.toString());
        }
    }

2 answers

3


You have a few options, one more suited to the name and signing of the method and the other more to what is normally done, and a variation of this.

The first way is to create a array with these phones (even if you only have one you have to create this array) and then pass the array, after all the method expects it. This has some problems like abstraction leak. Something like that:

Pessoa p1 = new Pessoa("Marta Pereira");
p1.setNIF(3454543);
p1.setContactos(new Telefone[] { new Telefone("Casa", 123456), new Telefone("telemovel", 123456) });

I put in the Github for future reference.

If you’re gonna do this I think you should be in builder.

The second way, more conceptually correct, is to change the method to adicionarTelefone() and receive as parameter only Telefone and then within this method you add the phone into a list (no array), and then calls this method with each phone.

Nothing prevents you from already accepting at least one phone in the constructor, and maybe an initial list.

Needs to be a list so you can add new phones and not get fixed size.

Should have the methods RemoverTelefone() and maybe ModificarTelefone() but this last one is more complicated, I think it’s not even worth the effort.

Note that I changed the method name using Telefone and not Contato because it is not a contact that is adding, contact is a broader information.

Seems to me you’re obsessed with using getters and setters, it’s not your fault, there’s a lot of people who spread this fake news And a lot of people buy it like it’s a wonder. Not that I should never use, but it is not to use as they teach around, almost always the use of it is wrong. Start studying more by that question already made.

Another way would be to do more or less the same thing, but instead of creating an object Telefone and pass that object to the method must pass the phone data and the object be created internally. This has both advantages and disadvantages.

I’m sure you’re abusing toString() probably learned from the same bad material.

  • 1

    Getters and setters are a separate modification false insurance policy, they make sense when: 1-In a Setter, before updating the state in this object according to some input, we validate the input. 2 - The return type of a getter is an interface. So we dissociated the implementation from the exposed interface, I think it’s actually something taught out there in many Java/Android courses to make information "private", without explaining the concepts of building an object and why to do it

  • 1

    Reading clean code, there is a reference to this in chapter 6

  • Your method worked well. Thank you

  • Thanks for the @Scrapbench tip

1

As you reported, the error occurs:

"incompatible error types: Phone cannot be converted to Phone[];"

Explanation of the Error: The Phone type cannot be converted to an array of Phones. The method setContacts accepted as input parameter a Array of type Telephone and you are passing a simple phone type object.

Possible solution: To solve this problem, simply pass the correct type for your method, which in this case is an array of the Phone type :

Telefone t1 = new Telefone("Casa", 123456);
Telefone t2 = new Telefone("telemovel", 123456);
Telefone[] contactos = new Telefone[] {t1, t2};
Pessoa p1 = new Pessoa("Marta Pereira");
p1.setNIF(3454543);
p1.setContactos(contactos);
  • Thank you worked perfectly

Browser other questions tagged

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