diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/Budget.java b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/Budget.java deleted file mode 100644 index 37e45c2d8e8eb29883d670f0d9ff2d9bb70a7c32..0000000000000000000000000000000000000000 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/Budget.java +++ /dev/null @@ -1,11 +0,0 @@ -package no.ntnu.idatt1002.demo.data.Budget; - -public interface Budget { - - void addToBudget(int AmountToAddToBudget); - void setAmountBudget(int amountBudget); - void setBudgetPeriod(int budgetPeriod); - void subtractFromBudget(int amountToSubtractFromBudget); -// public void divideBudgetInDifferentCategories(ExpenseCategory category); - -} diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/BudgetItem.java b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/BudgetItem.java new file mode 100644 index 0000000000000000000000000000000000000000..976a772a405e8d964f467ef792f56c5bde3295bd --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/BudgetItem.java @@ -0,0 +1,27 @@ +package no.ntnu.idatt1002.demo.data.Budget; + +import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory; + +public class BudgetItem { + private double budgetAmount; + private ExpenseCategory category; + private String description; + + public BudgetItem(double budgetAmount, String description, ExpenseCategory category){ + this.budgetAmount = budgetAmount; + this.description = description; + this.category = category; + } + + public double getBudgetAmount() { + return budgetAmount; + } + + public ExpenseCategory getCategory() { + return category; + } + + public String getDescription() { + return description; + } +} diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FoodBudget.java b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FoodBudget.java deleted file mode 100644 index 58b34823fcad8272db6d07c6f02488d911465b13..0000000000000000000000000000000000000000 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FoodBudget.java +++ /dev/null @@ -1,128 +0,0 @@ -package no.ntnu.idatt1002.demo.data.Budget; - -import java.sql.Date; -import java.time.Duration; -import java.time.LocalDate; -import java.time.ZoneId; -import java.time.temporal.ChronoUnit; -import java.time.temporal.Temporal; -import java.util.Calendar; - -/** - * Class that represents FoodBudget and implements the Budget interface. - * - * @author Adele - */ -public class FoodBudget implements Budget { - - private int budgetAmount; - - private int budgetPeriod; - - /** - * The constructor of a new FoodBudget. - * - * @param budgetPeriod The period the budget are going to last for - * @param budgetAmount The amount that the budget consist of - * - */ - public FoodBudget(int budgetPeriod, int budgetAmount){ - this.budgetPeriod = budgetPeriod; - this.budgetAmount = budgetAmount; - } - - /** - * This method returns the amount in the foodBudget as an int - * - * @return the amount left in the foodBudget - * - */ - public int getBudgetAmount() { - return budgetAmount; - } - - /** - * - * @return - * - */ - public int getBudgetPeriod() { - return budgetPeriod; - } - - /** - * This method returns the amount of days left in the period as a string - * - * @return - * - */ - public long getDaysLeftOfBudgetPeriod() { - LocalDate today = LocalDate.now(); - LocalDate end = today.plus(Duration.ofDays(getBudgetPeriod())); - - Date todaysDate = (Date) Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant()); - Date endDate = (Date) Date.from(end.atStartOfDay(ZoneId.systemDefault()).toInstant()); - - Calendar cStart = Calendar.getInstance(); cStart.setTime(todaysDate); - Calendar cEnd = Calendar.getInstance(); cEnd.setTime(endDate); - - long timeLeft = ChronoUnit.DAYS.between((Temporal) cStart, (Temporal) cEnd); - - return timeLeft; - } - - /** - * - * @param amountBudget - * - */ - @Override - public void setAmountBudget(int amountBudget) { - if (amountBudget < 0) { - throw new IllegalArgumentException("Illegal amount in budget"); - } - - budgetAmount = amountBudget; - } - - /** - * - * @param periodOfTheBudget - * - */ - @Override - public void setBudgetPeriod(int periodOfTheBudget) { - if (periodOfTheBudget < 0) { - throw new IllegalArgumentException("Illegal period for the budget"); - } - - budgetPeriod = periodOfTheBudget; - } - - /** - * - * @param amountToAddToBudget - * - */ - @Override - public void addToBudget(int amountToAddToBudget) { - if (amountToAddToBudget < 0){ - throw new IllegalArgumentException("Amount to be added to the budget cant be below zero"); - } - budgetAmount += amountToAddToBudget; - } - - /** - * - * @param amountToSubtractFromBudget - * - */ - @Override - public void subtractFromBudget(int amountToSubtractFromBudget) { - if (amountToSubtractFromBudget < 0){ - throw new IllegalArgumentException("Amount to be subtracted from the budget cant be below zero"); - } - budgetAmount -= amountToSubtractFromBudget; - } - -} diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudget.java b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudget.java new file mode 100644 index 0000000000000000000000000000000000000000..b1b86156769c8a25b16aa4af1dc3247771e97350 --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudget.java @@ -0,0 +1,99 @@ +package no.ntnu.idatt1002.demo.data.Budget; + +import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory; + +import java.sql.Date; +import java.time.Duration; +import java.time.LocalDate; +import java.time.ZoneId; +import java.time.temporal.ChronoUnit; +import java.time.temporal.Temporal; +import java.util.Calendar; +import java.util.List; + +/** + * Class that represents GeneralBudget and implements the Budget interface. + * + * @author Adele + */ +public class GeneralBudget { + + private int budgetPeriod; + private List<BudgetItem> listOfItems; + private double maxAmount; + + /** + * The constructor of a new GeneralBudget. + * + * @param budgetPeriod The period the budget are going to last for + * + */ + public GeneralBudget(int budgetPeriod, List<BudgetItem> listOfItems, double maxAmount){ + this.budgetPeriod = budgetPeriod; + this.maxAmount = maxAmount; + this.listOfItems = listOfItems; + + } + + /** + * This method returns the amount of days the budget should last for. + * + * @return amount of days the budget lasts + * + */ + public int getBudgetPeriod() { + return budgetPeriod; + } + + /** + * This method returns the amount of days left in the period as a string. + * + * @return the amount of days left in the budget period + * + */ + public long getDaysLeftOfBudgetPeriod() { + LocalDate today = LocalDate.now(); + LocalDate end = today.plus(Duration.ofDays(getBudgetPeriod())); + + Date todaysDate = (Date) Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant()); + Date endDate = (Date) Date.from(end.atStartOfDay(ZoneId.systemDefault()).toInstant()); + + Calendar cStart = Calendar.getInstance(); cStart.setTime(todaysDate); + Calendar cEnd = Calendar.getInstance(); cEnd.setTime(endDate); + + return ChronoUnit.DAYS.between((Temporal) cStart, (Temporal) cEnd); + } + + public void addToBudget(double budgetAmount, String description, ExpenseCategory category) { + if (totalSum() + budgetAmount > maxAmount ){ + throw new IllegalArgumentException("Amount to be added goes over the max value of the budget"); + } + + if (!checksListOfItemsContainsBudgetItem(category)) { + listOfItems.add(new BudgetItem(budgetAmount, description, category)); + } else { + throw new IllegalArgumentException("There is already a budget with this category"); + } + } + + public boolean checksListOfItemsContainsBudgetItem(ExpenseCategory category){ + for (BudgetItem item : listOfItems) { + if (item.getCategory() == category) { + return true; + } + } + return false; + } + + public double totalSum(){ + double sum = 0; + for (BudgetItem budgetItem : listOfItems) { + sum += budgetItem.getBudgetAmount(); + } + return sum; + } + + public void deleteItemFromBudget(ExpenseCategory category){ + listOfItems.removeIf(item -> item.getCategory() == category); + } +}