Skip to content

Commit

Permalink
Add finance tracker (#37)
Browse files Browse the repository at this point in the history
* add basic finance tracker functionality

* RD: fixed test cases
  • Loading branch information
R-D-D-D committed Oct 16, 2019
1 parent 080074c commit db36df3
Show file tree
Hide file tree
Showing 34 changed files with 841 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_BUDGET_DISPLAYED_INDEX = "The budget index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_INVALID_PROJECT_DISPLAYED_INDEX = "The project index provided is invalid";
public static final String MESSAGE_INVALID_TASK_DISPLAYED_INDEX = "The task index provided is invalid";
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
Expand All @@ -23,7 +24,7 @@ public interface Logic {
* @throws CommandException If an error occurs during command execution.
* @throws ParseException If an error occurs during parsing.
*/
CommandResult execute(String commandText) throws CommandException, ParseException;
CommandResult execute(String commandText) throws CommandException, ParseException, IllegalValueException;

//======== AddressBook =======================================================================
/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.AddressBookParser;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyProjectList;
Expand All @@ -37,7 +37,7 @@ public LogicManager(Model model, Storage storage) {
}

@Override
public CommandResult execute(String commandText) throws CommandException, ParseException {
public CommandResult execute(String commandText) throws CommandException, IllegalValueException {
logger.info("----------------[USER COMMAND][" + commandText + "]");

CommandResult commandResult;
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddBudgetCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package seedu.address.logic.commands;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.finance.Budget;
import seedu.address.model.finance.Finance;
import seedu.address.model.project.Project;

import static seedu.address.logic.parser.CliSyntax.PREFIX_BUDGET;

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

import static java.util.Objects.requireNonNull;

/**
* Adds a budget type to the project.
*/

public class AddBudgetCommand extends Command {

private final List<Budget> budgets = new ArrayList<>();

public static final String COMMAND_WORD = "addBudget";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Add a budget type to the project"
+ "[" + PREFIX_BUDGET + "BUDGET]...\n"
+ "Example: " + COMMAND_WORD
+ PREFIX_BUDGET + "equipment $3000.00";
public static final String MESSAGE_SUCCESS = "New budgets added";

public AddBudgetCommand(List<Budget> bugets) {
this.budgets.addAll(bugets);
}

@Override
public CommandResult execute(Model model) throws CommandException, IllegalValueException {
requireNonNull(model);
if (!model.isCheckedOut()) {
throw new CommandException(model.checkoutConstrain());
}
Project currWorkingProject = model.getWorkingProject().get();
for (Budget budget : budgets) {
currWorkingProject.getFinance().addBudget(budget);
}
Project editedProject = new Project(currWorkingProject.getTitle(),
currWorkingProject.getDescription(), currWorkingProject.getTasks(),
new Finance(currWorkingProject.getFinance().getBudgets()));
model.setWorkingProject(editedProject);
model.setProject(currWorkingProject, editedProject);
return new CommandResult(String.format(MESSAGE_SUCCESS));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.project.Project;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;


import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.*;

public class AddProjectCommand extends Command {
public static final String COMMAND_WORD = "add_project";
public static final String COMMAND_WORD = "addProject";

public static final String MESSAGE_SUCCESS = "New project added: %1$s";

Expand Down
67 changes: 67 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddSpendingCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package seedu.address.logic.commands;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.finance.Budget;
import seedu.address.model.finance.Finance;
import seedu.address.model.finance.Spending;
import seedu.address.model.project.Project;

import java.util.List;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.*;

/**
* Adds a budget type to the project.
*/

public class AddSpendingCommand extends Command {

private final Spending toAdd;
private final Index index;

public static final String COMMAND_WORD = "addExpense";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Add an expense to the specific budget"
+ "index of the budget you want to add...\n"
+ "[" + PREFIX_DESCRIPTION + "DESCRIPTION]...\n"
+ "[" + PREFIX_EXPENSE + "EXPENSE]...\n"
+ "[" + PREFIX_TIME + "TIME]...s\n"
+ "Example: " + COMMAND_WORD
+ "1 s/bought pizza for the team ex/60.00 c/20/10/19";
public static final String MESSAGE_SUCCESS = "New expense added";

public AddSpendingCommand(Index index, Spending toAdd) {
requireNonNull(toAdd);
this.toAdd = toAdd;
this.index = index;
}

@Override
public CommandResult execute(Model model) throws CommandException, IllegalValueException {
requireNonNull(model);
if (!model.isCheckedOut()) {
throw new CommandException(model.checkoutConstrain());
}

Project currWorkingProject = model.getWorkingProject().get();
List<Budget> budgets = currWorkingProject.getFinance().getBudgets();

if (index.getZeroBased() >= budgets.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_BUDGET_DISPLAYED_INDEX);
}

budgets.get(index.getZeroBased()).addSpending(toAdd);
Project editedProject = new Project(currWorkingProject.getTitle(),
currWorkingProject.getDescription(), currWorkingProject.getTasks(),
new Finance(budgets));

model.setWorkingProject(editedProject);
model.setProject(currWorkingProject, editedProject);
return new CommandResult(String.format(MESSAGE_SUCCESS));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public CommandResult execute(Model model) throws CommandException {
Set<Task> newTaskList = new HashSet<>();
newTaskList.addAll(taskToEdit);
newTaskList.add(task);
Project editedProject = new Project(projectToEdit.getTitle(), projectToEdit.getDescription(), newTaskList);
Project editedProject = new Project(projectToEdit.getTitle(), projectToEdit.getDescription(), newTaskList, projectToEdit.getFinance());

if (projectToEdit.hasTask(task)) {
throw new CommandException(MESSAGE_DUPLICATE_TASK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public CheckoutCommand(Index index) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Project> lastShownList = model.getFilteredProjectList();

if (true) {
//throw new CommandException(lastShownList.get(0).getFinance().getBudgets().toString());
}
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PROJECT_DISPLAYED_INDEX);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/Command.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.logic.commands;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;

Expand All @@ -15,6 +16,6 @@ public abstract class Command {
* @return feedback message of the operation result for display
* @throws CommandException If an error occurs during command execution.
*/
public abstract CommandResult execute(Model model) throws CommandException;
public abstract CommandResult execute(Model model) throws CommandException, IllegalValueException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.finance.Finance;
import seedu.address.model.project.Project;
import seedu.address.model.project.Task;

Expand Down Expand Up @@ -52,8 +53,9 @@ public CommandResult execute(Model model) throws CommandException {
taskList.addAll(taskToEdit);
Task task = taskList.remove(index.getZeroBased());
newTaskList.addAll(taskList);
Finance finance = projectToEdit.getFinance();

Project editedProject = new Project(projectToEdit.getTitle(), projectToEdit.getDescription(), newTaskList);
Project editedProject = new Project(projectToEdit.getTitle(), projectToEdit.getDescription(), newTaskList, finance);


model.setProject(projectToEdit, editedProject);
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/seedu/address/logic/commands/ListBudgetCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import seedu.address.model.Model;
import seedu.address.model.finance.Budget;
import seedu.address.model.project.Project;

/**
* Lists all persons in the address book to the user.
*/
public class ListBudgetCommand extends Command {

public static final String COMMAND_WORD = "listBudget";

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

Project workingProject = model.getWorkingProject().get();
StringBuilder sb = new StringBuilder();
int index = 0;
for (Budget budget : workingProject.getFinance().getBudgets()) {
index++;
sb.append(index + ". ");
sb.append(budget.toString());
sb.append("\n");
}
return new CommandResult(sb.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BUDGET;

import java.util.List;
import java.util.stream.Stream;

import seedu.address.logic.commands.AddBudgetCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.finance.Budget;

/**
* Parses input arguments and creates a new AddCommand object
*/
public class AddBudgetCommandParser implements Parser<AddBudgetCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddBudgetCommand
* and returns an AddBudgetCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public AddBudgetCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_BUDGET);

if (!arePrefixesPresent(argMultimap, PREFIX_BUDGET)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddBudgetCommand.MESSAGE_USAGE));
}

List<Budget> budgetList = ParserUtil.parseBudgets(argMultimap.getAllValues(PREFIX_BUDGET));

return new AddBudgetCommand(budgetList);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import seedu.address.logic.commands.AddProjectCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.finance.Finance;
import seedu.address.model.project.Description;
import seedu.address.model.project.Project;
import seedu.address.model.project.Task;
Expand Down Expand Up @@ -36,8 +37,9 @@ public AddProjectCommand parse(String args) throws ParseException {
Title title = ParserUtil.parseTitle(argMultimap.getValue(PREFIX_NAME).get());
Description description = ParserUtil.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION).get());
Set<Task> tasks = new HashSet<>();
Finance finance = new Finance();

Project project = new Project(title, description, tasks);
Project project = new Project(title, description, tasks, finance);

return new AddProjectCommand(project);
}
Expand Down

0 comments on commit db36df3

Please sign in to comment.