Android Open Source - FastBudget3 Category






From Project

Back to project page FastBudget3.

License

The source code is released under:

GNU General Public License

If you think the Android project FastBudget3 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.splitstudio.fastbudget3.db;
/*w  w  w .  j  a v a2 s.c om*/
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.db4o.config.annotations.Indexed;

import de.splitstudio.utils.DateUtils;
import de.splitstudio.utils.db.UniqueEntity;

public class Category extends UniqueEntity implements Comparable<Category> {

  @Indexed
  public String name;

  public int budget;

  private final List<Expense> expenses = new ArrayList<Expense>();

  public Date date;

  public Category(String name, int budget, Date date) {
    this(name);
    this.budget = budget;
    this.date = date;
  }

  public Category(String name) {
    super();
    this.name = name;
  }

  public Category() {}

  /**
   * start or end may be null. in this case it means endless to this direction.
   */
  public int summarizeExpenses(Date start, Date end) {
    int sum = 0;
    for (Expense expense : expenses) {
      if (DateUtils.isBetween(start, expense.date, end)) {
        sum += expense.amount;
      }
    }
    return sum;
  }

  /**
   * Sorts descending by expenses size.
   */
  @Override
  public int compareTo(Category other) {
    int compareBySize = other.expenses.size() - expenses.size();
    if (compareBySize != 0) {
      return compareBySize;
    }
    return name.compareTo(other.name);
  }

  int calculateGrossBudget() {
    int budget = 0;
    Calendar startedMonth = Calendar.getInstance();
    startedMonth.setTime(date);

    Calendar currentMonth = Calendar.getInstance();
    while (!startedMonth.after(currentMonth)) {
      budget += this.budget;
      currentMonth.add(Calendar.MONTH, -1);
    }

    return budget;
  }

  public int calculateBudget() {
    Calendar lastMonth = DateUtils.createFirstDayOfMonth();
    lastMonth.add(Calendar.MILLISECOND, -1);
    return calculateGrossBudget() - summarizeExpenses(null, lastMonth.getTime());
  }

  public List<Expense> findExpenses(Date start, Date end) {
    List<Expense> expenses = new ArrayList<Expense>(this.expenses.size());
    for (Expense expense : this.expenses) {
      if (DateUtils.isBetween(start, expense.date, end)) {
        expenses.add(expense);
      }
    }
    return expenses;
  }

  public Category add(Expense expense) {
    //TreeSet is buggy with db4o
    if (!expenses.contains(expense)) {
      expenses.add(expense);
      Collections.sort(expenses);
    }
    return this;
  }

  public List<Expense> getExpenses() {
    return expenses;
  }

  public void remove(Expense expense) {
    expenses.remove(expense);
  }

}




Java Source Code List

de.splitstudio.fastbudget3.CategoryActivity.java
de.splitstudio.fastbudget3.CategoryListActivity.java
de.splitstudio.fastbudget3.CategoryListDialog.java
de.splitstudio.fastbudget3.ExpenseActivity.java
de.splitstudio.fastbudget3.ExpenseListActivity.java
de.splitstudio.fastbudget3.db.BackupRestore.java
de.splitstudio.fastbudget3.db.CategoryDao.java
de.splitstudio.fastbudget3.db.CategoryListAdapter.java
de.splitstudio.fastbudget3.db.CategoryValidator.java
de.splitstudio.fastbudget3.db.Category.java
de.splitstudio.fastbudget3.db.CsvExport.java
de.splitstudio.fastbudget3.db.ExpenseDao.java
de.splitstudio.fastbudget3.db.ExpenseListAdapter.java
de.splitstudio.fastbudget3.db.Expense.java
de.splitstudio.fastbudget3.db.FastBudgetMigration.java
de.splitstudio.fastbudget3.enums.Extras.java
de.splitstudio.fastbudget3.enums.Fragments.java
de.splitstudio.fastbudget3.enums.RequestCode.java