Skip to content

Commit

Permalink
Merge branch 'master' into branch-Code/FilterCommandFinal
Browse files Browse the repository at this point in the history
  • Loading branch information
sp4ce-cowboy committed Oct 28, 2023
2 parents 896ed00 + a618c21 commit 1c52660
Show file tree
Hide file tree
Showing 29 changed files with 761 additions and 43 deletions.
21 changes: 7 additions & 14 deletions src/main/java/unicash/logic/UniCashMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import unicash.logic.commands.ResetCommand;
import unicash.logic.commands.SummaryCommand;
import unicash.logic.parser.Prefix;
import unicash.model.budget.Budget;
import unicash.model.commons.Amount;
import unicash.model.transaction.Transaction;

Expand Down Expand Up @@ -102,22 +103,14 @@ public static String formatTransaction(Transaction transaction) {
}

/**
* Formats the {@code transaction} for output as a continuous string.
* Formats the {@code budget} for display to the user.
*/
public static String formatTransactionAsString(Transaction transaction) {
public static String formatBudget(Budget budget) {
final StringBuilder builder = new StringBuilder();
builder.append("Name: ")
.append(transaction.getName())
.append("; Type: ")
.append(transaction.getType())
.append("; Amount: ")
.append(transaction.getAmount())
.append("; Date: ")
.append(transaction.getDateTime())
.append("; Location: ")
.append(transaction.getLocation())
.append("; Category: ");
transaction.getCategories().forEach(builder::append);
builder.append("Amount: ")
.append(budget.getAmount())
.append("; \nInterval: ")
.append(budget.getInterval());
return builder.toString();
}
}
73 changes: 73 additions & 0 deletions src/main/java/unicash/logic/commands/AddBudgetCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package unicash.logic.commands;

import static java.util.Objects.requireNonNull;

import unicash.commons.util.ToStringBuilder;
import unicash.logic.UniCashMessages;
import unicash.logic.commands.exceptions.CommandException;
import unicash.logic.parser.CliSyntax;
import unicash.model.Model;
import unicash.model.budget.Budget;

/**
* Adds a budget to UniCash.
*/
public class AddBudgetCommand extends Command {
public static final String COMMAND_WORD = "add_budget";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a budget to UniCa$h. \n"
+ "\n"
+ "Parameters: "
+ CliSyntax.PREFIX_AMOUNT + "AMOUNT "
+ CliSyntax.PREFIX_INTERVAL + "TYPE "
+ "\n"
+ "Example: " + COMMAND_WORD + " "
+ CliSyntax.PREFIX_AMOUNT + "300 "
+ CliSyntax.PREFIX_INTERVAL + "month ";

public static final String MESSAGE_SUCCESS = "New budget added: \n\n%1$s";

private final Budget budget;

/**
* Creates an AddBudgetCommand to add the specified {@code Budget}
*/
public AddBudgetCommand(Budget budget) {
requireNonNull(budget);
this.budget = budget;
}
/**
* Executes the command and returns the result message.
*
* @param model {@code Model} which the command should operate on.
* @return feedback message of the operation result for display
* @throws CommandException If an error occurs during command execution.
*/
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
model.setBudget(budget);
return new CommandResult(String.format(MESSAGE_SUCCESS, UniCashMessages.formatBudget(budget)));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

if (!(other instanceof AddBudgetCommand)) {
return false;
}

AddBudgetCommand otherCommand = (AddBudgetCommand) other;
return budget.equals(otherCommand.budget);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("budget", budget)
.toString();
}
}
76 changes: 76 additions & 0 deletions src/main/java/unicash/logic/commands/EditBudgetCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package unicash.logic.commands;

import static java.util.Objects.requireNonNull;

import unicash.commons.util.ToStringBuilder;
import unicash.logic.commands.exceptions.CommandException;
import unicash.logic.parser.CliSyntax;
import unicash.model.Model;
import unicash.model.budget.Budget;

/**
* Edits the budget of the specified interval.
*/
public class EditBudgetCommand extends Command {
public static final String COMMAND_WORD = "edit_budget";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the budget of the specified interval. "
+ "Existing values will be overwritten by the input values."
+ "\n"
+ "Parameters: "
+ CliSyntax.PREFIX_AMOUNT + "AMOUNT "
+ CliSyntax.PREFIX_INTERVAL + "TYPE "
+ "\n"
+ "Example: " + COMMAND_WORD + " "
+ CliSyntax.PREFIX_AMOUNT + "300 "
+ CliSyntax.PREFIX_INTERVAL + "month ";

public static final String MESSAGE_EDIT_BUDGET_SUCCESS = "Edited Budget: \n\n%1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";

public final Budget budget;

/**
* Creates an EditBudgetCommand to edit the specified {@code Budget}
*/
public EditBudgetCommand(Budget budget) {
requireNonNull(budget);
this.budget = budget;
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

if (!(other instanceof EditBudgetCommand)) {
return false;
}

EditBudgetCommand e = (EditBudgetCommand) other;
return budget.equals(e.budget);
}

/**
* Executes the command and returns the result message.
*
* @param model {@code Model} which the command should operate on.
* @return feedback message of the operation result for display
* @throws CommandException If an error occurs during command execution.
*/
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

//TODO: Edit budget command

return null;
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("budget", budget)
.toString();
}
}
50 changes: 50 additions & 0 deletions src/main/java/unicash/logic/parser/AddBudgetCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package unicash.logic.parser;

import static unicash.logic.UniCashMessages.MESSAGE_INVALID_COMMAND_FORMAT;
import static unicash.logic.parser.CliSyntax.PREFIX_AMOUNT;
import static unicash.logic.parser.CliSyntax.PREFIX_INTERVAL;

