Skip to content

Commit

Permalink
Merge pull request #372 from EkChinHui/branch-fix-photo
Browse files Browse the repository at this point in the history
Branch fix constraints bug
  • Loading branch information
EkChinHui authored Nov 9, 2020
2 parents 0d71454 + 1824405 commit 1b942cf
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.MainPageCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.budget.Budget;

Expand Down Expand Up @@ -43,7 +44,7 @@ public CreateBudgetCommand(Budget toCreate) {
* @return the commmand result along with a success message
*/
@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model) throws CommandException {
model.saveToHistory();
model.addBudget(toCreate);
return new CommandResult(String.format(MESSAGE_SUCCESS, toCreate));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public interface Model {

void closeBudget();

void addBudget(Budget budget);
void addBudget(Budget budget) throws CommandException;

void editBudget(Budget toEdit, Budget editedBudget);

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class ModelManager implements Model {
private final History<VersionedNusave> history;

/**
* Initializes a ModelManager with the given addressBook and userPrefs.
* Initializes a ModelManager with the given nusave and userPrefs.
*/
public ModelManager(ReadOnlyNusave nusave, ReadOnlyUserPrefs userPrefs) {
super();
Expand Down Expand Up @@ -176,8 +176,11 @@ private BudgetIndex getActualBudgetIndex(BudgetIndex budgetIndex) {
* @param budget the budget to be added
*/
@Override
public void addBudget(Budget budget) {
public void addBudget(Budget budget) throws CommandException {
requireNonNull(budget);
if (nusave.isBudgetOverLimit()) {
throw new CommandException("You are not allowed to have more than 100 budgets");
}
nusave.addBudgetToFront(budget);
displayAllRenderables();
}
Expand Down Expand Up @@ -299,9 +302,12 @@ public void deleteExpenditure(ExpenditureIndex expenditureIndex) {
* @param expenditure the expenditure to be added
*/
@Override
public void addExpenditure(Expenditure expenditure) {
public void addExpenditure(Expenditure expenditure) throws CommandException {
requireNonNull(expenditure);
Optional<Integer> budgetIndex = this.state.getBudgetIndex();
if (nusave.isExpenditureOverLimit(budgetIndex)) {
throw new CommandException("You are not allowed to have more than 100 expenditures");
}
nusave.addExpenditure(expenditure, this.state.getBudgetIndex());
setTotalExpenditure(nusave.getTotalExpenditureValue(budgetIndex));
displayAllRenderables();
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/model/Nusave.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public int getIndexOfBudget(Budget budget) {
return budgetList.getIndexOfBudget(budget);
}

public boolean isBudgetOverLimit() {
return budgetList.getSize() > 100;
}

//=========== Expenditure ==================================================================================
/**
* Adds a expenditure to the NUSave budget according to its index.
Expand Down Expand Up @@ -165,6 +169,18 @@ public String getTotalExpenditureValue(Optional<Integer> budgetIndexOpt) {
return this.budgetList.getTotalExpenditureValue(budgetIndex);
}

/**
*
* @param budgetIndexOpt
* @return
*/
public boolean isExpenditureOverLimit(Optional<Integer> budgetIndexOpt) {
assert budgetIndexOpt.isPresent();
int budgetIndex = budgetIndexOpt.get();
int expenditureSize = budgetList.getExpenditures(budgetIndex).getExpenditureSize();
return expenditureSize > 99;
}

//=========== ObservableList ==================================================================================
/**
* Sets the observable list displayed in UI based on the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.testutil.TypicalBudget;
Expand All @@ -19,7 +20,7 @@ public void execute_emptyNusave_success() {
}

@Test
public void execute_nonEmptyNusave_success() {
public void execute_nonEmptyNusave_success() throws CommandException {
Model model = new ModelManager();
model.addBudget(TypicalBudget.getMcDonaldsBudget());
Model expectedModel = new ModelManager();
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/seedu/address/model/ModelManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void listBudget_noBudgets() throws CommandException {
}

@Test
void sortBudgetsByName() {
void sortBudgetsByName() throws CommandException {
ModelManager modelManager = new ModelManager(getTypicalNusave(), new UserPrefs());

modelManager.addBudget(getSubwayBudget());
Expand All @@ -240,7 +240,7 @@ void sortBudgetsByName() {
}

@Test
void sortBudgetsByCreatedDate() {
void sortBudgetsByCreatedDate() throws CommandException {
ModelManager modelManager = new ModelManager(getTypicalNusave(), new UserPrefs());

modelManager.addBudget(getMcDonaldsBudget());
Expand All @@ -263,7 +263,7 @@ void deleteExpenditure() {
}

@Test
void addExpenditure() {
void addExpenditure() throws CommandException {
ModelManager modelManager = new ModelManager(getTypicalNusave(), new UserPrefs());
modelManager.openBudget(new BudgetIndexManager(0));
modelManager.addExpenditure(getMcMuffinExpenditure());
Expand Down

0 comments on commit 1b942cf

Please sign in to comment.