How to access variables from another class of objects per array?

Asked

Viewed 1,069 times

2

I have two classes, : Route and Costs.

Route :

public double getKmPercorrida() {
    return kmPercorrida;
}
public void setKmPercorrida(double kmPercorrida) {
    this.kmPercorrida = kmPercorrida;
}
public double getValorCombustivel() {
    return valorCombustivel;
}
public void setValorCombustivel(double valorCombustivel) {
    this.valorCombustivel = valorCombustivel;
}
public double getValorPedagio() {
    return valorPedagio;
}
public void setValorPedagio(double valorPedagio) {
    this.valorPedagio = valorPedagio;
}

public void cadastrarPercurso() {
    setKmPercorrida(Double.parseDouble(JOptionPane.showInputDialog(null,"Digite os km percorridos : ","Km percorridos",JOptionPane.QUESTION_MESSAGE)));
    setValorCombustivel(Double.parseDouble(JOptionPane.showInputDialog(null,"Digite o valor do combustivel : ","Combustivel",JOptionPane.QUESTION_MESSAGE)));
    setValorPedagio(Double.parseDouble(JOptionPane.showInputDialog(null,"Digite o valor do pedágio : ","Pedágio",JOptionPane.QUESTION_MESSAGE)));
}

public void listarPercurso() {
    JOptionPane.showMessageDialog(null,"Km percorridos : " + getKmPercorrida() +
            "Valor do combustivel : "  + getValorCombustivel() +  
            "Valor do pedágio : " + getValorPedagio());
}

Costs :

private double totalPercurso;


public double getTotalPercurso() {
    return totalPercurso;
}

public void setTotalPercurso(double totalPercurso) {
    this.totalPercurso = totalPercurso;
}



public String calcularViagem(Percurso [] p) {


    return "";



}

In class Costs in the method calculate i have to receive an array of objects of type Route,and calculate the value of the trip, :

totalPercurso = (kmPercorrida * valorCombustivel) + valorPedagio

The variable totalPercurst will receive the variables of the Path object and calculate them all through the method in which it is.

But how can I do this ?

  • 1

    It was not clear. When it comes to object-oriented programming, we do not speak of "class variables" but "object variables". But I don’t see any Route object being used in your class "Costs".

  • I’m receiving it as an array of objects there in the calculator method,.

  • 1

    Again, it makes no sense to talk about "total class". Class is just a template to create objects. Besides, what do you mean by "total"? Total of what?

  • Total value together with other variables. I have to add all of them by the total variable.

1 answer

2


Considering that this total you want to calculate is the total of all attributes, the calculation (since it is not clear in the question) would look like this:

public String calcularViagem(Percurso[] p) {
  totalPercurso = 0;

  for (int indice = 0; indice < p.length; indice++) {
    Percurso percurso = p[indice];

    totalPercurso = totalPercurso + (percurso.getKmPercorrida() * percurso.getValorCombustivel()) + percurso.getValorPedagio();
  }

  return String.valueOf(totalPercurso);
}

And the method for testing the solution:

public static void main(String[] args) {
  Percurso percurso1 = new Percurso();
  percurso1.cadastrarPercurso();
  percurso1.listarPercurso();

  Percurso percurso2 = new Percurso();
  percurso2.cadastrarPercurso();
  percurso2.listarPercurso();

  Percurso[] percursos = new Percurso[]{percurso1, percurso2};
  Custos custos = new Custos();

  System.out.println(custos.calcularViagem(percursos));
}

Now a few remarks:

  • The return of your method is as String when in reality it is a calculation. It should be equal to the type of its quantitative variables, which in this case is double;
  • You are mixing presentation elements with business elements, in this case using JOptionPane within your business/model classes. See more how you could better organize your project in answer to the question How the MVC framework works for Desktop applications? that despite being in C++ can be applied to virtually any type of project.
  • The total would be the sum of all the variables of the Path object, and this I would have to do by the parameter I am receiving. Your example helped me, but I need to calculate all the variables of the object and not just one. And the MVC model I already understand yes, but since it’s an application that I’m just doing at home and not at work,.

  • 1

    @Mondial Edit your question describing better what this calculation is. With examples, please, because we are not understanding.

  • @Pabloalmeida All right, I’m sorry if I didn’t make the question very clear,I’m new here and still don’t know very well how to post correctly.

  • @Mondial changed to match with its statement

  • @Thank you Sorack, I tested your answer and it worked.

  • @Mondial I think the best approach is to use a List and not a array.

  • I managed to make it work right with your approach, thank you very much. But why would it be better to use List and not array ?

Show 2 more comments

Browser other questions tagged

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