Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2021S1#70 from yyutong/view-expense-c…
Browse files Browse the repository at this point in the history
…ommand

Add view command and parser
  • Loading branch information
yyutong authored Oct 14, 2020
2 parents 176a6a3 + 6111a83 commit ba8e3e2
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ The index refers to the index number shown in the displayed expense list.
Examples:
* `view 1` views the `amount, category, date and description` of the 1st expense displayed in the list.

### View Category Labels : `viewCategory`

Show all the category lables used in the UniSave.

Format: `viewCategory`

### Add a description to an expense : `addDes`

Add a description to an existing expense in the finance book.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package seedu.address.logic.commands;

public class ViewCategoryCommand {
}
52 changes: 52 additions & 0 deletions src/main/java/seedu/address/logic/commands/ViewCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.address.commons.core.Messages;
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 static java.util.Objects.requireNonNull;

public class ViewCommand extends Command {
public static final String COMMAND_WORD = "view";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Views the expense identified by the index number used in the displayed expense list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_VIEW_EXPENSE_SUCCESS = "View Expense: %1$s";

private final Index targetIndex;

public ViewCommand(Index targetIndex) {
this.targetIndex = targetIndex;
}

@Override
public CommandResult execute(Model model) throws CommandException {
List<Expense> lastShownList = model.getFilteredExpenseList();

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

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));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ViewCommand // instanceof handles nulls
&& targetIndex.equals(((ViewCommand) other).targetIndex)); // state check
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;

Expand Down Expand Up @@ -51,16 +52,18 @@ public Command parseCommand(String userInput) throws ParseException {

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 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);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package seedu.address.logic.parser;

public class ViewCategoryCommandParser {
}
32 changes: 32 additions & 0 deletions src/main/java/seedu/address/logic/parser/ViewCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CATEGORY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import seedu.address.commons.exceptions.IllegalValueException;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.ViewCommand;
import seedu.address.logic.parser.exceptions.ParseException;

public class ViewCommandParser {

/**
* Parses the given {@code String} of arguments in the context of the ViewCommand
* and returns a ViewCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ViewCommand parse(String args) throws ParseException {
requireNonNull(args);
try{
Index index = ParserUtil.parseIndex(args);
return new ViewCommand(index);
} catch(ParseException e) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ViewCommand.MESSAGE_USAGE));
}
}
}
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 @@ -5,6 +5,7 @@
import java.util.List;

import javafx.collections.ObservableList;
import seedu.address.commons.core.index.Index;
import seedu.address.model.person.Expense;
import seedu.address.model.person.UniqueExpenseList;

Expand Down Expand Up @@ -93,6 +94,10 @@ public void setExpense(Expense target, Expense editedExpense) {
expenses.setExpense(target, editedExpense);
}

public void viewExpense(Index index){
expenses.view(index);
}

/**
* Removes {@code key} from this {@code AddressBook}.
* {@code key} must exist in the address book.
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/seedu/address/model/ExpenseModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.collections.transformation.FilteredList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.index.Index;
import seedu.address.model.person.Expense;
import seedu.address.model.person.Person;

Expand Down Expand Up @@ -119,6 +120,16 @@ 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 =============================================================

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.index.Index;
import seedu.address.model.person.Expense;
import seedu.address.model.person.Person;

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.collections.transformation.FilteredList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.index.Index;
import seedu.address.model.person.Expense;
import seedu.address.model.person.Person;

Expand All @@ -23,7 +24,11 @@ public class ModelManager implements Model {
private final AddressBook addressBook;
private final UserPrefs userPrefs;
private final FilteredList<Person> filteredPersons;

private final FilteredList<Expense> filteredExpenses;
// private final ExpenseBook expenseBook;

private Expense expenseToBeViewed;

/**
* Initializes a ModelManager with the given addressBook and userPrefs.
Expand All @@ -36,6 +41,7 @@ public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs

this.addressBook = new AddressBook(addressBook);
this.userPrefs = new UserPrefs(userPrefs);
// this.expenseBook = new ExpenseBook(expenseBook);
filteredPersons = new FilteredList<>(this.addressBook.getPersonList());
filteredExpenses = new FilteredList<>(this.addressBook.getExpenseList());
}
Expand All @@ -44,6 +50,8 @@ public ModelManager() {
this(new AddressBook(), new UserPrefs());
}

//=========== ExpenseBook ==================================================================================

//=========== UserPrefs ==================================================================================

@Override
Expand Down Expand Up @@ -121,6 +129,7 @@ public void deletePerson(Person target) {
public void deleteExpense(Expense targetExpense) {
addressBook.removeExpense(targetExpense);
}

/**
* To add a person.
*/
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/seedu/address/model/person/Expense.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public int hashCode() {
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();

if (description.isEmpty()) {
builder.append(" Amount: ")
.append(this.getAmount())
Expand All @@ -98,14 +97,16 @@ public String toString() {
} else {
builder.append(" Amount: ")
.append(this.getAmount())
.append("/n")
.append(" Date: ")
.append(this.getDate())
.append(" Category: ")
.append("/n")
.append(this.getCategory())
.append(" Description: ")
.append(this.getDescription());
.append(this.getDescription())
.append("/n");
return builder.toString();
}
}

}
11 changes: 11 additions & 0 deletions src/main/java/seedu/address/model/person/UniqueExpenseList.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.commons.core.index.Index;
import seedu.address.model.person.exceptions.DuplicateExpenseException;
import seedu.address.model.person.exceptions.ExpenseNotFoundException;

Expand Down Expand Up @@ -51,6 +52,16 @@ public void add(Expense toAdd) {
internalList.add(toAdd);
}

/**
* View the detals of a certain expense.
*
* @param index The index of the expense to be viewed in the ExpenseBook.
*/
public void view(Index index){
requireAllNonNull(index);
Expense expense = internalList.get(index.getOneBased());
}

/**
* Replaces the expense {@code target} in the list with {@code editedExpense}.
* {@code target} must exist in the list.
Expand Down

0 comments on commit ba8e3e2

Please sign in to comment.