Issue
I want to create totalHaveMoney
I was try to looping data income - expense
in totalMoney
method, but this failed. It is only getting income
from one data, so the calculation doesn't work.
Model :
public class MoneyManager {
String name, category;
String date, description, key;
Long income, expense, totalMoney;
public MoneyManager(String name, String category, String date, Long income,Long expense , String description) {
this.name = name;
this.category = category;
this.date = date;
this.income = income;
this.expense = expense;
this.description = description;
}
public MoneyManager() {
}
public Long totalMoney(){
Long haveMoney = getIncome();
for (int i = 0; i < this.getIncome(); i++){
haveMoney -= getExpense();
}
return haveMoney;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Long getExpense() {
return expense;
}
public void setExpense(Long expense) {
this.expense = expense;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Long getIncome() {
return income;
}
public void setIncome(Long income) {
this.income = income;
}
public Long getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(Long totalMoney) {
this.totalMoney = totalMoney;
}
}
When im getting data:
MoneyManager money = moneyManagers.get(position);
Resources resources = context.getResources();
Locale rupiah = new Locale("id", "id");
holder.textName.setText(String.valueOf(money.totalMoney()));
holder.textDate.setText(money.getDate());
The result of money.totalMoney()
is the same as income
.
Solution
If you have a List
of you can iterate through them and add
long sum = 0;
for (MoneyManager mm : listOfMoneyManager) { // or whatever your local variable is
sum += mm.getIncome() - mm.getExpense();
}
// after the iteration you have the total
System.out.println (sum);
Answered By - Scary Wombat
Answer Checked By - Katrina (JavaFixing Volunteer)