How to use an object attribute to increment a counter?

Asked

Viewed 555 times

2

I have the exercise practically solved, it lacks only an "accountant".

"For the determination of the monthly receivables per employee, the clients they raise should be counted. Each customer must have an attribute with the identification of the employee who collected it."

One of the attributes of each Client is the name of the employee who "raised". How, when going through an array with Customers, Employees and even more some types of objects (all of the same super class), I can make the number of clients raised from the Employee increase according to the number of clients raised by him?

I hope you can understand the doubt, I’m having a little trouble explaining what I want..

  • falta apenas um "contador". I can recommend what takes care of my firm, he’s a very nice guy :D

1 answer

2


In the employee class Voce can have a list of clients agariados (0 for N) In the customer class have a reference of 0 for 1 with the employee who participated In the agariar method (in the employee class) receives the client class as reference and adds it in the employee’s list of agariates and arrow the reference of the employee who squatted.

That is in C# would look like this, (in Java should be very similar) :

private abstract class Pessoa {
    private string nome_;
}

private class Funcionario : Pessoa {
    private List<Cliente> clientesAgariados_ = new List<Cliente>();

    private void Agariar(Cliente cliente) { 
        cliente.setAgariador(this);
        clientesAgariados.add(cliente);
    }

    public int ClientesAgariados()
    {
        return clientesAgariados.count;
    }
}

private class Cliente : Pessoa {
    private Funcionario agariador_;

    public void setAgariador(Functionario funcionario)
    {
        agariador_ = funcionario;
    }
}

In the main method you do what is necessary and check the number of customers greeted by the function:

public static Main() {
     Funcionario funcionario = new Funcionario;
     Cliente cliente1 = new Cliente;
     Cliente cliente2 = new Cliente;

     funcionario.Agariar(cliente1);
     funcionario.Agariar(cliente2);

     Console.WriteLine(funcionario.ClientesAgariados().toString());
}
  • It is possible to change something in the class Cliente or Funcionario so you don’t have to use the method Agariar in main?

  • The goal of using object orientation is to be the closest to the real world, if by chance in the real world of your company a customer only exists if this has already been accommodated. it is possible to place this attribution ex: public void Cliente(Funcionario fc) { fc.Agariar(this); } and for this it would be necessary to pass the Employee at the time of creation of the Ex client: new Cliente(funcionario1).

  • For the purpose of this exercise, a customer may exist without being recruited by any employee. Trying in the manner of the above comment, everything is working correctly except for the situation where no one has raised the customer. 'Cause if you try to pass null as the raising employee, happens the Nullpointerexception.

  • If a client can exist without having been accommodated, there will never be the situation where the employee should be passed on to the constructor, because by definition values that stop in the constructor of a class, are indispensable for the existence of the same, what ends up not being the case of the Agariador and the first solution presented is in accordance with this reality.

  • I certainly misunderstood the statement then. With the first solution works perfectly whether the customer has been raised by an employee or not, so I will use this option.

Browser other questions tagged

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