Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sorting of transaction list by time #75

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions data/Bob.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0.00
Stocks|400.00|Oct 24 2024 12:00PM|INVESTMENT|I
Groceries|-150.00|Jun 23 2023 06:30PM|SHOPPING|O
Refund|100.00|Jun 23 2023 05:00PM|REFUND|I
3 changes: 3 additions & 0 deletions src/main/java/command/AddInflowCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public AddInflowCommand(String[] commandParts) {
}

private void createInflow() throws CategoryNotFoundException {
//@@author Kishen271828
String inflowName = null;
double inflowAmount = 0;
String inflowDate = null;
Expand All @@ -39,7 +40,9 @@ private void createInflow() throws CategoryNotFoundException {
inflow = new Inflow(inflowName, inflowAmount, inflowDateTime);
assert inflowCategory != null;
inflow.setCategory(Inflow.Category.valueOf(inflowCategory.toUpperCase()));
//@@author
}

public String execute(TransactionManager manager) {
//@@author Kishen271828
manager.addTransaction(inflow);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/command/AddOutflowCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public AddOutflowCommand(String[] commandParts) {
}

private void createOutflow() throws CategoryNotFoundException {
//@@author Kishen271828
String outflowName = null;
double outflowAmount = 0.0;
String outflowDate = null;
Expand All @@ -40,6 +41,7 @@ private void createOutflow() throws CategoryNotFoundException {
outflow = new Outflow(outflowName, outflowAmount, outflowDateTime);
assert outflowCategory != null;
outflow.setCategory(Outflow.Category.valueOf(outflowCategory.toUpperCase()));
//@@author
}

public String execute(TransactionManager manager) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/financeproject/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class Main {
public static void main(String[] args) throws SecurityException {
Storage storage = new Storage("./data"); // Storage manager for jar file

UI ui = new UI();
ui.printMessage("Welcome. Enter your username and password to login.");

Expand All @@ -25,7 +25,7 @@ public static void main(String[] args) throws SecurityException {
String response = "";

// Authenticating user
BaseUser user = null;
BaseUser user;
InactivityTimer inactivityTimer = new InactivityTimer();
try {
ui.printMessage("Username: ");
Expand All @@ -41,6 +41,7 @@ public static void main(String[] args) throws SecurityException {
} else {
return;
}

TransactionManager manager = storage.loadFile(user.getUsername());
ui.printMessage(manager.generateQuickReport());

Expand Down
5 changes: 0 additions & 5 deletions src/main/java/financeproject/data/data.txt

This file was deleted.

5 changes: 5 additions & 0 deletions src/main/java/financialtransactions/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public String toSave() {
public BaseDate getDate() {
return date;
}

public int compareTo(Transaction<?> otherTransaction) {
return this.date.compareTo(otherTransaction.getDate());
}

}
11 changes: 11 additions & 0 deletions src/main/java/financialtransactions/TransactionComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package financialtransactions;

import java.util.Comparator;

public class TransactionComparator implements Comparator<Transaction<?>> {
@Override
public int compare(Transaction<?> t1, Transaction<?> t2) {
// Compare the dates of the transactions
return t1.getDate().compareTo(t2.getDate());
}
}
8 changes: 8 additions & 0 deletions src/main/java/financialtransactions/TransactionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public String toSave() {
return baseString.toString();
}
//@@author

protected void printTransactionsSafeInfo() {
int index = 1;
for (T transaction : transactionList) {
Expand All @@ -97,6 +98,11 @@ public void setTransactionsType(String transactionsType) {
}
}

public void sortTransactions() {
transactionList.sort(new TransactionComparator());
}


public class NameComparator<T extends Transaction<?>> implements Comparator<T> {
@Override
public int compare(T o1, T o2) {
Expand Down Expand Up @@ -126,6 +132,7 @@ public void sortList() {
public void sortListByDate() {
this.transactionList.sort(new DateComparator<>());
}

//@@author chenhowy
public double totalSpentInPastMonth() {
double amount = 0;
Expand All @@ -148,6 +155,7 @@ public double getTotalSpentInMonth(int month, int year) {
}
return amount;
}

public int getTransactionsAfterToday() {
int numberOfTransactions = 0;
LocalDateTime today = LocalDateTime.now();
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/financialtransactions/TransactionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void setBudget(double budget) {

public void addTransaction(Transaction<?> transaction) {
transactionList.addTransaction(transaction);
// transactionList.sortList();
transactionList.sortTransactions();
if (transaction instanceof Inflow) {
Inflow inflow = (Inflow) transaction;
transactionList.setTransactionsType("Inflow");
Expand Down Expand Up @@ -76,15 +76,21 @@ public Inflow removeInflow(int index) throws Exception {
int numOfInflows = inflows.getTransactionListSize();
Inflow transactionRemoved = (Inflow) inflows.getNthTransaction(numOfInflows - index);
transactionList.removeTransactionIndex(transactionList.getIndexOfParticularTransaction(transactionRemoved));
transactionList.sortTransactions();

inflows.removeTransactionIndex(numOfInflows - index);
inflows.sortTransactions();
return transactionRemoved;
}

public Outflow removeOutflow(int index) throws Exception {
int numOfOutflows = outflows.getTransactionListSize();
Outflow transactionRemoved = (Outflow) outflows.getNthTransaction(numOfOutflows - index);
transactionList.removeTransactionIndex(transactionList.getIndexOfParticularTransaction(transactionRemoved));
transactionList.sortTransactions();

outflows.removeTransactionIndex(numOfOutflows - index);
outflows.sortTransactions();
return transactionRemoved;
}

Expand All @@ -96,20 +102,28 @@ public Reminder removeReminder(int index) throws Exception {
return transactionRemoved;
}

public boolean editInflow(int index, Transaction<?> updatedTransaction) throws Exception {
public Inflow editInflow(int index, Transaction<?> updatedTransaction) throws Exception {
int numOfInflows = inflows.getTransactionListSize();
Transaction<?> transactionEdited = inflows.getNthTransaction(numOfInflows - index);
Inflow transactionEdited = (Inflow) inflows.getNthTransaction(numOfInflows - index);
transactionList.editTransactionIndex(transactionList.getIndexOfParticularTransaction(transactionEdited),
updatedTransaction);
return inflows.editTransactionIndex(numOfInflows - index, (Inflow) updatedTransaction);
transactionList.sortTransactions();

inflows.editTransactionIndex(numOfInflows - index, (Inflow) updatedTransaction);
inflows.sortTransactions();
return (Inflow) transactionEdited;
}

public boolean editOutflow(int index, Transaction<?> updatedTransaction) throws Exception {
public Outflow editOutflow(int index, Transaction<?> updatedTransaction) throws Exception {
int numOfOutflows = outflows.getTransactionListSize();
Transaction<?> transactionEdited = outflows.getNthTransaction(numOfOutflows - index);
transactionList.editTransactionIndex(transactionList.getIndexOfParticularTransaction(transactionEdited),
updatedTransaction);
return outflows.editTransactionIndex(numOfOutflows - index, (Outflow) updatedTransaction);
transactionList.sortTransactions();

outflows.editTransactionIndex(numOfOutflows - index, (Outflow) updatedTransaction);
outflows.sortTransactions();
return (Outflow) transactionEdited;
}

public boolean editReminder(int index, Transaction<?> updatedTransaction) throws Exception {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/template/BaseDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ public boolean isBefore(BaseDate otherDate) {
public LocalDateTime getDateTime() {
return dateTime;
}

public int compareTo(BaseDate otherDate) {
return this.dateTime.compareTo(otherDate.dateTime);
}

}
2 changes: 0 additions & 2 deletions src/test/java/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public void addOutflow_success() throws Exception {

@Test
public void sampleTest() throws Exception{
UI ui = new UI();
Parser parser = new Parser(ui);
String assert1 = "add-inflow n/Salary a/400.00 d/23/05/2022 t/1900 c/income";
String assert2 = "add-outflow n/Rent a/1500.00 d/23/06/2023 t/1800 c/rent\n";
assertInstanceOf(AddInflowCommand.class, parser.parseCommand(assert1));
Expand Down
Loading