import java.util.stream.Stream;

import unicash.logic.commands.AddBudgetCommand;
import unicash.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new AddBudgetCommand object
*/
public class AddBudgetCommandParser implements Parser<AddBudgetCommand> {
/**
* Parses {@code userInput} into a command and returns it.
*
* @throws ParseException if {@code userInput} does not conform the expected format
*/
@Override
public AddBudgetCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_AMOUNT, PREFIX_INTERVAL);

//check if mandatory fields (type, amt) are present
if (!arePrefixesPresent(argMultimap, PREFIX_AMOUNT, PREFIX_INTERVAL)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
AddBudgetCommand.MESSAGE_USAGE));
}

//check for duplicates
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_AMOUNT, PREFIX_INTERVAL);

//TODO: Parse amount and interval, and create new budget object

return null;
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values
* @param argumentMultimap
* @param prefixes
* @return
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}
}
1 change: 1 addition & 0 deletions src/main/java/unicash/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public class CliSyntax {
public static final Prefix PREFIX_LOCATION = new Prefix("l/");
public static final Prefix PREFIX_MONTH = new Prefix("month/");
public static final Prefix PREFIX_YEAR = new Prefix("year/");
public static final Prefix PREFIX_INTERVAL = new Prefix("interval/");
}
1 change: 0 additions & 1 deletion src/main/java/unicash/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,4 @@ public static UniqueCategoryList parseCategories(Collection<String> categories)

return new UniqueCategoryList(categoryList);
}

}
4 changes: 4 additions & 0 deletions src/main/java/unicash/logic/parser/UniCashParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.regex.Pattern;

import unicash.commons.core.LogsCenter;
import unicash.logic.commands.AddBudgetCommand;
import unicash.logic.commands.AddTransactionCommand;
import unicash.logic.commands.ClearTransactionsCommand;
import unicash.logic.commands.Command;
Expand Down Expand Up @@ -79,6 +80,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ClearTransactionsCommand.COMMAND_WORD:
return new ClearTransactionsCommand();

case AddBudgetCommand.COMMAND_WORD:
return new AddBudgetCommandParser().parse(arguments);

case ResetCommand.COMMAND_WORD:
return new ResetCommand();

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/unicash/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import javafx.collections.ObservableList;
import unicash.commons.core.GuiSettings;
import unicash.model.budget.Budget;
import unicash.model.transaction.Transaction;

/**
Expand All @@ -17,6 +18,11 @@ public interface Model {
*/
Predicate<Transaction> PREDICATE_SHOW_ALL_TRANSACTIONS = unused -> true;

/**
* Returns true if a budget with the same identity as {@code budget} exists in UniCash.
*/
Predicate<Budget> PREDICATE_SHOW_ALL_BUDGETS = unused -> true;

/**
* Replaces user prefs data with the data in {@code userPrefs}.
*/
Expand Down Expand Up @@ -92,6 +98,16 @@ public interface Model {
*/
void updateFilteredTransactionList(Predicate<Transaction> predicate);

/**
* Adds the given budget
*/
void setBudget(Budget budget);

/**
* Returns the budget
*/
Budget getBudget();

/**
* Updates the summary of expenses saved in UniCash.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/unicash/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javafx.collections.transformation.FilteredList;
import unicash.commons.core.GuiSettings;
import unicash.commons.core.LogsCenter;
import unicash.model.budget.Budget;
import unicash.model.transaction.Transaction;

/**
Expand Down Expand Up @@ -116,6 +117,15 @@ public void addTransaction(Transaction transaction) {
updateFilteredTransactionList(PREDICATE_SHOW_ALL_TRANSACTIONS);
}

@Override
public void setBudget(Budget budget) {
uniCash.setBudget(budget);
}

public Budget getBudget() {
return uniCash.getBudget();
}

//=========== Filtered Transaction List Accessors =============================================================

/**
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/unicash/model/ReadOnlyUniCash.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package unicash.model;

import javafx.collections.ObservableList;
import unicash.model.budget.Budget;
import unicash.model.transaction.Transaction;

/**
Expand All @@ -13,4 +14,9 @@ public interface ReadOnlyUniCash {
*/
ObservableList<Transaction> getTransactionList();

/**
* Returns an unmodifiable view of the budget.
*/
Budget getBudget();

}
15 changes: 15 additions & 0 deletions src/main/java/unicash/model/UniCash.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javafx.collections.ObservableList;
import unicash.commons.util.ToStringBuilder;
import unicash.model.budget.Budget;
import unicash.model.category.Category;
import unicash.model.transaction.Transaction;
import unicash.model.transaction.TransactionList;
Expand All @@ -17,6 +18,7 @@
public class UniCash implements ReadOnlyUniCash {

private final TransactionList transactions;
private Budget budget;

/*
* The 'unusual' code block below is a non-static initialization block, sometimes used to avoid duplication
Expand Down Expand Up @@ -91,6 +93,11 @@ public void removeTransaction(Transaction key) {
transactions.remove(key);
}

public void setBudget(Budget budget) {
requireNonNull(budget);
this.budget = budget;
}

/**
* Returns the amount for each category of expenses.
* Note: This function ignores all 'income' transactions
Expand Down Expand Up @@ -141,6 +148,14 @@ public ObservableList<Transaction> getTransactionList() {
return transactions.asUnmodifiableObservableList();
}

/**
* Returns an unmodifiable view of the budget.
*/
@Override
public Budget getBudget() {
return budget;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
Loading

0 comments on commit 1c52660

Please sign in to comment.