Esempio n. 1
0
  public Budget deleteBudget(String categoryToDelete) {
    Budget budget = getLatestBudget();
    BudgetsDAO budgetsDao = new BudgetsDAO(context);
    List<String> categoryList =
        (new LinkedList<>(Arrays.asList(budget.getCategories().split(","))));
    List<String> initialAmountsList =
        (new LinkedList<>(Arrays.asList(budget.getInitialAmounts().split(","))));
    List<String> amountsSpent =
        (new LinkedList<>(Arrays.asList(budget.getAmountsSpent().split(","))));
    int targetIndex = categoryList.indexOf(categoryToDelete);

    categoryList.remove(categoryToDelete);
    initialAmountsList.remove(targetIndex);
    amountsSpent.remove(targetIndex);

    String categoryStr = TextUtils.join(",", categoryList);
    String initialAmountsStr = TextUtils.join(",", initialAmountsList);
    String amountsSpentStr = TextUtils.join(",", amountsSpent);

    budget.setCategories(categoryStr);
    budget.setInitialAmounts(initialAmountsStr);
    budget.setAmountsSpent(amountsSpentStr);
    budget.setAmountAvailable(calculateAmountAvailable(budget));
    budget.setAmountStartedWith(calculateAmountStartedWith(budget));

    Date currentDateForComparison = Util.stringToDate(Util.getCurrentDateTime());
    Date latestDateForComparison = Util.stringToDate(budget.getDateCreated());

    if (currentDateForComparison.after(latestDateForComparison)) {
      budget.setDateCreated(Util.getCurrentDateTime());
      budgetsDao.createBudgetItem(budget);
    } else {
      budgetsDao.updateBudgetAllLists(budget);
    }

    return budget;
  }