Error trying to sum all through a class in Java

0

I'm doing a small project on expense control, and I'm having a problem calculating total expenses. I'm having a class called Expense which is an abstract class, and other classes called Transport , Food and Daily will inherit from it the total value of expenses and two other methods. And I also have a separate class called Expenses Manager with a method that will pass the Expenses class parameter and get the total value of all other classes, and add the total value within a variable called total expenses. The problem is that when I calculate one of the three classes that they are inheriting, the total value of the Expense class will go to all, and I wanted to separate the total value of each class and then the total sum of the three classes.

Class Expenses Manager :

public class GerenciadorDespesas {

    private int qtdeAlimentacao;
    private int qtdeTransporte;
    private int qtdeDiaria;
    private double totalAlimentacao;
    private double totalTransporte;
    private double totalDiaria;
    private double totalDespesas;

    public int getQtdeAlimentacao() {
        return qtdeAlimentacao;
    }
    public void setQtdeAlimentacao(int qtdeAlimentacao) {
        this.qtdeAlimentacao = qtdeAlimentacao;
    }
    public int getQtdeTransporte() {
        return qtdeTransporte;
    }
    public void setQtdeTransporte(int qtdeTransporte) {
        this.qtdeTransporte = qtdeTransporte;
    }
    public int getQtdeDiaria() {
        return qtdeDiaria;
    }
    public void setQtdeDiaria(int qtdeDiaria) {
        this.qtdeDiaria = qtdeDiaria;
    }
    public double getTotalAlimentacao() {
        return totalAlimentacao;
    }
    public void setTotalAlimentacao(double totalAlimentacao) {
        this.totalAlimentacao = totalAlimentacao;
    }
    public double getTotalTransporte() {
        return totalTransporte;
    }
    public void setTotalTransporte(double totalTransporte) {
        this.totalTransporte = totalTransporte;
    }
    public double getTotalDiaria() {
        return totalDiaria;
    }
    public void setTotalDiaria(double totalDiaria) {
        this.totalDiaria = totalDiaria;
    }
    public double getTotalDespesas() {
        return totalDespesas;
    }
    public void setTotalDespesas(double totalDespesas) {
        this.totalDespesas = totalDespesas;
    }

    public void analisarDespesas(Despesa despesa) {
    setTotalAlimentacao(despesa.getValorTotal());
    setTotalTransporte(despesa.getValorTotal());
    setTotalDiaria(despesa.getValorTotal());
    setTotalDespesas(getTotalAlimentacao() + getTotalTransporte() +
            getTotalDiaria());

    }


}

Expense Class:

import javax.swing.JOptionPane;

public abstract class Despesa {

    private String descricao;
    private double valorTotal;

    public String getDescricao() {
        return descricao;
    }
    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }
    public double getValorTotal() {
        return valorTotal;
    }
    public void setValorTotal(double valorTotal) {
        this.valorTotal = valorTotal;
    }

    public void cadastrarDespesa() {
        setValorTotal(0);
        setDescricao(JOptionPane.showInputDialog("Digite a descrição da Despesa : "));
    }


    public abstract void calcularDespesa();
    public abstract void listarDespesa();

}

I'm sorry if the explanation of the problem got too big or if the code got too big, please, if anyone can help me I'll be very grateful.

    
asked by anonymous 12.04.2016 / 07:40

1 answer

1

You have said: " ... Manager Expenses with a method that will pass by parameter the Expense class and get the total value of all other classes , and sum the total amount inside of a variable called total expenses.The problem is that when I calculate one of the three classes that are inheriting, the total value of the Expense class will go to all, being that I wanted to separate the total value of each class and then the total sum of the three classes. "

The solution is to create a static ArrayList of Expenses within Expenses and every time a new "Expense" object is created, it adds it to this Array. This will save all objects created within the class itself

import java.util.ArrayList;

import javax.swing.JOptionPane;

public abstract class Despesa {

private static ArrayList<Despesa> despesa=new ArrayList<Despesa>();

private String descricao;
private double valorTotal;

public String getDescricao() {
    return descricao;
}

public void setDescricao(String descricao) {
    this.descricao = descricao;
}

public double getValorTotal() {
    return valorTotal;
}

public void setValorTotal(double valorTotal) {
    this.valorTotal = valorTotal;
}
   public void cadastrarDespesa() {
        setValorTotal(0);
        setDescricao(JOptionPane.showInputDialog("Digite a descrição da Despesa : "));
    }


    public abstract void calcularDespesa();
    public abstract void listarDespesa();

    public static ArrayList<Despesa> getDespesa() {
        return despesa;
    }

}

To add all Expenses in ManagementExpenses If you want to add only one, that first method works

PS: I just do not understand why you are assigning the same value to food, transport and daily ... But vlw

public void analisarDespesas(Despesa desp) {
    double ali = 0;
    double trans = 0;
    double dia = 0;
    double total = 0;

    for (Despesa despesa : Despesa.getDespesa()) {
        ali += despesa.getValorTotal();
        trans += despesa.getValorTotal();
        dia += despesa.getValorTotal();
        total += ali + trans + dia;
    }
    setTotalAlimentacao(ali);
    setTotalTransporte(trans);
    setTotalDiaria(dia);
    setTotalDespesas(total);
}

Thank you for giving that UP in response :)

    
12.04.2016 / 14:53