Skip to content

Commit

Permalink
Merge pull request #27 from Chinorea/branch-display-expense
Browse files Browse the repository at this point in the history
Add Display Expense with Filters
  • Loading branch information
Chinorea authored Oct 8, 2024
2 parents 2d34ace + c7d4afe commit 88221cd
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 0 deletions.
2 changes: 2 additions & 0 deletions data/BudgetBuddy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ expense | hello | 2.0 | 15/9/2024 | OTHERS
expense | go skiing | 500.0 | 15/9/2024 | OTHERS
expense | plane tickets to korea | 1000.0 | 15/9/2024 | OTHERS
expense | zoo tickets | 20.0 | 15/9/2024 | FOOD
expense | plane tickets to japan | 1000.0 | 15/10/2024 | OTHERS
expense | plane tickets to europe | 1000.0 | 1/8/2024 | OTHERS
income | tuition fees | 1000.0 | 15/9/2024
income | tuition | 2.0E8 | 15/9/2024
income | tuition fee | 1000.0 | 15/9/2024
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/budgetbuddy/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.budgetbuddy.commands.ListBudgetCommand;
import seedu.budgetbuddy.commands.ListExpenseCommand;
import seedu.budgetbuddy.commands.ListIncomeCommand;
import seedu.budgetbuddy.commands.DisplayExpenseCommand;
import seedu.budgetbuddy.transaction.budget.Budget;
import seedu.budgetbuddy.transaction.budget.BudgetManager;
import seedu.budgetbuddy.transaction.expense.Category;
Expand All @@ -27,6 +28,7 @@
import seedu.budgetbuddy.validators.DeleteExpenseValidator;
import seedu.budgetbuddy.validators.DeleteIncomeValidator;
import seedu.budgetbuddy.validators.ListBudgetValidator;
import seedu.budgetbuddy.validators.DisplayExpenseValidator;

