Skip to content

Commit

Permalink
Merge pull request #28 from Chinorea/branch-display-income
Browse files Browse the repository at this point in the history
Add Display Income with Filters
  • Loading branch information
Chinorea authored Oct 8, 2024
2 parents 88221cd + b399ec0 commit 099de8a
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 36 deletions.
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 @@ -7,6 +7,7 @@
import seedu.budgetbuddy.commands.DeductBudgetCommand;
import seedu.budgetbuddy.commands.DeleteExpenseCommand;
import seedu.budgetbuddy.commands.DeleteIncomeCommand;
import seedu.budgetbuddy.commands.DisplayIncomeCommand;
import seedu.budgetbuddy.commands.ExitCommand;
import seedu.budgetbuddy.commands.HelpCommand;
import seedu.budgetbuddy.commands.IncorrectCommand;
Expand All @@ -27,6 +28,7 @@
import seedu.budgetbuddy.validators.DeductBudgetValidator;
import seedu.budgetbuddy.validators.DeleteExpenseValidator;
import seedu.budgetbuddy.validators.DeleteIncomeValidator;
import seedu.budgetbuddy.validators.DisplayIncomeValidator;
import seedu.budgetbuddy.validators.ListBudgetValidator;
import seedu.budgetbuddy.validators.DisplayExpenseValidator;

Expand Down Expand Up @@ -96,6 +98,9 @@ public Command parseCommand(String userCommandText) {
if (DisplayExpenseCommand.isCommand(userCommandText)) {
return DisplayExpenseValidator.processCommand(userCommandText);
}
if (DisplayIncomeCommand.isCommand(userCommandText)) {
return DisplayIncomeValidator.processCommand(userCommandText);
}
return new IncorrectCommand("Invalid input");
}

