From 1faf49c86f09ca05197e20babf8144b08a553e4f Mon Sep 17 00:00:00 2001 From: EkChinHui <57344608+EkChinHui@users.noreply.github.com> Date: Mon, 9 Nov 2020 23:47:47 +0800 Subject: [PATCH 1/2] Fix bug --- .../logic/commands/main/CreateBudgetCommand.java | 3 ++- src/main/java/seedu/address/model/Model.java | 2 +- src/main/java/seedu/address/model/ModelManager.java | 12 +++++++++--- src/main/java/seedu/address/model/Nusave.java | 11 +++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/main/CreateBudgetCommand.java b/src/main/java/seedu/address/logic/commands/main/CreateBudgetCommand.java index 4fd0b8252a6..1f04924b8b8 100644 --- a/src/main/java/seedu/address/logic/commands/main/CreateBudgetCommand.java +++ b/src/main/java/seedu/address/logic/commands/main/CreateBudgetCommand.java @@ -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; @@ -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)); diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 148a3340f3e..50094260871 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -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); diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index d1955359ae4..c218adb8634 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -48,7 +48,7 @@ public class ModelManager implements Model { private final History 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(); @@ -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(); } @@ -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 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(); diff --git a/src/main/java/seedu/address/model/Nusave.java b/src/main/java/seedu/address/model/Nusave.java index ba90ecc0117..473b0a0e5d1 100644 --- a/src/main/java/seedu/address/model/Nusave.java +++ b/src/main/java/seedu/address/model/Nusave.java @@ -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. @@ -165,6 +169,13 @@ public String getTotalExpenditureValue(Optional budgetIndexOpt) { return this.budgetList.getTotalExpenditureValue(budgetIndex); } + public boolean isExpenditureOverLimit(Optional 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 From 182440549e7d8923da5f91c4ee083598c8a526da Mon Sep 17 00:00:00 2001 From: EkChinHui <57344608+EkChinHui@users.noreply.github.com> Date: Mon, 9 Nov 2020 23:51:57 +0800 Subject: [PATCH 2/2] Fix checkstyle --- src/main/java/seedu/address/model/Nusave.java | 5 +++++ .../logic/commands/main/ClearBudgetsCommandTest.java | 3 ++- src/test/java/seedu/address/model/ModelManagerTest.java | 6 +++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/seedu/address/model/Nusave.java b/src/main/java/seedu/address/model/Nusave.java index 473b0a0e5d1..b7cc4f6d27f 100644 --- a/src/main/java/seedu/address/model/Nusave.java +++ b/src/main/java/seedu/address/model/Nusave.java @@ -169,6 +169,11 @@ public String getTotalExpenditureValue(Optional budgetIndexOpt) { return this.budgetList.getTotalExpenditureValue(budgetIndex); } + /** + * + * @param budgetIndexOpt + * @return + */ public boolean isExpenditureOverLimit(Optional budgetIndexOpt) { assert budgetIndexOpt.isPresent(); int budgetIndex = budgetIndexOpt.get(); diff --git a/src/test/java/seedu/address/logic/commands/main/ClearBudgetsCommandTest.java b/src/test/java/seedu/address/logic/commands/main/ClearBudgetsCommandTest.java index 8904f161dd9..443a6c56892 100644 --- a/src/test/java/seedu/address/logic/commands/main/ClearBudgetsCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/main/ClearBudgetsCommandTest.java @@ -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; @@ -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(); diff --git a/src/test/java/seedu/address/model/ModelManagerTest.java b/src/test/java/seedu/address/model/ModelManagerTest.java index 7ab699c99c8..1794b25b4c8 100644 --- a/src/test/java/seedu/address/model/ModelManagerTest.java +++ b/src/test/java/seedu/address/model/ModelManagerTest.java @@ -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()); @@ -240,7 +240,7 @@ void sortBudgetsByName() { } @Test - void sortBudgetsByCreatedDate() { + void sortBudgetsByCreatedDate() throws CommandException { ModelManager modelManager = new ModelManager(getTypicalNusave(), new UserPrefs()); modelManager.addBudget(getMcDonaldsBudget()); @@ -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());