import java.time.LocalDate;
import java.time.YearMonth;
Expand Down Expand Up @@ -91,6 +93,9 @@ public Command parseCommand(String userCommandText) {
if (HelpCommand.isCommand(userCommandText)){
return new HelpCommand();
}
if (DisplayExpenseCommand.isCommand(userCommandText)) {
return DisplayExpenseValidator.processCommand(userCommandText);
}
return new IncorrectCommand("Invalid input");
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import seedu.budgetbuddy.Ui;

import java.time.YearMonth;
import java.util.ArrayList;

import java.time.LocalDate;

/**
* Manages a list of expenses, providing functionalities to add, delete,
* and list expenses, as well as tracking the total number of expenses.
Expand Down Expand Up @@ -67,6 +70,80 @@ public static void listExpenses() {
Ui.displayToUser(result);
}

/**
* Display all expense that matches with month & category field
* Displays each expense with its corresponding number.
* @param category
* @param month
*/
public static void displayExpensesWithCategoryAndDate(Category category, YearMonth month) {
String result = "";
int counter = 1;
for (Expense expense : expenses) {
if(category.equals(expense.getCategory()) && month.equals(getYearMonthFromDate(expense.getDate()))) {
result += counter + ". " + expense.toString() + "\n";
counter++;
}
}
if(result.equals("")) {
result = getEmptyDisplayMessage();
}
Ui.displayToUser(result);
}

/**
* Display all expense that matches with category field
* Displays each expense with its corresponding number.
* @param category
*/
public static void displayExpensesWithCategory(Category category) {
String result = "";
int counter = 1;
for (Expense expense : expenses) {
if(category.equals(expense.getCategory())) {
result += counter + ". " + expense.toString() + "\n";
counter++;
}
}
if(result.equals("")) {
result = getEmptyDisplayMessage();
}
Ui.displayToUser(result);
}

/**
* Display all expense that matches with month field
* Displays each expense with its corresponding number.
* @param month
*/
public static void displayExpensesWithDate(YearMonth month) {
String result = "";
int counter = 1;
for (Expense expense : expenses) {
if(month.equals(getYearMonthFromDate(expense.getDate()))) {
result += counter + ". " + expense.toString() + "\n";
counter++;
}
}
if(result.equals("")) {
result = getEmptyDisplayMessage();
}
Ui.displayToUser(result);
}

/**
* Extract YearMonth value from date
* @param date
* @return
*/
public static YearMonth getYearMonthFromDate(LocalDate date) {
return YearMonth.from(date);
}

public static String getEmptyDisplayMessage() {
return "No expense entry with given parameters found, try again with a different parameter.";
}

/**
* A get-function to obtain the information in the current Expense List.
*
Expand Down
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
}
}

}
48 changes: 48 additions & 0 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,54 @@ You have 1 income transaction(s) in total.
Enter commands: ========================================================
1. Description: Internship Amount: 123.0 Date: 2024-10-13

========================================================
Enter commands: ========================================================
The following expense transaction has been added:
Description: Japan Flight Amount: 123.45 Date: 2024-10-30 Category: TRANSPORT
You have 2 expense transaction(s) in total.
========================================================
Enter commands: ========================================================
The following expense transaction has been added:
Description: Ipad Amount: 123.45 Date: 2024-08-30 Category: EDUCATION
You have 3 expense transaction(s) in total.
========================================================
Enter commands: ========================================================
1. Description: Buy Tesla Stock Amount: 100.1 Date: 2024-10-02 Category: OTHERS
2. Description: Japan Flight Amount: 123.45 Date: 2024-10-30 Category: TRANSPORT
3. Description: Ipad Amount: 123.45 Date: 2024-08-30 Category: EDUCATION

========================================================
Enter commands: ========================================================
1. Description: Buy Tesla Stock Amount: 100.1 Date: 2024-10-02 Category: OTHERS
2. Description: Japan Flight Amount: 123.45 Date: 2024-10-30 Category: TRANSPORT
3. Description: Ipad Amount: 123.45 Date: 2024-08-30 Category: EDUCATION

========================================================
Enter commands: ========================================================
1. Description: Buy Tesla Stock Amount: 100.1 Date: 2024-10-02 Category: OTHERS
2. Description: Japan Flight Amount: 123.45 Date: 2024-10-30 Category: TRANSPORT

========================================================
Enter commands: ========================================================
1. Description: Buy Tesla Stock Amount: 100.1 Date: 2024-10-02 Category: OTHERS
2. Description: Japan Flight Amount: 123.45 Date: 2024-10-30 Category: TRANSPORT
3. Description: Ipad Amount: 123.45 Date: 2024-08-30 Category: EDUCATION

========================================================
Enter commands: ========================================================
1. Description: Buy Tesla Stock Amount: 100.1 Date: 2024-10-02 Category: OTHERS
2. Description: Japan Flight Amount: 123.45 Date: 2024-10-30 Category: TRANSPORT
3. Description: Ipad Amount: 123.45 Date: 2024-08-30 Category: EDUCATION

========================================================
Enter commands: ========================================================
1. Description: Buy Tesla Stock Amount: 100.1 Date: 2024-10-02 Category: OTHERS
2. Description: Japan Flight Amount: 123.45 Date: 2024-10-30 Category: TRANSPORT
3. Description: Ipad Amount: 123.45 Date: 2024-08-30 Category: EDUCATION

========================================================
Enter commands: ========================================================
No expense entry with given parameters found, try again with a different parameter.
========================================================
Enter commands: ========================================================
Bye!
Expand Down
9 changes: 9 additions & 0 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@ remove incomes 1
list income
delete income 1
list incomes
add expense Japan Flight a/123.45 d/30/10/2024 c/transport
add expense Ipad a/123.45 d/30/08/2024 c/education
display expense c/transport
display expense m/08/2024
display expense c/transport m/10/2024
display expense c/incorrect category
display expense m/invalid month
display expense c/food
display expense c/education m/02/2024
bye

0 comments on commit 88221cd

Please sign in to comment.