Error while trying to add everything together through a Java class

Asked

Viewed 106 times

0

I’m doing a small project on expense control, I’m with a class called Expense which is an abstract class, and other classes called Transport,Feeding and Daily will inherit from it the total value of expenses and two other methods. And I also have a separate class called Expense manager with a method that will pass the class by parameter Expense and take the total value of all other classes, and sum the total value within a variable called total expenses. The problem is that when I calculate one of the three classes that are inheriting, Expense 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.

Class Expense 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();

}

Excuse me, if the explanation of the problem has gotten too big or if the code has gotten too big, please,.

  • 1

    Actually, you can’t answer with just this. You have less information. But something tells me I shouldn’t be using inheritance.

  • The other three classes only have the registration of variables and their listing and the calculation nothing else.Now about the inheritance,worked in my other project,I do not know why this one is not working.

  • 1

    Ah, yes if it works on a project, it will work on everyone. I don’t even know what it means worked. You mean you compiled? OOP is not this.

  • When I said this I meant that it worked with practically the same code and with a class that was also a manager that had the same function as this one. If you want to take the irony and sarcasm side that’s fine, but I didn’t insult you in any way to come do this.

1 answer

1


You spoke: "...Expense manager with a method that will pass the Expense class by parameter and take the value total of all other classes,and sum the total value within 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 Expense Arraylist within Expenses and every time a new "Expense" object is created add it to that Array. This way all objects created within the class are saved

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 the Expenses in the Management If you want to add just one, that first method of yours works

PS:I just didn’t understand why you’re assigning the same value to power, transfer 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 the reply :)

  • In fact, everyone would take the total value of the Expense class, since in the three classes both take the total value variable with getters and setters. That’s why I’m assigning the same value.In another project I did something like this, it worked normally,.

  • I tried your code, but it wasn’t, :)

Browser other questions tagged

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