Responsibility to keep information of items in a class

Asked

Viewed 86 times

-2

There’s an issue that I need to resolve urgently:

Consider the code Venda.java.

Remove the responsibility of keeping information from items sold in the class Venda, creating a new class ItemDeVenda.

import java.util.ArrayList;
import java.util.List;


public class Venda {

    private List<Double> itemValores = new ArrayList<Double>();
    private List<Double> itemQuantidades = new ArrayList<Double>();

    public double total() {
        double soma = 0;
        for (int i = 0; i < itemValores.size(); i++) {
            soma += itemValores.get(i) * itemQuantidades.get(i);
        }
        return soma;
    }

    public void adicionarItem(double valor) {
        itemValores.add(valor);
        itemQuantidades.add(1.0);
    }

    public void adicionarItem(double valor, double quantidade) {
        itemValores.add(valor);
        itemQuantidades.add(quantidade);
    }

}
  • 3

    Accounts receivable and sale is always urgent :). I will prepare the receipt now.

  • What’s your real question?

  • How could I remove the liabilities of the sold items from the Sale class and put in the new class(Itemdevenda).

  • So @Diogonazareno, I think you should rephrase your question, trying to show what you’ve tried to do. Hardly anyone here will be willing to settle this issue without you showing an effort. Your question is very interesting, because it deals with essential aspects of programming, mainly OO programming. Separation of responsibility is fundamental and your question goes in this aspect.

  • Look for responsibility in the O.R. There’s a lot of good stuff in there. Example: http://answall.com/questions/81314/o-que-s%C3%A3o-os-conceitos-de-coes%C3%A3o-e-acoplamento

  • @Cantoni, I’m on. Thank you :)

Show 1 more comment

1 answer

3

  1. Question: What’s in a sold item?

    Answer: Value and quantity.

    So you create a class ItemDeVenda with the value and quantity, which are doubles.

  2. Question: What exactly represents the class Venda?

    Answer: A list of items sold.

    So you replace the two List<Double> by a single List<ItemDeVenda>.

  3. Question: After placing the List<ItemDeVenda>, the methods adicionarItem no more compiling. And now?

    Answer: Change the methods so that an instance of ItemDeVenda be created and then add this instance to the list.

  4. Question: And the method total?

    Answer: Create a getter getTotal() in class ItemDeVenda which multiplies the value by the quantity. Then in the method total() class Venda, you only go through the elements of the list adding up the totals of each.

  5. Question: And the responsibilities?

    Answer: By making the above changes, you will have the values relating to a sales item as well as the methods that operate on each separate item in its own class, while the class Venda refers only to a list of items of sale with the methods that operate on that list. As a result, the class Venda do not need to know the details of the operation of the sale items and the details of each sale item do not need to know the whole concept of the sale as a whole. The name of it is separation of concepts.

    One direct advantage you get from this is to keep the values and quantities together within the same object, rather than separate into different lists. In addition, it is much easier to add new fields of sales items if needed in the future (example, a field to tell if the item has been reversed or returned after purchase), as they will all be together, and not kept in separate lists.

Browser other questions tagged

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