How to access variables of another class of objects by array?

2

I have 2 Classes, which are: Path and Costs .

Path :

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 the Costs class in the Calculate method, I have to get an array of objects of type Route and calculate the value of the trip,

totalPercurso = (kmPercorrida * valorCombustivel) + valorPedagio

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

But how can I do this?

    
asked by anonymous 09.12.2016 / 02:47

1 answer

2

Considering that this total you want to calculate is the total of all the 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 to test 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 some comments:

  • The return of your method is String when in fact 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 the question answer
09.12.2016 / 05:18