Expand Down
16 changes: 9 additions & 7 deletions src/main/java/seedu/budgetbuddy/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,19 @@ public static void displayHelpMessage(){
"Examples:\ndel expense 1 \n" + "del income 2 \n" +
"3. Tag new category to expense. \n" + "Example:\n tag expense 2 /c food \n" +
"4. List all expenses/income. \n" + "Examples:\nlist expenses \n" + "list income\n" +
"5. Display expenses based on category and month. Note: category - c/, month (optional) - m/\n" +
"Example:\ndisplay expenses c/food m/10\n" +
"6. Add budget for current month. Note: month - m/MM/yyyy \n" +
"5. Display expenses based on category and month. Note: category - c/, month (optional) - m/MM/yyyy\n" +
"Example:\ndisplay expenses c/food m/10/2024\n" +
"6. Display income based on month. Note: month (optional) - m/MM/yyyy\n" +
"Example:\ndisplay incomes m/10/2024\n" +
"7. Add budget for current month. Note: month - m/MM/yyyy \n" +
"Example:\nadd budget a/1000 m/09/2024\n" +
"7. Deduct budget for current month. Note: month - m/MM/yyyy \n" +
"8. Deduct budget for current month. Note: month - m/MM/yyyy \n" +
"Example:\ndeduct budget a/500 m/10/2024\n" +
"8. list budget for specific month. Note: month - m/MM/yyyy \n" +
"9. list budget for specific month. Note: month - m/MM/yyyy \n" +
"Example:\nlist budget m/05/2024\n" +
"9. list budget for the 12 most recent entries. \n" +
"10. list budget for the 12 most recent entries. \n" +
"Example:\nlist budget\n" +
"10. Exit app. \n" +
"11. Exit app. \n" +
"Example:\nbye\n";
displayToUser(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public DisplayExpenseCommand() {
}

/**
* Checks if the provided command matches the command to list expenses.
* Checks if the provided command matches the command to display expenses.
*
* @param command The command to be checked.
* @return True if the command matches "list expenses", false otherwise.
* @return True if the command matches "display expenses", false otherwise.
*/
public static boolean isCommand(String command) {
return command.startsWith("display expense");
return command.startsWith("display expenses");
}

/**
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/seedu/budgetbuddy/commands/DisplayIncomeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package seedu.budgetbuddy.commands;

import seedu.budgetbuddy.transaction.income.IncomeManager;

import java.time.YearMonth;

public class DisplayIncomeCommand extends Command {

private YearMonth month;

/**
* Constructs a DisplayIncomeCommand with no specified month
*/
public DisplayIncomeCommand() {
this.month = null;
}

/**
* Constructs a DisplayIncomeCommand with a valid month field
* @param month
*/
public DisplayIncomeCommand(YearMonth month) {
this.month = month;
}

/**
* Checks if the provided command matches the command to display income.
*
* @param command The command to be checked.
* @return True if the command matches "display expenses", false otherwise.
*/
public static boolean isCommand(String command) { return command.startsWith("display incomes"); }

@Override
public void execute() {
if(month == null) {
IncomeManager.listIncomes();
}
else{
IncomeManager.displayIncomeWithMonth(month);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public static YearMonth getYearMonthFromDate(LocalDate date) {
return YearMonth.from(date);
}

/**
* Generates a custom empty Display Expense message
* @return custom display Expense message
*/
public static String getEmptyDisplayMessage() {
return "No expense entry with given parameters found, try again with a different parameter.";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import seedu.budgetbuddy.Ui;

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

/**
Expand Down Expand Up @@ -67,6 +69,43 @@ public static void listIncomes() {
Ui.displayToUser(result);
}

/**
* Display all income that matches with month field that are managed by the manager.
* Displays each income with its corresponding number.
* @param month
*/
public static void displayIncomeWithMonth(YearMonth month) {
String result = "";
int counter = 1;
for (Income income : incomes) {
if(month.equals(getYearMonthFromDate(income.getDate()))) {
result += counter + ". " + income.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);
}

/**
* Generates a custom empty Display income message
* @return custom display income message
*/
public static String getEmptyDisplayMessage() {
return "No income entry with given month found, try again with a different month.";
}

/**
* A get-function to obtain the information in the current Income List.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package seedu.budgetbuddy.validators;

import seedu.budgetbuddy.commands.Command;
import seedu.budgetbuddy.commands.DisplayIncomeCommand;
import seedu.budgetbuddy.commands.IncorrectCommand;

import java.time.YearMonth;

import static seedu.budgetbuddy.validators.DateValidator.validateYearMonth;

public class DisplayIncomeValidator {

/**
* Processes the command string to determine if it is valid for displaying income.
* If valid, it returns a DisplayIncomeCommand with the parsed date.
*
* @param command
* @return
*/
public static Command processCommand(String command) {
if (command.equals("display income")){
return new DisplayIncomeCommand();
}

String trimmedCommand = command.substring("display income ".length());
String[] parts = trimmedCommand.split(" ");

//Process Initial Value
YearMonth month = 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.");
}
}
}

return new DisplayIncomeCommand(month);
}

}
51 changes: 32 additions & 19 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -175,42 +175,55 @@ 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
1. 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
1. 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
1. 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

Unknown category. Use a valid category
========================================================
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
Invalid month format. Use m/MM/yyyy.
========================================================
Enter commands: ========================================================
No expense entry with given parameters found, try again with a different parameter.
========================================================
Enter commands: ========================================================
No expense entry with given parameters found, try again with a different parameter.
========================================================
Enter commands: ========================================================
The following income transaction has been added:
Description: Tuition Amount: 100.0 Date: 2024-12-15
You have 2 income transaction(s) in total.
========================================================
Enter commands: ========================================================
The following income transaction has been added:
Description: Internship Amount: 123.0 Date: 2024-10-13
You have 3 income transaction(s) in total.
========================================================
Enter commands: ========================================================
1. Description: Internship Amount: 123.0 Date: 2024-10-13
2. Description: Tuition Amount: 100.0 Date: 2024-12-15
3. Description: Internship Amount: 123.0 Date: 2024-10-13

========================================================
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
1. Description: Internship Amount: 123.0 Date: 2024-10-13
2. Description: Internship Amount: 123.0 Date: 2024-10-13

========================================================
Enter commands: ========================================================
No expense entry with given parameters found, try again with a different parameter.
No income entry with given month found, try again with a different month.
========================================================
Enter commands: ========================================================
Invalid month format. Use m/MM/yyyy.
========================================================
Enter commands: ========================================================
Bye!
Expand Down
20 changes: 13 additions & 7 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ 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
display expenses c/transport
display expenses m/08/2024
display expenses c/transport m/10/2024
display expenses c/incorrect category
display expenses m/invalid month
display expenses c/food
display expenses c/education m/02/2024
add income Tuition a/100.00 d/15/12/2024
add income Internship a/123 d/13/10/2024
display incomes
display incomes m/10/2024
display incomes m/08/2024
display incomes m/20/2024
bye

0 comments on commit 099de8a

Please sign in to comment.