From 378d4e8be5786db7fa059f2be78a7e949fa2a8a2 Mon Sep 17 00:00:00 2001 From: chydarren Date: Mon, 31 Oct 2022 21:05:46 +0800 Subject: [PATCH] Add ability to sort the transactions in ascending order of the transaction dates --- .../seedu/duke/command/ListAndStatsCommand.java | 5 +++++ .../seedu/duke/data/transaction/Transaction.java | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/duke/command/ListAndStatsCommand.java b/src/main/java/seedu/duke/command/ListAndStatsCommand.java index 6b4b94cf4..29778660c 100644 --- a/src/main/java/seedu/duke/command/ListAndStatsCommand.java +++ b/src/main/java/seedu/duke/command/ListAndStatsCommand.java @@ -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()); @@ -142,8 +143,12 @@ public ArrayList 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; } } \ No newline at end of file diff --git a/src/main/java/seedu/duke/data/transaction/Transaction.java b/src/main/java/seedu/duke/data/transaction/Transaction.java index 5c23cbd70..0271f0869 100644 --- a/src/main/java/seedu/duke/data/transaction/Transaction.java +++ b/src/main/java/seedu/duke/data/transaction/Transaction.java @@ -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 { //@@author chydarren private static final String PREFIX_CATEGORY = "["; private static final String POSTFIX_CATEGORY = "]"; @@ -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 /**