Skip to content
Snippets Groups Projects
Commit 77724320 authored by Adele Iren Westrum Kjølstad's avatar Adele Iren Westrum Kjølstad
Browse files

Refactored budget classes for easier reuse

parent 305f22e8
No related branches found
No related tags found
1 merge request!11Budget
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);
}
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;
}
}
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;
......@@ -7,43 +9,36 @@ 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 FoodBudget and implements the Budget interface.
* Class that represents GeneralBudget and implements the Budget interface.
*
* @author Adele
*/
public class FoodBudget implements Budget {
private int budgetAmount;
public class GeneralBudget {
private int budgetPeriod;
private List<BudgetItem> listOfItems;
private double maxAmount;
/**
* The constructor of a new FoodBudget.
* The constructor of a new GeneralBudget.
*
* @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){
public GeneralBudget(int budgetPeriod, List<BudgetItem> listOfItems, double maxAmount){
this.budgetPeriod = budgetPeriod;
this.budgetAmount = budgetAmount;
}
this.maxAmount = maxAmount;
this.listOfItems = listOfItems;
/**
* This method returns the amount in the foodBudget as an int
*
* @return the amount left in the foodBudget
*
*/
public int getBudgetAmount() {
return budgetAmount;
}
/**
* This method returns the amount of days the budget should last for.
*
* @return
* @return amount of days the budget lasts
*
*/
public int getBudgetPeriod() {
......@@ -51,9 +46,9 @@ public class FoodBudget implements Budget {
}
/**
* This method returns the amount of days left in the period as a string
* This method returns the amount of days left in the period as a string.
*
* @return
* @return the amount of days left in the budget period
*
*/
public long getDaysLeftOfBudgetPeriod() {
......@@ -66,63 +61,39 @@ public class FoodBudget implements Budget {
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;
return ChronoUnit.DAYS.between((Temporal) cStart, (Temporal) cEnd);
}
/**
*
* @param amountBudget
*
*/
@Override
public void setAmountBudget(int amountBudget) {
if (amountBudget < 0) {
throw new IllegalArgumentException("Illegal amount in budget");
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");
}
budgetAmount = amountBudget;
}
/**
*
* @param periodOfTheBudget
*
*/
@Override
public void setBudgetPeriod(int periodOfTheBudget) {
if (periodOfTheBudget < 0) {
throw new IllegalArgumentException("Illegal period for the budget");
if (!checksListOfItemsContainsBudgetItem(category)) {
listOfItems.add(new BudgetItem(budgetAmount, description, category));
} else {
throw new IllegalArgumentException("There is already a budget with this category");
}
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");
public boolean checksListOfItemsContainsBudgetItem(ExpenseCategory category){
for (BudgetItem item : listOfItems) {
if (item.getCategory() == category) {
return true;
}
}
budgetAmount += amountToAddToBudget;
return false;
}
/**
*
* @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");
public double totalSum(){
double sum = 0;
for (BudgetItem budgetItem : listOfItems) {
sum += budgetItem.getBudgetAmount();
}
budgetAmount -= amountToSubtractFromBudget;
return sum;
}
public void deleteItemFromBudget(ExpenseCategory category){
listOfItems.removeIf(item -> item.getCategory() == category);
}
}
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