forked from nus-cs2113-AY2425S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Chinorea/branch-display-expense
Add Display Expense with Filters
- Loading branch information
Showing
7 changed files
with
298 additions
and
0 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
75 changes: 75 additions & 0 deletions
75
src/main/java/seedu/budgetbuddy/commands/DisplayExpenseCommand.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,75 @@ | ||
package seedu.budgetbuddy.commands; | ||
|
||
import seedu.budgetbuddy.transaction.expense.Category; | ||
import seedu.budgetbuddy.transaction.expense.ExpenseManager; | ||
|
||
import java.time.YearMonth; | ||
|
||
public class DisplayExpenseCommand extends Command { | ||
|
||
private Category category; | ||
private YearMonth month; | ||
|
||
/** | ||
* Constructs a DisplayExpenseCommand with valid category and month field | ||
* @param category | ||
* @param month | ||
*/ | ||
public DisplayExpenseCommand(Category category, YearMonth month) { | ||
this.category = category; | ||
this.month = month; | ||
} | ||
|
||
/** | ||
* Constructs a DisplayExpenseCommand with valid month field | ||
* @param month | ||
*/ | ||
public DisplayExpenseCommand(YearMonth month) { | ||
this.category = null; | ||
this.month = month; | ||
} | ||
|
||
/** | ||
* Constructs a DisplayExpenseCommand with valid category field | ||
* @param category | ||
*/ | ||
public DisplayExpenseCommand(Category category) { | ||
this.category = category; | ||
this.month = null; | ||
} | ||
|
||
/** | ||
* Constructs a DisplayExpenseCommand with no specified date or category | ||
*/ | ||
public DisplayExpenseCommand() { | ||
this.category = null; | ||
this.month = null; | ||
} | ||
|
||
/** | ||
* Checks if the provided command matches the command to list expenses. | ||
* | ||
* @param command The command to be checked. | ||
* @return True if the command matches "list expenses", false otherwise. | ||
*/ | ||
public static boolean isCommand(String command) { | ||
return command.startsWith("display expense"); | ||
} | ||
|
||
/** | ||
* Executes the command to list all expenses by invoking the ExpenseManager's method. | ||
*/ | ||
@Override | ||
public void execute() { | ||
if (category == null && month == null) { | ||
ExpenseManager.listExpenses(); | ||
} else if (category == null){ | ||
ExpenseManager.displayExpensesWithDate(month); | ||
} else if (month == null) { | ||
ExpenseManager.displayExpensesWithCategory(category); | ||
} else { | ||
ExpenseManager.displayExpensesWithCategoryAndDate(category,month); | ||
} | ||
} | ||
|
||
} |
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
82 changes: 82 additions & 0 deletions
82
src/main/java/seedu/budgetbuddy/validators/DisplayExpenseValidator.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,82 @@ | ||
package seedu.budgetbuddy.validators; | ||
|
||
import seedu.budgetbuddy.commands.Command; | ||
import seedu.budgetbuddy.commands.DisplayExpenseCommand; | ||
import seedu.budgetbuddy.commands.IncorrectCommand; | ||
import seedu.budgetbuddy.transaction.expense.Category; | ||
import java.time.YearMonth; | ||
import static seedu.budgetbuddy.validators.DateValidator.validateYearMonth; | ||
|
||
public class DisplayExpenseValidator{ | ||
|
||
/** | ||
* Processes the command string to determine if it is valid for displaying expenses. | ||
* If valid, it returns a DisplayExpenseCommand with the parsed date. | ||
* | ||
* @param command | ||
* @return | ||
*/ | ||
public static Command processCommand(String command) { | ||
if (command.equals("display expenses")){ | ||
return new DisplayExpenseCommand(); | ||
} | ||
|
||
String trimmedCommand = command.substring("display expenses ".length()); | ||
String[] parts = trimmedCommand.split(" "); | ||
|
||
//Process Initial Value | ||
YearMonth month = null; | ||
Category category = null; | ||
|
||
//Process parts to extract details | ||
for (String part : parts) { | ||
if (part.startsWith("m/")) { | ||
month = validateYearMonth(part); | ||
if (month == null) { | ||
return new IncorrectCommand("Invalid month format. Use m/MM/yyyy."); | ||
} | ||
} else if (part.startsWith("c/")) { | ||
category = parseCategory(part); | ||
if (category == null) { | ||
return new IncorrectCommand("Unknown category. Use a valid category"); | ||
} | ||
} | ||
} | ||
|
||
//Check of Display Type | ||
return checkDisplayType(category, month); | ||
} | ||
|
||
/** | ||
* Checks the value of category and date and returns the corresponding | ||
* DisplayExpenseCommand type | ||
* @param category | ||
* @param date | ||
* @return | ||
*/ | ||
public static Command checkDisplayType(Category category, YearMonth date) { | ||
if (category != null && date == null) { | ||
return new DisplayExpenseCommand(category); | ||
} else if (category == null && date != null) { | ||
return new DisplayExpenseCommand(date); | ||
} else{ | ||
return new DisplayExpenseCommand(category, date); | ||
} | ||
} | ||
|
||
/** | ||
* Parses the category from the command part. | ||
* | ||
* @param part The command part containing the category. | ||
* @return The parsed category or OTHERS if invalid. | ||
*/ | ||
private static Category parseCategory(String part) { | ||
String categoryStr = part.substring(2).toUpperCase(); | ||
try { | ||
return Category.valueOf(categoryStr); | ||
} catch (IllegalArgumentException e) { | ||
return null; // if invalid category search applied | ||
} | ||
} | ||
|
||
} |
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