From 305f22e80103ae5f0cbcf34f31c6a575dfb4698d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adele=20Iren=20Westrum=20Kj=C3=B8lstad?= <aikjolst@stud.ntnu.no> Date: Wed, 15 Mar 2023 12:36:00 +0100 Subject: [PATCH] Created and implemented FoodBudget --- .../demo/data/Budget/FoodBudget.java | 128 ++++++++++++++++++ .../demo/data/Budget/FoodBudgetTest.java | 85 ++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 src/main/java/no/ntnu/idatt1002/demo/data/Budget/FoodBudget.java create mode 100644 src/test/java/no/ntnu/idatt1002/demo/data/Budget/FoodBudgetTest.java 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 new file mode 100644 index 00000000..58b34823 --- /dev/null +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FoodBudget.java @@ -0,0 +1,128 @@ +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/test/java/no/ntnu/idatt1002/demo/data/Budget/FoodBudgetTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Budget/FoodBudgetTest.java new file mode 100644 index 00000000..22503bb9 --- /dev/null +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Budget/FoodBudgetTest.java @@ -0,0 +1,85 @@ +package no.ntnu.idatt1002.demo.data.Budget; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class FoodBudgetTest { + + @Test + void set_amount_budget() { + FoodBudget foodBudget = new FoodBudget(30, 0); + int amount = 2000; + foodBudget.setAmountBudget(amount); + + assertEquals(amount, foodBudget.getBudgetAmount()); + } + + @Test + void set_amount_budget_negativ_amount() { + FoodBudget foodBudget = new FoodBudget(30, 0); + int amount = -2000; + + assertThrows(IllegalArgumentException.class, () -> foodBudget.setAmountBudget(amount)); + } + + @Test + void set_budget_period() { + FoodBudget foodBudget = new FoodBudget(0, 30); + int period = 30; + foodBudget.setBudgetPeriod(period); + + assertEquals(period,foodBudget.getBudgetPeriod()); + } + + @Test + void set_budget_period_negative_period() { + FoodBudget foodBudget = new FoodBudget(0, 30); + int period = -5; + + assertThrows(IllegalArgumentException.class, () -> foodBudget.setBudgetPeriod(period)); + } + + @Test + void add_to_budget_positive_number() { + FoodBudget foodBudget = new FoodBudget(0, 30); + int beforeAdding = foodBudget.getBudgetAmount(); + int amountToBeAdded = 50; + foodBudget.addToBudget(amountToBeAdded); + + assertEquals(beforeAdding + amountToBeAdded, foodBudget.getBudgetAmount()); + + } + + @Test + void add_to_budget_negative_number() { + FoodBudget foodBudget = new FoodBudget(0, 30); + int amountToBeAdded = -7; + + assertThrows(IllegalArgumentException.class, () -> foodBudget.addToBudget(amountToBeAdded)); + } + + @Test + void subtract_from_budget_positive_number() { + FoodBudget foodBudget = new FoodBudget(0, 30); + int beforeSubtracting = foodBudget.getBudgetAmount(); + int amountToBeSubtracted = 50; + foodBudget.subtractFromBudget(amountToBeSubtracted); + + assertEquals(beforeSubtracting - amountToBeSubtracted, foodBudget.getBudgetAmount()); + } + + @Test + void subtract_from_budget_negative_number() { + FoodBudget foodBudget = new FoodBudget(0, 30); + int amountToBeSubtracted = -7; + + assertThrows(IllegalArgumentException.class, () -> foodBudget.subtractFromBudget(amountToBeSubtracted)); + } + + @Test + void getDaysLeftOfBudgetPeriod(){ + + } + +} \ No newline at end of file -- GitLab