Skip to content

Commit

Permalink
Add ability to sort the transactions in ascending order of the transa…
Browse files Browse the repository at this point in the history
…ction dates
  • Loading branch information
chydarren committed Oct 31, 2022
1 parent c17abf0 commit 378d4e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/seedu/duke/command/ListAndStatsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public abstract class ListAndStatsCommand extends Command {
public static final int CONTAIN_EITHER = 2;
public static final int CONTAIN_EITHER_INVALID = 3;
private static final int FALSE = 0;
private static final String DAYS = "days";
private static final String WEEKS = "weeks";
private static final String MONTHS = "months";
private static Logger listStatsLogger = Logger.getLogger(ListAndStatsCommand.class.getName());
Expand Down Expand Up @@ -142,8 +143,12 @@ public ArrayList<Transaction> getTimeTransactions(TransactionList transactions)
timeTransactions = transactions.getTransactionsByMonthRange(LocalDate.now(), number);
} else if (containPeriodNumber() == CONTAIN_BOTH && period == WEEKS) {
timeTransactions = transactions.getTransactionsByWeekRange(LocalDate.now(), number);
} else if (containPeriodNumber() == CONTAIN_BOTH && period == DAYS) {
timeTransactions = transactions.getTransactionsByDayRange(LocalDate.now(), number);
}

// Sorts time-filtered transactions array list based on ascending order of date
Collections.sort(timeTransactions);
return timeTransactions;
}
}
15 changes: 14 additions & 1 deletion src/main/java/seedu/duke/data/transaction/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Represents a transaction made by the user, which could be either an income or an expense.
*/
public abstract class Transaction {
public abstract class Transaction implements Comparable<Transaction> {
//@@author chydarren
private static final String PREFIX_CATEGORY = "[";
private static final String POSTFIX_CATEGORY = "]";
Expand Down Expand Up @@ -68,6 +68,19 @@ public void setDate(LocalDate date) {
this.date = date;
}

//@@author chydarren

/**
* A comparator between dates of two transaction objects to facilitate sorting by date.
*
* @param transaction The transaction object.
* @return An integer that informs the order of comparison between two objects of same type.
*/
@Override
public int compareTo(Transaction transaction) {
return getDate().compareTo(transaction.getDate());
}

//@@author wcwy

/**
Expand Down

0 comments on commit 378d4e8

Please sign in to comment.