Skip to content

Commit

Permalink
Merge pull request #83 from yyutong/command-view-category
Browse files Browse the repository at this point in the history
Add view category command
  • Loading branch information
yyutong authored Oct 14, 2020
2 parents 4d2e956 + 50b47bd commit 7defdaf
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 45 deletions.
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/ExpenseMainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}. <br>
* Returns a {@code ExpenseModelManager} with the data from {@code storage}'s expense book
* and {@code userPrefs}. <br>
* 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;



/**
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -85,4 +87,4 @@ private String generateSuccessMessage(Expense expenseToEdit) {
String message = MESSAGE_DELETE_DESCRIPTION_SUCCESS;
return String.format(message, expenseToEdit);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
package seedu.address.logic.commands;

public class ViewCategoryCommand {
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Category;
import seedu.address.model.person.Expense;

import java.util.ArrayList;
import java.util.List;

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 ExpenseBook 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 {
String message = "";
List<Category> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public CommandResult execute(Model model) throws CommandException {
}

Expense expenseToView = lastShownList.get(targetIndex.getZeroBased());
String prefix = String.format(MESSAGE_VIEW_EXPENSE_SUCCESS, targetIndex.getZeroBased());
String prefix = String.format(MESSAGE_VIEW_EXPENSE_SUCCESS, targetIndex.getOneBased());
String message = prefix + "\n" + expenseToView.toString();
return new CommandResult(message);
}
Expand Down
64 changes: 29 additions & 35 deletions src/main/java/seedu/address/logic/parser/ExpenseBookParser.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ViewCategoryCommand>{
@Override
public ViewCategoryCommand parse(String args) throws ParseException {
return new ViewCategoryCommand();
}
}
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/ExpenseBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -145,4 +146,8 @@ public double getRemainingBudget() {
}
//end of yuanxing edits

public List<Category> getCategoryLabels(){
return expenses.getCategoryLabels();
}

}
9 changes: 8 additions & 1 deletion src/main/java/seedu/address/model/ExpenseModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -26,6 +28,7 @@ public class ExpenseModelManager implements Model {
private final FilteredList<Expense> filteredExpenses;



/**
* Initializes a ModelManager with the given addressBook and userPrefs.
*/
Expand Down Expand Up @@ -120,7 +123,6 @@ public void setExpense(Expense target, Expense editedExpense) {
expenseBook.setExpense(target, editedExpense);
}


//=========== Filtered Expense List Accessors =============================================================

/**
Expand Down Expand Up @@ -183,5 +185,10 @@ public void setExpenseBookBudget(double budget) {
expenseBook.setBudget(budget);
}

@Override
public List<Category> getCategoryLabels() {
return expenseBook.getCategoryLabels();
}


}
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -102,4 +104,6 @@ public interface Model {
double getExpenseBookRemaining();

void setExpenseBookBudget(double budget);

List<Category> getCategoryLabels();
}
8 changes: 7 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -28,7 +30,6 @@ public class ModelManager implements Model {
private final FilteredList<Expense> filteredExpenses;
// private final ExpenseBook expenseBook;

private Expense expenseToBeViewed;

/**
* Initializes a ModelManager with the given addressBook and userPrefs.
Expand Down Expand Up @@ -201,6 +202,11 @@ public void setExpenseBookBudget(double budget) {

}

@Override
public List<Category> getCategoryLabels() {
return null;
}

@Override
public boolean equals(Object obj) {
// short circuit if same object
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/model/person/Expense.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public String toString() {
.append("\n")
.append(" Date: ")
.append(this.getDate())
.append("\n")
.append(" Category: ")
.append(this.getCategory())
.append("\n")
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/seedu/address/model/person/UniqueExpenseList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -164,4 +165,16 @@ public double getRemainingBudget() {
}
return this.budget - used;
}

public List<Category> getCategoryLabels(){
List<Category> categories = new ArrayList<>();
for(int i = 0; i < internalList.size(); i++) {
Expense current = internalList.get(i);
Category currentCategory = current.getCategory();
if(!categories.contains(currentCategory)){
categories.add(currentCategory);
}
}
return categories;
}
}

0 comments on commit 7defdaf

Please sign in to comment.