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

Branch final junit #194

Merged
merged 10 commits into from
Nov 8, 2023
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
53 changes: 51 additions & 2 deletions src/main/java/seedu/financialplanner/cashflow/Cashflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
* Represents an income or expense.
*/
public abstract class Cashflow {

protected static double balance = 0;
Expand All @@ -21,6 +24,14 @@ public abstract class Cashflow {
protected boolean hasRecurred;
protected final double MAX_AMOUNT = 999999999999.99;

/**
* Constructor for a cashflow. hasRecurred variable is set to false by default and date is initialised depending
* on whether recur is set by the user.
*
* @param amount The value of the cashflow.
* @param recur The number of days before the next automatic addition of the cashflow.
* @param description The description of the cashflow.
*/
public Cashflow(double amount, int recur, String description) {
this.amount = amount;
this.recur = recur;
Expand All @@ -30,6 +41,16 @@ public Cashflow(double amount, int recur, String description) {
}
this.hasRecurred = false;
}

/**
* Constructor for a cashflow.
*
* @param amount The value of the cashflow.
* @param recur The number of days before the next automatic addition of the cashflow.
* @param description The description of the cashflow.
* @param date The date that the cashflow is added.
* @param hasRecurred Whether the cashflow has recurred.
*/
public Cashflow(double amount, int recur, String description, LocalDate date, boolean hasRecurred) {
this.amount = amount;
this.recur = recur;
Expand All @@ -41,6 +62,9 @@ public Cashflow(double amount, int recur, String description, LocalDate date, bo
protected Cashflow() {
}

/**
* Sets the balance to 0.
*/
public static void clearBalance() {
balance = 0;
}
Expand All @@ -49,9 +73,18 @@ public static void setBalance(double amount) {
balance = amount;
}

public void deleteCashflowvalue() {
}
/**
* Deletes the value of a cashflow from the balance.
*/
public abstract void deleteCashflowValue();

/**
* Rounds a double to the specified number of decimal places. The rounding is done half-up.
*
* @param value The double to be rounded.
* @param places The number of decimal places to round to.
* @return The rounded double.
*/
//@author mhadidg-reused
//Reused from https://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places
public static double round(double value, int places) {
Expand All @@ -65,6 +98,12 @@ public static double round(double value, int places) {
}
//@author mhadidg

/**
* Capitalizes the first letter of a provided string.
*
* @param line The input string to be capitalized.
* @return The string that has been capitalized.
*/
//@author Nick Bolton-reused
//Reused from
//https://stackoverflow.com/questions/1892765/how-to-capitalize-the-first-character-of-each-word-in-a-string
Expand All @@ -73,6 +112,11 @@ public String capitalize(String line) {
}
//@author Nick Bolton

/**
* Formats the cashflow into an easy-to-read format to be output to the user.
*
* @return The formatted cashflow.
*/
public String toString() {
DecimalFormat decimalFormat = new DecimalFormat("####0.00");

Expand Down Expand Up @@ -134,6 +178,11 @@ public String getDescription() {
return description;
}

/**
* Formats the cashflow into a standard format to be saved into a text file.
*
* @return The formatted cashflow.
*/
public String formatString() {
String string;
if (recur == 0) {
Expand Down
116 changes: 91 additions & 25 deletions src/main/java/seedu/financialplanner/cashflow/CashflowList.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Represents the list containing all cashflows.
*/
public class CashflowList {
private static final Logger logger = Logger.getLogger("Financial Planner Logger");

Expand All @@ -18,13 +21,26 @@ public class CashflowList {
private CashflowList() {
}

/**
* Gets the single instance of CashflowList class.
*
* @return the CashflowList instance.
*/
public static CashflowList getInstance() {
if (cashflowList == null) {
cashflowList = new CashflowList();
}
return cashflowList;
}

/**
* Add an income to the list.
*
* @param value The value of the income.
* @param type The type of the income, using the values in the enum of IncomeType.
* @param recur The number of days before the next automatic addition of the income.
* @param description The description of the income.
*/
public void addIncome(double value, IncomeType type, int recur, String description) {
try {
logger.log(Level.INFO, "Adding income");
Expand All @@ -45,6 +61,14 @@ private void addToList(Cashflow toAdd) {
list.add(toAdd);
}

/**
* Adds an expense to the list.
*
* @param value The value of the expense.
* @param type The type of the expense, using the values in the enum of ExpenseType.
* @param recur The number of days before the next automatic addition of the expense.
* @param description The description of the expense.
*/
public void addExpense(double value, ExpenseType type, int recur, String description) {
try {
logger.log(Level.INFO, "Adding expense");
Expand All @@ -61,20 +85,31 @@ public void addExpense(double value, ExpenseType type, int recur, String descrip
}
}

/**
* Deletes a cashflow when its category is not specified.
*
* @param index The index of the cashflow as displayed to the user.
* @return The value of the cashflow to be removed.
*/
public double deleteCashflowWithoutCategory(int index) {
int existingListSize = list.size();
int listIndex = index - 1;

Cashflow toRemove = list.get(listIndex);
list.remove(listIndex);
toRemove.deleteCashflowvalue();
toRemove.deleteCashflowValue();
ui.printDeletedCashflow(toRemove);

int newListSize = list.size();
assert newListSize == existingListSize - 1;
return toRemove.getAmount();
}

/**
* Deletes all future recurrences of a cashflow that has an unspecified category.
*
* @param index The index of the cashflow as displayed to the user.
*/
public void deleteRecurWithoutCategory(int index) {
int listIndex = index - 1;

Expand All @@ -88,10 +123,8 @@ public void deleteRecurWithoutCategory(int index) {
ui.printDeletedRecur(toRemoveRecur);
}
}
//helper method to find the index of a given cashflow in the overall list
//given its index in its respective list. e.g. "income 3" is the third income
//in the overall list
private int cashflowIndexFinder(CashflowCategory category, int cashflowIndex) {

private int cashflowIndexFinder(CashflowCategory category, int cashflowIndex) throws FinancialPlannerException {
assert category.equals(CashflowCategory.INCOME) || category.equals(CashflowCategory.EXPENSE)
|| category.equals(CashflowCategory.RECURRING);

Expand All @@ -103,7 +136,7 @@ private int cashflowIndexFinder(CashflowCategory category, int cashflowIndex) {
case RECURRING:
return findCashflowIndexFromRecurIndex(cashflowIndex);
default:
return -1;
throw new FinancialPlannerException("Error in finding cashflow in the list.");
}
}

Expand Down Expand Up @@ -153,37 +186,70 @@ private int findCashflowIndexFromRecurIndex(int cashflowIndex) {
}
return overallCashflowIndex;
}
public void deleteRecurWithCategory(CashflowCategory category, int index) {
int listIndex = cashflowIndexFinder(category, index);

Cashflow toRemoveRecur = list.get(listIndex);
if (toRemoveRecur.getRecur() == 0 || toRemoveRecur.hasRecurred) {
ui.showMessage("Cashflow is already not recurring or has already recurred");
} else {
toRemoveRecur.setDate(null);
toRemoveRecur.setRecur(0);
list.set(listIndex, toRemoveRecur);
ui.printDeletedRecur(toRemoveRecur);
/**
* Deletes all future recurrences of a cashflow that has a specified category.
*
* @param category The type of cashflow: income, expense or recurring.
* @param index The index of the cashflow as displayed to the user.
*/
public void deleteRecurWithCategory(CashflowCategory category, int index) {
try {
int listIndex = cashflowIndexFinder(category, index);
Cashflow toRemoveRecur = list.get(listIndex);
if (toRemoveRecur.getRecur() == 0 || toRemoveRecur.hasRecurred) {
ui.showMessage("Cashflow is already not recurring or has already recurred.");
} else {
toRemoveRecur.setDate(null);
toRemoveRecur.setRecur(0);
list.set(listIndex, toRemoveRecur);
ui.printDeletedRecur(toRemoveRecur);
}
} catch (FinancialPlannerException e) {
ui.showMessage(e.getMessage());
}
}

/**
* Deletes a cashflow that has a specified category.
*
* @param category The type of cashflow: income, expense or recurring.
* @param index The index of the cashflow as displayed to the user.
* @return The value of the cashflow to be deleted.
*/
public double deleteCashflowWithCategory(CashflowCategory category, int index) {
int existingListSize = list.size();
int listIndex = cashflowIndexFinder(category, index);
try {
int existingListSize = list.size();
int listIndex = cashflowIndexFinder(category, index);

Cashflow toRemove = list.get(listIndex);
list.remove(listIndex);
toRemove.deleteCashflowvalue();
ui.printDeletedCashflow(toRemove);
Cashflow toRemove = list.get(listIndex);
list.remove(listIndex);
toRemove.deleteCashflowValue();
ui.printDeletedCashflow(toRemove);

int newListSize = list.size();
assert newListSize == existingListSize - 1;
return toRemove.getAmount();
int newListSize = list.size();
assert newListSize == existingListSize - 1;
return toRemove.getAmount();
} catch (FinancialPlannerException e) {
ui.showMessage(e.getMessage());
}
return 0;
}

/**
* Adds a saved cashflow from the storage to the list.
*
* @param entry The cashflow object to be laoded.
*/
public void load(Cashflow entry) {
addToList(entry);
}

/**
* Formats the list to string with each entry seperated by a newline.
*
* @return The formatted list.
*/
public String getList() {
StringBuilder output = new StringBuilder();
for (Cashflow entry : list) {
Expand Down
44 changes: 43 additions & 1 deletion src/main/java/seedu/financialplanner/cashflow/Expense.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,51 @@

import java.time.LocalDate;

/**
* A cashflow object that represents an expense.
*/
public class Expense extends Cashflow {
protected ExpenseType type;

/**
* Constructor for an expense.
*
* @param amount The value of the expense.
* @param type The type of the expense, using the values in the enum of ExpenseType.
* @param recur The number of days before the next automatic addition of the expense.
* @param description The description of the expense.
* @throws FinancialPlannerException if the balance exceeds the minimum value of -999,999,999,999.99.
*/
public Expense(double amount, ExpenseType type, int recur, String description) throws FinancialPlannerException {
super(amount, recur, description);
this.type = type;
addExpenseValue();
}

/**
* Constructor for an expense.
*
* @param amount The value of the expense.
* @param type The type of the expense, using the values in the enum of ExpenseType.
* @param recur The number of days before the next automatic addition of the expense.
* @param description The description of the expense.
* @param date The date that the expense is added.
* @param hasRecurred Whether the expense has recurred.
* @throws FinancialPlannerException if the balance exceeds the minimum value of -999,999,999,999.99.
*/
public Expense(double amount, ExpenseType type, int recur,
String description, LocalDate date, boolean hasRecurred) throws FinancialPlannerException {
super(amount, recur, description, date, hasRecurred);
this.type = type;
addExpenseValue();
}

/**
* Constructor for an expense.
*
* @param expense An expense object to be copied.
* @throws FinancialPlannerException if the balance exceeds the minimum value of -999,999,999,999.99.
*/
public Expense(Expense expense) throws FinancialPlannerException {
this.amount = expense.getAmount();
this.recur = expense.getRecur();
Expand Down Expand Up @@ -53,17 +82,30 @@ private void addExpenseValue() throws FinancialPlannerException {
expenseBalance += this.amount;
}

/**
* Deletes the value of an expense from the balance.
*/
@Override
public void deleteCashflowvalue() {
public void deleteCashflowValue() {
balance += this.amount;
}

/**
* Formats the expense into an easy-to-read format to be output to the user.
*
* @return The formatted expense.
*/
@Override
public String toString() {
return "Expense" + System.lineSeparator() +
" Type: " + capitalize(type.toString().toLowerCase()) + System.lineSeparator() + super.toString();
}

/**
* Formats the expense into a standard format to be saved into a text file.
*
* @return The formatted expense.
*/
@Override
public String formatString() {
return "E | " + this.amount + " | " + this.type + super.formatString();
Expand Down
Loading
Loading