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.