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

Added a feature to generate report for a past month #72

Merged
merged 1 commit into from
Apr 3, 2024
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
64 changes: 64 additions & 0 deletions src/main/java/command/GenerateReportCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package command;

import financialtransactions.TransactionManager;

public class GenerateReportCommand extends BaseCommand {

public GenerateReportCommand(String[] commandParts) {
super(false,commandParts);
}

public String execute(TransactionManager manager) throws Exception{
String monthString = "";
int month = 0;
int year = 0;
if (commandParts[1].startsWith("m/")) {
monthString = commandParts[1].substring(2);
}
switch (monthString) {
case "JAN":
month = 1;
break;
case "FEB":
month = 2;
break;
case "MAR":
month = 3;
break;
case "APR":
month = 4;
break;
case "MAY":
month = 5;
break;
case "JUN":
month = 6;
break;
case "JUL":
month = 7;
break;
case "AUG":
month = 8;
break;
case "SEP":
month = 9;
break;
case "OCT":
month = 10;
break;
case "NOV":
month = 11;
break;
case "DEC":
month = 12;
break;
default:
return "Month must be in the form of MMM";
}
if (commandParts[2].startsWith("y/")) {
year = Integer.parseInt(commandParts[2].substring(2));
}
return manager.generateFullReport(monthString, month, year);
}

}
3 changes: 2 additions & 1 deletion src/main/java/command/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public String execute(TransactionManager manager) throws Exception {
baseString += "9) delete-reminder i/INDEX n/NAME a/AMOUNT d/DATE t/TIME c/CATEGORY\n";
baseString += "10) set-budget a/AMOUNT\n";
baseString += "11) view-history n/NUM \n";
baseString += "12) quit \n";
baseString += "12) generate-report m/MONTH y/YEAR\n";
baseString += "13) quit \n";
baseString += "_____________";
return baseString;
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/financeproject/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

public class Main {
public static void main(String[] args) throws SecurityException {
// Storage storage = new Storage("./data"); // Storage manager for jar file
Storage storage = new Storage("../../../data");
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 Down
29 changes: 26 additions & 3 deletions src/main/java/financialtransactions/TransactionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ public String toString(){
return baseString.toString();
}

//@@author chenhowy
public String toSave() {
StringBuilder baseString = new StringBuilder();
for (T transaction : transactionList) {
baseString.append(transaction.toSave());
}
return baseString.toString();
}

//@@author
protected void printTransactionsSafeInfo() {
int index = 1;
for (T transaction : transactionList) {
Expand Down Expand Up @@ -125,14 +126,36 @@ public void sortList() {
public void sortListByDate() {
this.transactionList.sort(new DateComparator<>());
}

//@@author chenhowy
public double totalSpentInPastMonth() {
double amount = 0;
for (T transaction : transactionList) {
if (transaction.getDate().getDateTime().getMonth() == LocalDateTime.now().getMonth()) {
if ((transaction.getDate().getDateTime().getMonth() == LocalDateTime.now().getMonth()) &&
transaction.getDate().getDateTime().isBefore(LocalDateTime.now())) {
amount += transaction.getAmount();
}
}
return amount;
}

public double getTotalSpentInMonth(int month, int year) {
double amount = 0;
for (T transaction : transactionList) {
if (transaction.getDate().getDateTime().getMonthValue() == month &&
transaction.getDate().getDateTime().getYear() == year) {
amount += transaction.getAmount();
}
}
return amount;
}
public int getTransactionsAfterToday() {
int numberOfTransactions = 0;
LocalDateTime today = LocalDateTime.now();
for (T transaction : transactionList) {
if (transaction.getDate().getDateTime().isAfter(today)) {
numberOfTransactions++;
}
}
return numberOfTransactions;
}
}
40 changes: 36 additions & 4 deletions src/main/java/financialtransactions/TransactionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import storage.BarChart;

import java.time.LocalDateTime;

public class TransactionManager {
private TransactionList<Transaction<?>> transactionList;
private TransactionList<Inflow> inflows;
Expand Down Expand Up @@ -173,20 +175,50 @@ public String showLastNTransactions(int n, boolean isIncludeBarChart) throws Exc
}

public String toSave() {
return String.format("%.2f\n", budget) + inflows.toSave() + outflows.toSave() + reminders.toSave();
return String.format("%.2f\n", budget) + transactionList.toSave();
}

//@@author chenhowy
public String generateQuickReport() {
String baseString = "";
baseString += String.format("You have spent " +
"%.2f in the current month.\n", outflows.totalSpentInPastMonth());
baseString += String.format("With a budget of " +
"%.2f, you have %.2f left to spend.\n", budget, budget - outflows.totalSpentInPastMonth());
"%.2f, you have %.2f left to spend.\n", budget, budget - outflows.totalSpentInPastMonth() -
reminders.totalSpentInPastMonth());
baseString += String.format("You have " +
"%d upcoming payments that require your attention", reminders.getTransactionListSize());
"%d upcoming payments that require your attention", reminders.getTransactionsAfterToday());
return baseString;
}


public String generateFullReport(String monthString, int month, int year) {
if (!isBefore(month, year)) {
return "Please enter a month that is before the current month";
}
String baseString = "";
baseString += String.format("In the month of %s %s, " +
"you had an income of $%.2f.\n", monthString, year, inflows.getTotalSpentInMonth(month, year));
baseString += String.format("You spent $%.2f.\n", outflows.getTotalSpentInMonth(month, year) +
reminders.getTotalSpentInMonth(month, year));
baseString += String.format("You managed to save $%.2f!",
inflows.getTotalSpentInMonth(month, year) - outflows.getTotalSpentInMonth(month, year) -
reminders.getTotalSpentInMonth(month, year));
return baseString;
}

public boolean isBefore(int month, int year) {
LocalDateTime today = LocalDateTime.now();
int todayMonth = today.getMonthValue();
int todayYear = today.getYear();
if (year < todayYear) {
return true;
} else if (year == todayYear && month < todayMonth) {
return true;
} else {
return false;
}
}
//@@author
public int getTransactionListSize() {
return transactionList.getTransactionListSize();
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import command.ViewHistoryCommand;
import command.BaseCommand;
import command.HelpCommand;
import command.GenerateReportCommand;
import customexceptions.IncompletePromptException;
import financialtransactions.Inflow;
import financialtransactions.Outflow;
Expand Down Expand Up @@ -118,6 +119,11 @@ public BaseCommand parseCommand(String command) throws Exception {
throw new IncompletePromptException(command);
}
return new ViewHistoryCommand(commandParts);
case "generate-report":
if (commandParts.length < 3) {
throw new IncompletePromptException(command);
}
return new GenerateReportCommand(commandParts);
case "undo":
UndoCommand undoCommand = new UndoCommand(lastCommandParts);
if (lastCommandParts[0].contains("inflow")) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@@author chenhowy
package storage;

import financialtransactions.Inflow;
Expand Down
Loading