Mas ja é… vou colocar o código das classes… talvez fique mais claro entender essa “confusão” que estou fazendo kkk Estou colocando como estava antes da sugestão de criação de uma classe Value, ok?
public interface Calculable {
public Double getValue();
}
public abstract class Transaction implements Calculable {
protected Integer id;
protected String title;
protected User user;
protected YearMonth period;
public Transaction(Integer id, String title, User user, YearMonth period) {
this.id = id;
this.title = title;
this.user = user;
this.period = period;
}
public Integer getId() {
return id;
}
public String getTitle() {
return title;
}
public User getUser() {
return user;
}
public YearMonth getPeriod() {
return period;
}
}
public abstract class SingleTransaction extends Transaction {
protected Double value; //Não tenho o getValue da classe SingleTransaction. Ele vem da Interface Calculable
public SingleTransaction(Integer id, String title, User user, YearMonth period, Double value) {
super(id, title, user, period);
this.value = value;
}
}
public abstract class CompositeTransaction extends Transaction {
protected List<Operation> values;
public CompositeTransaction(Integer id, String title, User user, YearMonth period, List<Operation> values) {
super(id, title, user, period);
this.values = values;
}
public List<Operation> getValues() {
return values;
}
}
public class SingleExpense extends SingleTransaction {
public SingleExpense(Integer id, String title, User user, YearMonth period, Double value) {
super(id, title, user, period, value);
}
//Interface Calculable
@Override
public Double getValue() {
return this.value;
}
}
public class CompositeExpense extends CompositeTransaction {
public CompositeExpense(Integer id, String title, User user, YearMonth period, List<Operation> values) {
super(id, title, user, period, values);
}
//Interface Calculable
@Override
public Double getValue() {
Double amount = 0.;
for (Operation operation : values) {
amount += operation.getValue();
}
return amount;
}
}
public class Operation {
private Integer id;
private String title;
private Double value;
public Operation(Integer id, String title, Double value) {
this.id = id;
this.title = title;
this.value = value;
}
public Integer getId() {
return id;
}
public String getTitle() {
return title;
}
public Double getValue() {
return value;
}
}
Informações importantes:
-
As classes abstratas SingleTransaction e CompositeTransaction foram criadas por que eu irei colocar receitas também, que são iguais as despesas mas são transações diferentes…
-
Respondendo ao seu questionamento anterior, na CompositeExpense eu criei um list de Operations e não de Expense por que na Lista eu não preciso ter novamente o Period… somente um titulo e valor (a principio…depois existirão variações…)
Opções até este momento:
-
Criar uma classe Value e alterar o retorno do método getValue da interface Calculable. A Classe Value receberá um valor e uma list de Operations, ou somente uma lista de Operations…
-
Algo que pensei: Ao invés de existir SingleExpense e CompositeExpense… existiria apenas Expense… e um atributo values sendo um list de Operations. Se uma Expense for simples esse list tem apenas uma linha…se for composta tem N linhas… mas ficaria algo do tipo:
Despesa Luz com um list de uma linha no valor , por exemplo, de R$200,00
Ai não sei se isso seria uma gambiarra ou não!