0
I created an application that receives some data and gathers in a ArrayList of a class that is in another package. How do I recover an attribute that is private by getter that is in the other class that is not the application?
package pkgpendrive;
import java.util.Scanner;
public class PenDrive {
    private String marca, modelo;
    private int capacidade;
    private double preco;
    public void setCapacidade(String capacidade){
        this.capacidade = capacidade;
    }
    public String getCapacidade(){
        return capacidade;
    }
    public void setPreco(double preco){
        this.preco = preco;
    }
    public double getPreco(){
        return preco;
    }
   .
   .
   .
    public void relatorio(){
        System.out.printf("%-5s %-6s %-10d %-5.2f\n", getMarca, getModelo, getCapacidade, getPreco);
    }
    public PenDrive(){
    }
}
In the application the iteration is created and the report is shown:
import pkgpendrive.PenDrive;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class AppPenDrive_2 {
    public static void main(String[] args) {
        boolean continua = true;
        int escolha = 0, i, totCapacidade = 0, digito;
        double totPreco = 0;
        Scanner kb = new Scanner(System.in);
        ArrayList<PenDrive> penDrive = new ArrayList<PenDrive>();        
        while (continua) {
            PenDrive aux = new PenDrive();
            System.out.println("Pen Drive -- Adicionar [1] Excluir [2]\nParar Aplicaçao [3]");            
            escolha = Integer.parseInt(kb.nextLine());
            if(escolha == 1){
                aux.entradaDados();
                penDrive.add(aux);
            } else {
                if(escolha == 2){
                    i=0;
                    Iterator<PenDrive> iterar = penDrive.iterator();
                    while(iterar.hasNext()){
                        System.out.print("[" +i + "] = " );
                        iterar.next().relatorio();
                        i++;
                    }
                    System.out.print("Excluir [#]: ");
                    digito = Integer.parseInt(kb.nextLine());
                    penDrive.remove(digito);
                } else {
                    continua = false;
                }
            }
        }
        Iterator<PenDrive> iterar = penDrive.iterator();
        System.out.println("\n");
        System.out.println("Marca Modelo Capacidade Preço");
        System.out.println("----- ------ ---------- ------");
        while (iterar.hasNext()) {
            iterar.next().relatorio();
        }
        System.out.println("----- ------ ---------- ------");
        while(iterar.hasNext()){
            totCapacidade += iterar.next().getPreco();
            totPreco += iterar.next().getCapacidade();            
        }
        System.out.println("Capacidade Total: " + totCapacidade);
        System.out.printf("Preço Total: R$%.2f\n", totPreco);
        System.out.println("Quantidade: " + penDrive.size());
    }
}
Output:
Marca Modelo Capacidade Preço
----- ------ ---------- ------
ACME  AC32X  32         220,00
XPTO  ARM32  32         260,00
BEAR  BR16A  16         120,00
----- ------ ---------- ------
Capacidade Total: 0
Preço Total: R$0,00
Quantidade: 3
The idea was to go out like this, as in another application I did, but in this case it was a vector of objects:
Marca Modelo Capacidade Preço
----- ------ ---------- ------
ACME  AC32X  32         220,00
XPTO  ARM32  32         260,00
BEAR  BR16A  16         120,00
----- ------ ---------- ------
Capacidade Total: 80
Preço Total: R$600,00
Quantidade: 3
Just reiterating the question: how do I take the values of a private attribute of another class and sum with another in the application class?
Oops, I was going to answer all this to him now but you were faster! :)
– Flávio Lisbôa
No words to thank you and Flávio, who helped me on another platform. I knew the "
foreach" from other periods but did not want to use because my current teacher did not teach, only withiterator- that you also helped. Thanks.– commandbear