diff --git a/src/main/java/budgetbuddy/storage/DataStorage.java b/src/main/java/budgetbuddy/storage/DataStorage.java index 4f3d175d80..3b6da13229 100644 --- a/src/main/java/budgetbuddy/storage/DataStorage.java +++ b/src/main/java/budgetbuddy/storage/DataStorage.java @@ -22,11 +22,23 @@ import java.util.ArrayList; import java.util.Scanner; +/** + * This class provides methods for storing and retrieving data related to transactions and accounts. + * It includes methods for saving and loading transactions and accounts to/from files. + */ public class DataStorage { public static final String TRANSACTIONS_FILE_PATH = "./data/transactions.txt"; public static final String ACCOUNTS_FILE_PATH = "./data/accounts.txt"; public static final String FOLDER_PATH = "./data"; + /** + * Writes the provided string to a file at the given file path. + * + * @param stringToWrite The string to write to the file. + * @param filePath The path of the file to write to. + * @throws IOException If an I/O error occurs while writing to the file. + */ + //@@author ShyamKrishna33 private static void writeToFile(String stringToWrite, String filePath) throws IOException { FileWriter fw = new FileWriter(filePath, true); fw.write(stringToWrite); @@ -47,7 +59,13 @@ private static void createDataFolderIfNotExists() throws IOException { Files.createDirectories(dataFolderPath); } } + //@@author + /** + * Saves the list of accounts to the file in the ACCOUNT_FILE_PATH. + * + * @param accounts The list of accounts to save. + */ public void saveAccounts(ArrayList accounts) { try { File f = new File(ACCOUNTS_FILE_PATH); @@ -69,6 +87,13 @@ public void saveAccounts(ArrayList accounts) { } } + /** + * Saves the list of transactions to a file. + * + * @param transactionArrayList The list of transactions to save. + * @throws IOException If an I/O error occurs while saving the transactions. + */ + //@@author ShyamKrishna33 public void saveTransactions(ArrayList transactionArrayList) throws IOException { File f = new File(TRANSACTIONS_FILE_PATH); @@ -84,7 +109,15 @@ public void saveTransactions(ArrayList transactionArrayList) throws } } - // description, categoryNum, type, date, amount, accountNumber, accountName + /** + * Parses a string representing transaction data into a Transaction object. + * + * @param s The string representing the transaction data. + * @param existingAccountNumbers A list of existing account numbers. + * @return The parsed Transaction object. + * @throws FileCorruptedException If the file containing transaction data is corrupted. + * @throws InvalidCategoryException If the category specified in the transaction data is invalid. + */ private Transaction parseDataToTransaction(String s, ArrayList existingAccountNumbers) throws FileCorruptedException, InvalidCategoryException { String[] transactionInfo = s.split(" ,"); @@ -134,7 +167,16 @@ private Transaction parseDataToTransaction(String s, ArrayList existing return null; } } - + //@@author + + /** + * Reads account data from the accounts file and returns a list of Account objects. + * + * @param existingAccountNumbers A list of existing account numbers. + * @return The list of Account objects read from the file. + * @throws IOException If an I/O error occurs while reading the file. + * @throws FileCorruptedException If the file containing account data is corrupted. + */ public ArrayList readAccountFile(ArrayList existingAccountNumbers) throws IOException, FileCorruptedException { File f = new File(ACCOUNTS_FILE_PATH); @@ -186,6 +228,13 @@ public ArrayList readAccountFile(ArrayList existingAccountNumb return accounts; } + /** + * Reads transaction data from the transactions file and returns a list of Transaction objects. + * + * @param existingAccountNumbers A list of existing account numbers. + * @return The list of Transaction objects read from the file. + * @throws IOException If an I/O error occurs while reading the file. + */ public ArrayList readTransactionFile(ArrayList existingAccountNumbers) throws IOException { createDataFolderIfNotExists(); File f = new File(TRANSACTIONS_FILE_PATH); @@ -215,6 +264,11 @@ public ArrayList readTransactionFile(ArrayList existingAcc return transactionList; } + /** + * Loads the accounts from the accounts file and returns an AccountManager object. + * + * @return The loaded AccountManager object. + */ public AccountManager loadAccounts() { try { File f = new File(ACCOUNTS_FILE_PATH); @@ -252,6 +306,12 @@ private AccountManager createNewAccountManager() { return accountManager; } + /** + * Loads the transactions from the transactions file and returns a TransactionList object. + * + * @param existingAccountNumbers A list of existing account numbers. + * @return The loaded TransactionList object. + */ public TransactionList loadTransactions(ArrayList existingAccountNumbers) { try { ArrayList transactions = readTransactionFile(existingAccountNumbers); diff --git a/src/main/java/budgetbuddy/transaction/TransactionList.java b/src/main/java/budgetbuddy/transaction/TransactionList.java index 4497b9c6a4..d43a05f4f3 100644 --- a/src/main/java/budgetbuddy/transaction/TransactionList.java +++ b/src/main/java/budgetbuddy/transaction/TransactionList.java @@ -23,6 +23,9 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; +/** + * Represents a list of transactions and provides methods for managing them. + */ public class TransactionList { public static final int DELETE_BEGIN_INDEX = 7; @@ -46,11 +49,19 @@ public class TransactionList { private final DataStorage dataStorage = new DataStorage(); + /** + * Constructs a new TransactionList object with an empty list of transactions and a parser. + */ public TransactionList() { this.transactions = new ArrayList<>(); this.parser = new Parser(); } + /** + * Constructs a new TransactionList object with the specified list of transactions and a parser. + * + * @param transactions The list of transactions. + */ public TransactionList(ArrayList transactions) { this.transactions = transactions; this.parser = new Parser(); @@ -64,6 +75,15 @@ public void printTransactions() { UserInterface.printAllTransactions(transactions); } + /** + * Removes a transaction from the list based on the user input. + * + * @param input The user input specifying the transaction to be removed. + * @param accountManager The account manager for retrieving account information. + * @throws EmptyArgumentException If the input string is empty or does not contain the required parameter. + * @throws NumberFormatException If the index parsed from the input string is not a valid integer. + * @throws InvalidIndexException If the index is out of bounds or invalid. + */ public void removeTransaction(String input, AccountManager accountManager) throws EmptyArgumentException, NumberFormatException, InvalidIndexException { if (input.trim().length() < DELETE_BEGIN_INDEX) { @@ -107,11 +127,21 @@ void addTransaction(Transaction t) { transactions.add(t); } + /** + * Processes a transaction based on the user input and adds it to the transaction list. + * + * @param input The user input specifying the transaction details. + * @param account The account associated with the transaction. + * @throws InvalidTransactionTypeException If the transaction type is invalid. + * @throws InvalidAddTransactionSyntax If the syntax for adding a transaction is invalid. + * @throws EmptyArgumentException If the input string is empty or missing required arguments. + * @throws InvalidCategoryException If the category specified in the transaction is invalid. + */ public void processTransaction(String input, Account account) throws InvalidTransactionTypeException, InvalidAddTransactionSyntax, EmptyArgumentException, InvalidCategoryException { // Check for syntax for add transaction - String[] arguments = {"/a/","/t/", "/n/", "/$/", "/d/"}; + String[] arguments = {"/a/", "/t/", "/n/", "/$/", "/d/"}; for (String argument : arguments) { if (!input.contains(argument)) { throw new InvalidAddTransactionSyntax("Invalid add syntax."); @@ -126,16 +156,27 @@ public void processTransaction(String input, Account account) UserInterface.printAddMessage(fetchData, account.getBalance()); } + /** + * Saves the current list of transactions to a data storage. + * + * @throws IOException If an I/O error occurs while saving the transactions. + */ public void saveTransactionList() throws IOException { dataStorage.saveTransactions(transactions); } + /** + * Retrieves past transactions based on the specified duration. + * + * @param transactions The list of transactions to filter. + * @param duration The duration of past transactions to retrieve ("week" or "month"). + * @return An ArrayList containing past transactions based on the specified duration. + */ //@@author isaaceng7 - public static ArrayList getPastTransactions(ArrayList transactions, String duration) { LocalDate today = LocalDate.now(); LocalDate startDate = null; - switch(duration) { + switch (duration) { case "week": startDate = today.minusDays(DAYS_IN_WEEK + DAYS_OFFSET); break; @@ -154,6 +195,12 @@ public static ArrayList getPastTransactions(ArrayList return pastTransactions; } + /** + * Retrieves transactions within a custom date range. + * + * @param transactions The list of transactions to filter. + * @return An ArrayList containing transactions within the specified custom date range. + */ public static ArrayList getCustomDateTransactions(ArrayList transactions) { String start = UserInterface.getStartDate(); String end = UserInterface.getEndDate(); @@ -169,6 +216,13 @@ public static ArrayList getCustomDateTransactions(ArrayList getAccountTransactions(ArrayList transactions, int accountNumber) { ArrayList accountTransactions = new ArrayList<>(); @@ -180,6 +234,13 @@ public static ArrayList getAccountTransactions(ArrayList getCategoryTransactions(ArrayList transactions, Category category) { ArrayList categoryTransactions = new ArrayList<>(); @@ -191,6 +252,15 @@ public static ArrayList getCategoryTransactions(ArrayList accounts, AccountManager accountManager) throws InvalidIndexException, InvalidCategoryException { UserInterface.printListOptions(); @@ -240,6 +310,17 @@ public void processList(ArrayList accounts, AccountManager accountManag } + /** + * Processes the user input for editing a transaction and updates the transaction accordingly. + * + * @param input The user input specifying the transaction index to be edited. + * @param accountManager The account manager for retrieving account information. + * @throws EmptyArgumentException If the input string is empty or missing required arguments. + * @throws NumberFormatException If the index parsed from the input string is not a valid integer. + * @throws InvalidIndexException If the index is out of bounds or invalid. + * @throws InvalidEditTransactionData If the data for editing the transaction is invalid. + * @throws InvalidCategoryException If the specified category is invalid. + */ //@@author Vavinan public void processEditTransaction(String input, AccountManager accountManager) throws EmptyArgumentException, NumberFormatException, InvalidIndexException, InvalidEditTransactionData, InvalidCategoryException { @@ -264,9 +345,14 @@ public void processEditTransaction(String input, AccountManager accountManager) } } - public void helpWithUserCommands(String input){ + /** + * Provides help messages for user commands. + * + * @param input The user input specifying the command for which help is requested. + */ + public void helpWithUserCommands(String input) { String helpCommand = parser.parseHelpCommand(input); - switch(helpCommand.toLowerCase()){ + switch (helpCommand.toLowerCase()) { case ALL: UserInterface.printAllCommands(); break; @@ -295,6 +381,11 @@ public void helpWithUserCommands(String input){ } } //@@author + + /** + * Displays insights based on transaction categories. + */ + //@@author ShyamKrishna33 public void displayInsights() { Insight.displayCategoryInsight(transactions); } @@ -310,6 +401,11 @@ public ArrayList removeTransactionsByAccountNumber(int accountNumbe return transactionsToRemove; } + /** + * Searches transactions based on a keyword and prints search results. + * + * @param input The user input specifying the keyword to search for transactions. + */ public void searchTransactions(String input) { try { String keyword = input.split(" ")[1]; diff --git a/src/main/java/budgetbuddy/transaction/type/Transaction.java b/src/main/java/budgetbuddy/transaction/type/Transaction.java index 68054c3ce4..8b80ebf0bb 100644 --- a/src/main/java/budgetbuddy/transaction/type/Transaction.java +++ b/src/main/java/budgetbuddy/transaction/type/Transaction.java @@ -5,15 +5,39 @@ import java.time.LocalDate; import java.time.format.DateTimeFormatter; +/** + * Represents a transaction involving a financial account. + * This is an abstract class and must be subclassed to provide specific transaction types. + */ public abstract class Transaction { + // The account number associated with the transaction private final int accountNumber; + + // The name of the account associated with the transaction private final String accountName; + + // A brief description of the transaction private final String description; + + // The amount of money involved in the transaction private final double amount; + + // The category of the transaction private Category category; + + // The date of the transaction private final LocalDate date; - public Transaction(int accountNumber, String accountName, String description, double amount,String date) { + /** + * Constructs a new Transaction object with the specified parameters. + * + * @param accountNumber The account number associated with the transaction. + * @param accountName The name of the account associated with the transaction. + * @param description A brief description of the transaction. + * @param amount The amount of money involved in the transaction. + * @param date The date of the transaction in the format "dd-mm-yyyy". + */ + public Transaction(int accountNumber, String accountName, String description, double amount, String date) { this.accountNumber = accountNumber; this.accountName = accountName; this.description = description; @@ -41,25 +65,34 @@ public LocalDate getDate() { return date; } + /** + * Parses the date string into a LocalDate object. + * + * @param by The date string in the format "dd-MM-yyyy". + * @return The LocalDate object representing the date. + */ private LocalDate parseDate(String by) { return LocalDate.parse(by, DateTimeFormatter.ofPattern("dd-MM-yyyy")); } - + public Category getCategory() { return category; } public abstract String getTransactionType(); + /** + * Returns a string representation of the transaction. + */ @Override public String toString() { - return (" Account Number: " + getAccountNumber() + " | " + + return (" Account Number: " + getAccountNumber() + " | " + " Account Name: " + getAccountName() + " | " + " Transaction Type: " + getTransactionType() + " | " + " Description: " + getDescription() + " | " + " Date: " + getDate() + " | " + " Amount: " + getAmount() + " | " + - " Category: " + getCategory().getCategoryName()) ; + " Category: " + getCategory().getCategoryName()); } public void setCategory(Category category) {