public Budget logBudgetExpense(String category, String amountSpent) { Budget budget = getLatestBudget(); BudgetsDAO budgetsDao = new BudgetsDAO(context); List<String> categories = Arrays.asList(budget.getCategories().split(",")); List<String> amountsSpent = Arrays.asList(budget.getAmountsSpent().split(",")); int targetIndex = categories.indexOf(category); double targetAmountSpent = Double.parseDouble(amountsSpent.get(targetIndex)); double newAmountSpent = targetAmountSpent + Double.parseDouble(amountSpent); amountsSpent.set(targetIndex, String.valueOf(newAmountSpent)); String amountsSpentStr = TextUtils.join(",", amountsSpent); budget.setAmountsSpent(amountsSpentStr); budget.setAmountAvailable(calculateAmountAvailable(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.updateBudgetAmountSpent(budget); } return budget; }
public Budget editBudgetCategoryAndInitialAmount(String category, String inputAmount) { Budget budget = getLatestBudget(); BudgetsDAO budgetsDao = new BudgetsDAO(context); List<String> categories = Arrays.asList(budget.getCategories().split(",")); List<String> initialAmounts = Arrays.asList(budget.getInitialAmounts().split(",")); int targetIndex = categories.indexOf(category); if (inputAmount != null && !inputAmount.isEmpty()) { initialAmounts.set(targetIndex, inputAmount); } String categoriesStr = TextUtils.join(",", categories); String initialAmountsStr = TextUtils.join(",", initialAmounts); budget.setInitialAmounts(initialAmountsStr); budget.setCategories(categoriesStr); budget.setAmountStartedWith(calculateAmountStartedWith(budget)); budget.setAmountAvailable(calculateAmountAvailable(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.updateBudgetCategoryAndInitialAmount(budget); } return budget; }
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; }