Skip to content
Snippets Groups Projects
Commit b926b868 authored by Andreas Kluge Svendsrud's avatar Andreas Kluge Svendsrud
Browse files

Merge branch 'budget' into 'master'

Budget

See merge request !11
parents 2b9b9af0 d02e6617
No related branches found
No related tags found
1 merge request!11Budget
Pipeline #211427 passed
package no.ntnu.idatt1002.demo.data.Budget;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
/**
* This class represents a budgetItem
*
* @author Adele
*/
public class BudgetItem {
private double budgetAmount;
private ExpenseCategory category;
private String description;
/**
* The constructor of a new Budgetitem.
*
* @param budgetAmount The amount of budget as a double
* @param description A description of the budget as a String
* @param category A category from ExpenseCategory
*
*/
public BudgetItem(double budgetAmount, String description, ExpenseCategory category){
this.budgetAmount = budgetAmount;
this.description = description;
this.category = category;
}
/**
* This method gets the BudgetAmount.
*
* @return the budgetAmount as a double
*
*/
public double getBudgetAmount() {
return budgetAmount;
}
/**
* This method gets the category.
*
* @return the category as one of the categories in ExpenseCategory
*
*/
public ExpenseCategory getCategory() {
return category;
}
/**
* This method gets the description.
*
* @return the description as a String
*
*/
public String getDescription() {
return description;
}
}
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.
*
* @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
* @param listOfItems A list of BudgetItems
* @param maxAmount The maxAmount of the generalBudget as a double
*
*/
public GeneralBudget(int budgetPeriod, List<BudgetItem> listOfItems, double maxAmount){
if (budgetPeriod < 0) {
throw new IllegalArgumentException("The period cant be under zero days");
}
if (maxAmount < 0) {
throw new IllegalArgumentException("The maxamount of the budget cant be under zero");
}
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);
}
/**
* This method adds a budgetItem to the list of budgetItems in the generalBudget
*
* @param budgetAmount The amount of budget as a double
* @param description A description of the budget as a String
* @param category A category from ExpenseCategory
*
*/
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");
}
}
/**
* This method checks if the list in the generalBudget already contains a budgetItem with a specified category.
*
* @param category A category from ExpenseCategory
*
* @return True if the list contains a budgetItem with this category and false if it does not
*
*/
public boolean checksListOfItemsContainsBudgetItem(ExpenseCategory category){
for (BudgetItem item : listOfItems) {
if (item.getCategory() == category) {
return true;
}
}
return false;
}
/**
* This method returns the totalSum of all budgetsItems in the list in the generalBudget
*
* @return the sum of the budgetsItems as a double
*
*/
public double totalSum(){
double sum = 0;
for (BudgetItem budgetItem : listOfItems) {
sum += budgetItem.getBudgetAmount();
}
return sum;
}
/**
* This method deletes a budgetItem from the list in the GeneralBudget.
*
* @param category A category from ExpenseCategory
*
*/
public void deleteItemFromBudget(ExpenseCategory category){
listOfItems.removeIf(item -> item.getCategory() == category);
}
}
package no.ntnu.idatt1002.demo.data.Budget;
import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class GeneralBudgetTest {
@Test
@DisplayName("Constructor throws exception when the periodAmount is under zero")
void constructor_throws_exception_when_periodAmount_under_zero(){
List<BudgetItem> list = new ArrayList<>();
assertThrows(IllegalArgumentException.class, () -> new GeneralBudget(-1, list, 1200));
}
@Test
@DisplayName("Constructor throws exception when maxAmount is under zero")
void constructor_throws_exception_when_maxAmount_under_zero(){
List<BudgetItem> list = new ArrayList<>();
assertThrows(IllegalArgumentException.class, () -> new GeneralBudget(10, list, -12));
}
@Test
@DisplayName("AddToBudget throws when totalSum is higher than maxAmount ")
void add_to_budget_throws_when_totalSum_is_over_maxAmount(){
List<BudgetItem> list = new ArrayList<>();
GeneralBudget budget1 = new GeneralBudget(12, list, 1200);
assertThrows(IllegalArgumentException.class, () -> budget1.addToBudget(1300, "Food", ExpenseCategory.FOOD));
}
@Test
@DisplayName("addToBudget throws when a budget with same category already exists")
void add_to_budget_throws_when_a_budget_with_same_category_already_exists(){
List<BudgetItem> list = new ArrayList<>();
GeneralBudget budget1 = new GeneralBudget(12, list, 1200);
budget1.addToBudget(500, "Food", ExpenseCategory.FOOD);
assertThrows(IllegalArgumentException.class, () -> budget1.addToBudget(200, "Food", ExpenseCategory.FOOD));
}
@Test
@DisplayName("Adds a budget to generalBudget")
void add_budget_to_generalBudget(){
List<BudgetItem> list = new ArrayList<>();
GeneralBudget budget1 = new GeneralBudget(12, list, 1200);
BudgetItem item1 = new BudgetItem(500, "Food", ExpenseCategory.FOOD);
budget1.addToBudget(500, "Food", ExpenseCategory.FOOD);
assertEquals(1, list.size());
}
@Test
@DisplayName("Checks if the list contains a item with a given category")
void checks_if_the_list_contains_an_item_with_this_category_true(){
List<BudgetItem> list = new ArrayList<>();
GeneralBudget budget1 = new GeneralBudget(12, list, 1200);
budget1.addToBudget(500, "Food", ExpenseCategory.FOOD);
assertTrue(budget1.checksListOfItemsContainsBudgetItem(ExpenseCategory.FOOD));
}
@Test
@DisplayName("checks that the list does not contain an item with the given category")
void checks_if_the_list_contains_an_item_with_this_category_false(){
List<BudgetItem> list = new ArrayList<>();
GeneralBudget budget1 = new GeneralBudget(12, list, 1200);
budget1.addToBudget(500, "Books", ExpenseCategory.BOOKS);
assertFalse(budget1.checksListOfItemsContainsBudgetItem(ExpenseCategory.FOOD));
}
@Test
@DisplayName("Checks that the getTotalSum gives the correct number")
void get_total_sum_of_all_budgetItems_in_the_budget(){
List<BudgetItem> list = new ArrayList<>();
GeneralBudget budget1 = new GeneralBudget(12, list, 1200);
budget1.addToBudget(500, "Books", ExpenseCategory.BOOKS);
budget1.addToBudget(300, "Food", ExpenseCategory.FOOD);
assertEquals(800, budget1.totalSum());
}
@Test
@DisplayName("Checks that an item gets deleted from the budget")
void delete_from_budget(){
List<BudgetItem> list = new ArrayList<>();
GeneralBudget budget1 = new GeneralBudget(12, list, 1200);
budget1.addToBudget(500, "Books", ExpenseCategory.BOOKS);
budget1.deleteItemFromBudget(ExpenseCategory.BOOKS);
assertTrue(list.isEmpty());
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment