From 8dc10cc1144ec0cbcc529c15ad349c069d32e501 Mon Sep 17 00:00:00 2001 From: yyutong <1037662939@qq.com> Date: Wed, 14 Oct 2020 13:49:16 +0800 Subject: [PATCH 1/4] Add ViewCategory command and parser --- .../logic/commands/ViewCategoryCommand.java | 39 ++++++++++- .../logic/parser/ExpenseBookParser.java | 64 +++++++++---------- .../parser/ViewCategoryCommandParser.java | 18 +++++- .../java/seedu/address/model/ExpenseBook.java | 5 ++ .../address/model/ExpenseModelManager.java | 18 +++--- src/main/java/seedu/address/model/Model.java | 4 ++ .../seedu/address/model/ModelManager.java | 8 ++- .../seedu/address/model/person/Expense.java | 12 ++-- .../model/person/UniqueExpenseList.java | 13 ++++ 9 files changed, 129 insertions(+), 52 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/ViewCategoryCommand.java b/src/main/java/seedu/address/logic/commands/ViewCategoryCommand.java index a743f63481d..9d3a9e6ea72 100644 --- a/src/main/java/seedu/address/logic/commands/ViewCategoryCommand.java +++ b/src/main/java/seedu/address/logic/commands/ViewCategoryCommand.java @@ -1,4 +1,41 @@ package seedu.address.logic.commands; -public class ViewCategoryCommand { +import seedu.address.commons.core.Messages; +import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.Model; +import seedu.address.model.person.Category; +import seedu.address.model.person.Description; +import seedu.address.model.person.Expense; + +import java.util.List; + +import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; +import static seedu.address.model.Model.PREDICATE_SHOW_ALL_EXPENSES; + +public class ViewCategoryCommand extends Command { + + public static final String COMMAND_WORD = "viewCategory"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Finds all the category labels used in the ExpenseBokk so far. \n" + + "Example: " + COMMAND_WORD ; + + public static final String MESSAGE_VIEW_CATEGORY_LABELS_SUCCESS = + "View all the existing category labels: \n"; + + public ViewCategoryCommand() { + } + + @Override + public CommandResult execute(Model model) throws CommandException { + List categories = model.getCategoryLabels(); + String message = ""; + for(int i = 0; i < categories.size()-1; i++){ + message = message + categories.get(i).toString() + "\n"; + } + return new CommandResult(MESSAGE_VIEW_CATEGORY_LABELS_SUCCESS + message); + } + } diff --git a/src/main/java/seedu/address/logic/parser/ExpenseBookParser.java b/src/main/java/seedu/address/logic/parser/ExpenseBookParser.java index 7e1ae3c3bc4..b681df2c6c3 100644 --- a/src/main/java/seedu/address/logic/parser/ExpenseBookParser.java +++ b/src/main/java/seedu/address/logic/parser/ExpenseBookParser.java @@ -1,23 +1,13 @@ package seedu.address.logic.parser; -import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; -import static seedu.address.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND; +import seedu.address.logic.commands.*; +import seedu.address.logic.parser.exceptions.ParseException; import java.util.regex.Matcher; import java.util.regex.Pattern; -import seedu.address.logic.commands.AddExpenseCommand; -import seedu.address.logic.commands.Command; -import seedu.address.logic.commands.DeleteExpenseCommand; -import seedu.address.logic.commands.DeleteDescriptionCommand; -import seedu.address.logic.commands.DescriptionCommand; -import seedu.address.logic.commands.ListExpenseCommand; -import seedu.address.logic.commands.HelpCommand; -import seedu.address.logic.commands.ViewCommand; -import seedu.address.logic.commands.SetBudgetCommand; -import seedu.address.logic.commands.ShowBudgetCommand; - -import seedu.address.logic.parser.exceptions.ParseException; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND; /** * Parses user input. @@ -44,31 +34,35 @@ public Command parseCommand(String userInput) throws ParseException { final String commandWord = matcher.group("commandWord"); final String arguments = matcher.group("arguments"); switch (commandWord) { - case DescriptionCommand.COMMAND_WORD: - return new DescriptionCommandParser().parse(arguments); + case DescriptionCommand.COMMAND_WORD: + return new DescriptionCommandParser().parse(arguments); + + case AddExpenseCommand.COMMAND_WORD: + return new AddExpenseCommandParser().parse(arguments); + + case DeleteExpenseCommand.COMMAND_WORD: + return new DeleteExpenseCommandParser().parse(arguments); + + case ListExpenseCommand.COMMAND_WORD: + return new ListExpenseCommand(); + + case ViewCommand.COMMAND_WORD: + return new ViewCommandParser().parse(arguments); + + case ViewCategoryCommand.COMMAND_WORD: + return new ViewCategoryCommandParser().parse(arguments); - case AddExpenseCommand.COMMAND_WORD: - return new AddExpenseCommandParser().parse(arguments); + case DeleteDescriptionCommand.COMMAND_WORD: + return new DeleteDescriptionCommandParser().parse(arguments); - case DeleteExpenseCommand.COMMAND_WORD: - return new DeleteExpenseCommandParser().parse(arguments); - case ListExpenseCommand.COMMAND_WORD: - return new ListExpenseCommand(); + case ShowBudgetCommand.COMMAND_WORD: + return new ShowBudgetCommandParser().parse(arguments); - case ViewCommand.COMMAND_WORD: - return new ViewCommandParser().parse(arguments); - - case DeleteDescriptionCommand.COMMAND_WORD: - return new DeleteDescriptionCommandParser().parse(arguments); - - case ShowBudgetCommand.COMMAND_WORD: - return new ShowBudgetCommandParser().parse(arguments); - - case SetBudgetCommand.COMMAND_WORD: - return new SetBudgetCommandParser().parse(arguments); + case SetBudgetCommand.COMMAND_WORD: + return new SetBudgetCommandParser().parse(arguments); - default: - throw new ParseException(MESSAGE_UNKNOWN_COMMAND); + default: + throw new ParseException(MESSAGE_UNKNOWN_COMMAND); } } } diff --git a/src/main/java/seedu/address/logic/parser/ViewCategoryCommandParser.java b/src/main/java/seedu/address/logic/parser/ViewCategoryCommandParser.java index f4284a41ec3..b48e069d65a 100644 --- a/src/main/java/seedu/address/logic/parser/ViewCategoryCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/ViewCategoryCommandParser.java @@ -1,4 +1,20 @@ package seedu.address.logic.parser; -public class ViewCategoryCommandParser { +import seedu.address.commons.core.index.Index; +import seedu.address.commons.exceptions.IllegalValueException; +import seedu.address.logic.commands.DescriptionCommand; +import seedu.address.logic.commands.ShowBudgetCommand; +import seedu.address.logic.commands.ViewCategoryCommand; +import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.person.Description; + +import static java.util.Objects.requireNonNull; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.logic.parser.CliSyntax.PREFIX_CATEGORY; + +public class ViewCategoryCommandParser implements Parser{ + @Override + public ViewCategoryCommand parse(String args) throws ParseException { + return new ViewCategoryCommand(); + } } diff --git a/src/main/java/seedu/address/model/ExpenseBook.java b/src/main/java/seedu/address/model/ExpenseBook.java index 541b70e14b0..94098178a0f 100644 --- a/src/main/java/seedu/address/model/ExpenseBook.java +++ b/src/main/java/seedu/address/model/ExpenseBook.java @@ -6,6 +6,7 @@ import javafx.collections.ObservableList; import seedu.address.commons.core.index.Index; +import seedu.address.model.person.Category; import seedu.address.model.person.Expense; import seedu.address.model.person.UniqueExpenseList; @@ -145,4 +146,8 @@ public double getRemainingBudget() { } //end of yuanxing edits + public List getCategoryLabels(){ + return expenses.getCategoryLabels(); + } + } diff --git a/src/main/java/seedu/address/model/ExpenseModelManager.java b/src/main/java/seedu/address/model/ExpenseModelManager.java index a3da01e4a05..8216d0aa538 100644 --- a/src/main/java/seedu/address/model/ExpenseModelManager.java +++ b/src/main/java/seedu/address/model/ExpenseModelManager.java @@ -4,6 +4,7 @@ import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; import java.nio.file.Path; +import java.util.List; import java.util.function.Predicate; import java.util.logging.Logger; @@ -12,6 +13,7 @@ import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.LogsCenter; import seedu.address.commons.core.index.Index; +import seedu.address.model.person.Category; import seedu.address.model.person.Expense; import seedu.address.model.person.Person; @@ -26,6 +28,7 @@ public class ExpenseModelManager implements Model { private final FilteredList filteredExpenses; + /** * Initializes a ModelManager with the given addressBook and userPrefs. */ @@ -120,16 +123,6 @@ public void setExpense(Expense target, Expense editedExpense) { expenseBook.setExpense(target, editedExpense); } - /** - * View the detals of a certain expense. - * - * @param index The index of the expense to be viewed in the ExpenseBook. - */ - @Override - public void viewExpense(Index index) { - expenseBook.viewExpense(index); - } - //=========== Filtered Expense List Accessors ============================================================= /** @@ -192,5 +185,10 @@ public void setExpenseBookBudget(double budget) { expenseBook.setBudget(budget); } + @Override + public List getCategoryLabels() { + return expenseBook.getCategoryLabels(); + } + } diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 93c1686d244..07b433c0a80 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -1,11 +1,13 @@ package seedu.address.model; import java.nio.file.Path; +import java.util.List; import java.util.function.Predicate; import javafx.collections.ObservableList; import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.index.Index; +import seedu.address.model.person.Category; import seedu.address.model.person.Expense; import seedu.address.model.person.Person; @@ -102,4 +104,6 @@ public interface Model { double getExpenseBookRemaining(); void setExpenseBookBudget(double budget); + + List getCategoryLabels(); } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 91765c188c9..7fd51da3aec 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -4,6 +4,7 @@ import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; import java.nio.file.Path; +import java.util.List; import java.util.function.Predicate; import java.util.logging.Logger; @@ -12,6 +13,7 @@ import seedu.address.commons.core.GuiSettings; import seedu.address.commons.core.LogsCenter; import seedu.address.commons.core.index.Index; +import seedu.address.model.person.Category; import seedu.address.model.person.Expense; import seedu.address.model.person.Person; @@ -28,7 +30,6 @@ public class ModelManager implements Model { private final FilteredList filteredExpenses; // private final ExpenseBook expenseBook; - private Expense expenseToBeViewed; /** * Initializes a ModelManager with the given addressBook and userPrefs. @@ -201,6 +202,11 @@ public void setExpenseBookBudget(double budget) { } + @Override + public List getCategoryLabels() { + return null; + } + @Override public boolean equals(Object obj) { // short circuit if same object diff --git a/src/main/java/seedu/address/model/person/Expense.java b/src/main/java/seedu/address/model/person/Expense.java index e04f17e0ea4..faa72a2189f 100644 --- a/src/main/java/seedu/address/model/person/Expense.java +++ b/src/main/java/seedu/address/model/person/Expense.java @@ -89,23 +89,27 @@ public String toString() { if (description.isEmpty()) { builder.append(" Amount: ") .append(this.getAmount()) + .append("\n") .append(" Date: ") .append(this.getDate()) + .append("\n") .append(" Category: ") - .append(this.getCategory()); + .append(this.getCategory()) + .append("\n"); return builder.toString(); } else { builder.append(" Amount: ") .append(this.getAmount()) - .append("/n") + .append("\n") .append(" Date: ") .append(this.getDate()) + .append("\n") .append(" Category: ") - .append("/n") .append(this.getCategory()) + .append("\n") .append(" Description: ") .append(this.getDescription()) - .append("/n"); + .append("\n"); return builder.toString(); } } diff --git a/src/main/java/seedu/address/model/person/UniqueExpenseList.java b/src/main/java/seedu/address/model/person/UniqueExpenseList.java index 23a7ae4ff52..933c5de8eb1 100644 --- a/src/main/java/seedu/address/model/person/UniqueExpenseList.java +++ b/src/main/java/seedu/address/model/person/UniqueExpenseList.java @@ -4,6 +4,7 @@ import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -164,4 +165,16 @@ public double getRemainingBudget() { } return this.budget - used; } + + public List getCategoryLabels(){ + List categories = new ArrayList<>(); + for(int i = 0; i < internalList.size() -1; i++) { + Expense current = internalList.get(i); + Category currentCategory = current.getCategory(); + if(!categories.contains(currentCategory)){ + categories.add(currentCategory); + } + } + return categories; + } } From f6c23e16c35eb5a110c611129073702dc666eb56 Mon Sep 17 00:00:00 2001 From: yyutong <1037662939@qq.com> Date: Wed, 14 Oct 2020 13:54:55 +0800 Subject: [PATCH 2/4] Add ViewCategory command --- src/main/java/seedu/address/logic/commands/ViewCommand.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/ViewCommand.java b/src/main/java/seedu/address/logic/commands/ViewCommand.java index 666c2286a6a..a7c91870a4a 100644 --- a/src/main/java/seedu/address/logic/commands/ViewCommand.java +++ b/src/main/java/seedu/address/logic/commands/ViewCommand.java @@ -37,9 +37,9 @@ public CommandResult execute(Model model) throws CommandException { } Expense expenseToView = lastShownList.get(targetIndex.getZeroBased()); - String message = targetIndex.toString() + "\n" + expenseToView.toString(); -// model.viewExpense(targetIndex); - return new CommandResult(String.format(MESSAGE_VIEW_EXPENSE_SUCCESS, message)); + String prefix = String.format(MESSAGE_VIEW_EXPENSE_SUCCESS, targetIndex.getOneBased()); + String message = prefix + "\n" + expenseToView.toString(); + return new CommandResult(message); } @Override From 1fb550bf0abe1709689318dea2ae33a1c3bfb80f Mon Sep 17 00:00:00 2001 From: yyutong <1037662939@qq.com> Date: Wed, 14 Oct 2020 18:01:11 +0800 Subject: [PATCH 3/4] Add viewCategory command --- src/main/java/seedu/address/ExpenseMainApp.java | 3 ++- .../logic/commands/DeleteDescriptionCommand.java | 10 ++++++---- .../logic/commands/ViewCategoryCommand.java | 16 +++++----------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/main/java/seedu/address/ExpenseMainApp.java b/src/main/java/seedu/address/ExpenseMainApp.java index eeb6f8e3623..51d8e0100ad 100644 --- a/src/main/java/seedu/address/ExpenseMainApp.java +++ b/src/main/java/seedu/address/ExpenseMainApp.java @@ -69,7 +69,8 @@ public void init() throws Exception { } /** - * Returns a {@code ExpenseModelManager} with the data from {@code storage}'s expense book and {@code userPrefs}.
+ * Returns a {@code ExpenseModelManager} with the data from {@code storage}'s expense book + * and {@code userPrefs}.
* The data from the sample expense book will be used instead if {@code storage}'s expense book is not found, * or an empty expense book will be used instead if errors occur when reading {@code storage}'s expense book. */ diff --git a/src/main/java/seedu/address/logic/commands/DeleteDescriptionCommand.java b/src/main/java/seedu/address/logic/commands/DeleteDescriptionCommand.java index e7bd6386fa6..e7aa4ee04dc 100644 --- a/src/main/java/seedu/address/logic/commands/DeleteDescriptionCommand.java +++ b/src/main/java/seedu/address/logic/commands/DeleteDescriptionCommand.java @@ -8,8 +8,9 @@ import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; -import seedu.address.model.person.Expense; import seedu.address.model.person.Description; +import seedu.address.model.person.Expense; + /** @@ -30,8 +31,8 @@ public class DeleteDescriptionCommand extends Command { public static final String MESSAGE_ARGUMENTS = "Index: %1$d, Description: %2$s"; public static final String MESSAGE_ADD_DESCRIPTION_SUCCESS = "Added description to Expense: %1$s \n"; public static final String MESSAGE_DELETE_DESCRIPTION_SUCCESS = "Removed description from Expense: %1$s \n"; - private final Index index; private static final Description EMPTY_DESCRIPTION = new Description(""); + private final Index index; /** * @param index of the expense in the filtered expense list to edit the description @@ -51,7 +52,8 @@ public CommandResult execute(Model model) throws CommandException { } Expense expenseToEdit = lastShownList.get(index.getZeroBased()); - Expense editedExpense = new Expense(expenseToEdit.getAmount(), expenseToEdit.getDate(), expenseToEdit.getCategory(), + Expense editedExpense = new Expense(expenseToEdit.getAmount(), expenseToEdit.getDate(), + expenseToEdit.getCategory(), EMPTY_DESCRIPTION); model.setExpense(expenseToEdit, editedExpense); @@ -85,4 +87,4 @@ private String generateSuccessMessage(Expense expenseToEdit) { String message = MESSAGE_DELETE_DESCRIPTION_SUCCESS; return String.format(message, expenseToEdit); } -} \ No newline at end of file +} diff --git a/src/main/java/seedu/address/logic/commands/ViewCategoryCommand.java b/src/main/java/seedu/address/logic/commands/ViewCategoryCommand.java index 9d3a9e6ea72..45de39452b9 100644 --- a/src/main/java/seedu/address/logic/commands/ViewCategoryCommand.java +++ b/src/main/java/seedu/address/logic/commands/ViewCategoryCommand.java @@ -1,25 +1,19 @@ package seedu.address.logic.commands; -import seedu.address.commons.core.Messages; -import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.exceptions.CommandException; -import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.Model; import seedu.address.model.person.Category; -import seedu.address.model.person.Description; import seedu.address.model.person.Expense; +import java.util.ArrayList; import java.util.List; -import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; -import static seedu.address.model.Model.PREDICATE_SHOW_ALL_EXPENSES; - public class ViewCategoryCommand extends Command { public static final String COMMAND_WORD = "viewCategory"; public static final String MESSAGE_USAGE = COMMAND_WORD - + ": Finds all the category labels used in the ExpenseBokk so far. \n" + + ": Finds all the category labels used in the ExpenseBook so far. \n" + "Example: " + COMMAND_WORD ; public static final String MESSAGE_VIEW_CATEGORY_LABELS_SUCCESS = @@ -30,10 +24,10 @@ public ViewCategoryCommand() { @Override public CommandResult execute(Model model) throws CommandException { - List categories = model.getCategoryLabels(); String message = ""; - for(int i = 0; i < categories.size()-1; i++){ - message = message + categories.get(i).toString() + "\n"; + List categories = model.getCategoryLabels(); + for (int i = 0; i < categories.size(); i++){ + message = message + categories.get(i).categoryName + "\n"; } return new CommandResult(MESSAGE_VIEW_CATEGORY_LABELS_SUCCESS + message); } From 50b47bda90860d5ec8ff0bb9f38d22786caa01e1 Mon Sep 17 00:00:00 2001 From: yyutong <1037662939@qq.com> Date: Wed, 14 Oct 2020 18:01:56 +0800 Subject: [PATCH 4/4] Fix bugs --- src/main/java/seedu/address/model/person/UniqueExpenseList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/model/person/UniqueExpenseList.java b/src/main/java/seedu/address/model/person/UniqueExpenseList.java index 933c5de8eb1..58fd87a002b 100644 --- a/src/main/java/seedu/address/model/person/UniqueExpenseList.java +++ b/src/main/java/seedu/address/model/person/UniqueExpenseList.java @@ -168,7 +168,7 @@ public double getRemainingBudget() { public List getCategoryLabels(){ List categories = new ArrayList<>(); - for(int i = 0; i < internalList.size() -1; i++) { + for(int i = 0; i < internalList.size(); i++) { Expense current = internalList.get(i); Category currentCategory = current.getCategory(); if(!categories.contains(currentCategory)){