Skip to content

Commit

Permalink
Merge pull request #162 from ShyamKrishna33/branch-docs
Browse files Browse the repository at this point in the history
Add documentation for TransactionList, DataStorage and Transaction classes
  • Loading branch information
ShyamKrishna33 committed Apr 15, 2024
2 parents 4da12ba + 0ad93c2 commit 3ccd4eb
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 11 deletions.
64 changes: 62 additions & 2 deletions src/main/java/budgetbuddy/storage/DataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Account> accounts) {
try {
File f = new File(ACCOUNTS_FILE_PATH);
Expand All @@ -69,6 +87,13 @@ public void saveAccounts(ArrayList<Account> 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<Transaction> transactionArrayList) throws IOException {
File f = new File(TRANSACTIONS_FILE_PATH);

Expand All @@ -84,7 +109,15 @@ public void saveTransactions(ArrayList<Transaction> 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<Integer> existingAccountNumbers)
throws FileCorruptedException, InvalidCategoryException {
String[] transactionInfo = s.split(" ,");
Expand Down Expand Up @@ -134,7 +167,16 @@ private Transaction parseDataToTransaction(String s, ArrayList<Integer> 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<Account> readAccountFile(ArrayList<Integer> existingAccountNumbers)
throws IOException, FileCorruptedException {
File f = new File(ACCOUNTS_FILE_PATH);
Expand Down Expand Up @@ -186,6 +228,13 @@ public ArrayList<Account> readAccountFile(ArrayList<Integer> 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<Transaction> readTransactionFile(ArrayList<Integer> existingAccountNumbers) throws IOException {
createDataFolderIfNotExists();
File f = new File(TRANSACTIONS_FILE_PATH);
Expand Down Expand Up @@ -215,6 +264,11 @@ public ArrayList<Transaction> readTransactionFile(ArrayList<Integer> 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);
Expand Down Expand Up @@ -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<Integer> existingAccountNumbers) {
try {
ArrayList<Transaction> transactions = readTransactionFile(existingAccountNumbers);
Expand Down
106 changes: 101 additions & 5 deletions src/main/java/budgetbuddy/transaction/TransactionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Transaction> transactions) {
this.transactions = transactions;
this.parser = new Parser();
Expand All @@ -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) {
Expand Down Expand Up @@ -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.");
Expand All @@ -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<Transaction> getPastTransactions(ArrayList<Transaction> 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;
Expand All @@ -154,6 +195,12 @@ public static ArrayList<Transaction> getPastTransactions(ArrayList<Transaction>
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<Transaction> getCustomDateTransactions(ArrayList<Transaction> transactions) {
String start = UserInterface.getStartDate();
String end = UserInterface.getEndDate();
Expand All @@ -169,6 +216,13 @@ public static ArrayList<Transaction> getCustomDateTransactions(ArrayList<Transac
return customDateTransactions;
}

/**
* Retrieves transactions associated with a specific account number.
*
* @param transactions The list of transactions to filter.
* @param accountNumber The account number for which transactions are to be retrieved.
* @return An ArrayList containing transactions associated with the specified account number.
*/
public static ArrayList<Transaction> getAccountTransactions(ArrayList<Transaction> transactions,
int accountNumber) {
ArrayList<Transaction> accountTransactions = new ArrayList<>();
Expand All @@ -180,6 +234,13 @@ public static ArrayList<Transaction> getAccountTransactions(ArrayList<Transactio
return accountTransactions;
}

/**
* Retrieves transactions associated with a specific category.
*
* @param transactions The list of transactions to filter.
* @param category The category for which transactions are to be retrieved.
* @return An ArrayList containing transactions associated with the specified category.
*/
public static ArrayList<Transaction> getCategoryTransactions(ArrayList<Transaction> transactions,
Category category) {
ArrayList<Transaction> categoryTransactions = new ArrayList<>();
Expand All @@ -191,6 +252,15 @@ public static ArrayList<Transaction> getCategoryTransactions(ArrayList<Transacti
return categoryTransactions;
}

/**
* Processes the user-selected list option to list a specific set of transactions
* and perform the corresponding action.
*
* @param accounts The list of accounts.
* @param accountManager The account manager for retrieving account information.
* @throws InvalidIndexException If the selected option index is invalid.
* @throws InvalidCategoryException If the selected category is invalid.
*/
public void processList(ArrayList<Account> accounts, AccountManager accountManager) throws InvalidIndexException,
InvalidCategoryException {
UserInterface.printListOptions();
Expand Down Expand Up @@ -240,6 +310,17 @@ public void processList(ArrayList<Account> 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 {
Expand All @@ -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;
Expand Down Expand Up @@ -295,6 +381,11 @@ public void helpWithUserCommands(String input){
}
}
//@@author

/**
* Displays insights based on transaction categories.
*/
//@@author ShyamKrishna33
public void displayInsights() {
Insight.displayCategoryInsight(transactions);
}
Expand All @@ -310,6 +401,11 @@ public ArrayList<Transaction> 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];
Expand Down
Loading

0 comments on commit 3ccd4eb

Please sign in to comment.