Android: Как рассчитать итоги в модели array-list? - PullRequest
0 голосов
/ 10 октября 2019

[Извините за плохую грамматику] [ЗАПРОСИТЬ РЕДАКТИРОВАТЬ ВОПРОСЫ БУДУТ ЦЕНЫ]

я хочу создать totalHaveMoney, я пытался зацикливать данные income - expense в totalMoney методе, но это не удалось,там только income из одних данных и вычисление не работает

Модель:

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;
    }
}

При получении данных:

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

результат money.totalMoney()как то же самое с income и без расчета


любая помощь будет оценена

1 Ответ

1 голос
/ 10 октября 2019

Если у вас есть List, вы можете перебрать их и добавить

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);
...