forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
1,505 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/seedu/address/logic/commands/AddBudgetCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_AMOUNT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_END_DATE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.budget.Budget; | ||
|
||
/** | ||
* Adds a budget into the budget list. | ||
*/ | ||
public class AddBudgetCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "budget"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a budget to the budget list.\n" | ||
+ "Parameters: " | ||
+ PREFIX_NAME + "NAME " | ||
+ PREFIX_AMOUNT + "AMOUNT " | ||
+ PREFIX_DATE + "START-DATE " | ||
+ PREFIX_END_DATE + "END-DATE...\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_NAME + "Japan Travel " | ||
+ PREFIX_AMOUNT + "$2000.00 " | ||
+ PREFIX_DATE + "12/12/2019 " | ||
+ PREFIX_END_DATE + "18/12/2019\n"; | ||
|
||
|
||
public static final String MESSAGE_SUCCESS = "New budget added: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_BUDGET = "This budget already exists in the budget list"; | ||
public static final String MESSAGE_BUDGET_CLASH = "This budget period clashes with another budget"; | ||
public static final String MESSAGE_START_BEFORE_END = "The budget end date has to be after its start date"; | ||
|
||
private final Budget toAdd; | ||
|
||
/** | ||
* Creates an AddBudgetCommand to add the specified {@code Budget} | ||
*/ | ||
public AddBudgetCommand(Budget budget) { | ||
requireNonNull(budget); | ||
toAdd = budget; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (toAdd.getEndDate().localDate.isBefore(toAdd.getStartDate().localDate)) { | ||
throw new CommandException(MESSAGE_START_BEFORE_END); | ||
} | ||
|
||
if (model.hasBudgetPeriodClash(toAdd)) { | ||
throw new CommandException(MESSAGE_BUDGET_CLASH); | ||
} | ||
|
||
if (model.hasBudget(toAdd)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_BUDGET); | ||
} | ||
|
||
model.addBudget(toAdd); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/seedu/address/logic/parser/AddBudgetCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_AMOUNT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_END_DATE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.AddBudgetCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.ExpenseList; | ||
import seedu.address.model.budget.Budget; | ||
import seedu.address.model.expense.Amount; | ||
import seedu.address.model.expense.Date; | ||
import seedu.address.model.expense.Name; | ||
|
||
/** | ||
* Parses input arguments and creates a new AddBudgetCommand 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_NAME, PREFIX_AMOUNT, PREFIX_DATE, PREFIX_END_DATE); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_AMOUNT, PREFIX_DATE, PREFIX_END_DATE) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddBudgetCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()); | ||
Amount amount = ParserUtil.parseAmount(argMultimap.getValue(PREFIX_AMOUNT).get()); | ||
Date startDate = ParserUtil.parseDate(argMultimap.getValue(PREFIX_DATE).get()); | ||
Date endDate = ParserUtil.parseDate(argMultimap.getValue(PREFIX_END_DATE).get()); | ||
|
||
Budget budget = new Budget(name, amount, amount, startDate, endDate, new ExpenseList()); | ||
|
||
return new AddBudgetCommand(budget); | ||
} | ||
|
||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.