Skip to content

Commit

Permalink
Merge pull request #215 from nicleejy/master
Browse files Browse the repository at this point in the history
Newly discovered bugs, implemented some fixes
  • Loading branch information
gitsac authored Apr 8, 2023
2 parents 4f9ffee + cc2053f commit de46f89
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 35 deletions.
4 changes: 3 additions & 1 deletion src/main/java/fasttrack/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Messages {
public static final String MESSAGE_EXPENSES_LISTED_OVERVIEW = "%1$d expenses listed";

public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The index provided is invalid";
public static final String MESSAGE_INVALID_INDEX = "The index provided is invalid.";
public static final String MESSAGE_INVALID_CATEGORY_DISPLAYED_INDEX = "The category index provided is invalid!";
public static final String MESSAGE_INVALID_EXPENSE_DISPLAYED_INDEX = "The expense index provided is invalid!";
public static final String MESSAGE_INVALID_RECURRING_EXPENSE_DISPLAYED_INDEX = "The recurring expense index "
Expand All @@ -28,6 +28,8 @@ public class Messages {
public static final String MESSAGE_INVALID_CATEGORY_NAME = "Please provide a category name!";
public static final String MESSAGE_INVALID_EXPENSE_NAME = "Please provide an expense name!";
public static final String MESSAGE_ALREADY_EXISTING_CATEGORY = "This category name is already used!";
public static final String MESSAGE_INVALID_BUDGET = "Please provide a valid budget!";



}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class DeleteCategoryCommand implements DeleteCommand {
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_DELETE_CATEGORY_SUCCESS = "Deleted Category: %1$s";
public static final String MESSAGE_DELETE_CATEGORY_SUCCESS = "Deleted category: %1$s";

private final Index targetIndex;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public CommandResult execute(Model dataModel) throws CommandException {
List<Expense> lastShownList = dataModel.getFilteredExpenseList();

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
throw new CommandException(Messages.MESSAGE_INVALID_INDEX);
}

Expense expense = lastShownList.get(targetIndex.getZeroBased());
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/fasttrack/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public String toString() {
}


public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer.";
public static final String MESSAGE_INVALID_INDEX = "The index provided is invalid.";

/**
* Parses {@code oneBasedIndex} into an {@code Index} and returns it.
Expand Down Expand Up @@ -78,9 +78,7 @@ public static String parseExpenseName(String expenseName) throws ParseException
*/
public static Price parsePrice(String price) throws ParseException {
requireNonNull(price);
// remove trailing zeroes, commas, dollar signs, and spaces
String trimmedPrice = price.trim().replaceAll("0*$", "").replaceAll(",", "").replaceAll("\\$", "")
.replaceAll(" ", "");
String trimmedPrice = price.trim();
if (!Price.isValidPrice(trimmedPrice)) {
throw new ParseException(Price.MESSAGE_CONSTRAINTS);
}
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/fasttrack/model/Budget.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ public double getWeeklyBudget() {
return this.monthBudget / 4;
}

/**
* Set monthlyBudget.
* @param monthBudget
*/
public void setMonthlyBudget(double monthBudget) {
this.monthBudget = monthBudget;
}

@Override
public String toString() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/fasttrack/model/ExpenseTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableList;


/**
* Wraps all data at the expense tracker level
* Duplicate categories are not allowed (by .isSameCategory comparison)
Expand Down Expand Up @@ -139,6 +140,7 @@ public void addCategory(Category toAdd) {
public void removeCategory(Category key) {
categories.remove(key);
expenses.replaceDeletedCategory(key);
recurringGenerators.replaceDeletedCategory(key);
}

@Override
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/fasttrack/model/expense/Expense.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,10 @@ public Category getCategory() {

@Override
public String toString() {
return "Expense{"
+ "name='" + name + '\''
+ ", amount=" + amount
+ ", date=" + date
+ ", category='" + category + '\''
+ '}';
return "Name: " + name
+ ", Amount: $" + amount
+ ", Date: " + date
+ ", Category: " + category;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/fasttrack/model/expense/Price.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
public class Price {

public static final String MESSAGE_CONSTRAINTS =
"Prices should only contain numbers, and should not be negative";
public static final String VALIDATION_REGEX = "^(0|[1-9]\\d*)(\\.\\d+)?$";
"Amounts should only contain numbers, and should not be negative or empty.";
public static final String VALIDATION_REGEX = "^(-0|[1-9]\\d*)(\\.\\d+)?$";
private String value;

/**
Expand All @@ -31,7 +31,7 @@ public Price(String price) {
*/
public Price(double price) {
requireNonNull(price);
if (!(price >= -0.0)) {
if (!(price > -0.0)) {
throw new IllegalArgumentException(MESSAGE_CONSTRAINTS);
}
value = String.valueOf(price);
Expand All @@ -41,7 +41,7 @@ public Price(double price) {
* Returns true if a given string is a valid price.
*/
public static boolean isValidPrice(String test) {
return test.matches(VALIDATION_REGEX) && Double.parseDouble(test) >= -0;
return test.matches(VALIDATION_REGEX) && Double.parseDouble(test) > -0;
}


Expand Down
20 changes: 19 additions & 1 deletion src/main/java/fasttrack/model/expense/RecurringExpenseList.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package fasttrack.model.expense;

import static fasttrack.commons.util.CollectionUtil.requireAllNonNull;
import static java.util.Objects.requireNonNull;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import fasttrack.model.category.Category;
import fasttrack.model.category.MiscellaneousCategory;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
* Represents a List of Recurring Expenses in the Expense Tracker.
*/
Expand All @@ -18,13 +22,14 @@ public class RecurringExpenseList {
private final ObservableList<RecurringExpenseManager> internalUnmodifiableList = FXCollections
.unmodifiableObservableList(recurringExpenseList);

private final Category misc = new MiscellaneousCategory();

/**
* Adds a recurring expense to the internal list of recurring expenses.
* @param recurringExpense Recurring expense to add.
*/
public void addRecurringExpense(RecurringExpenseManager recurringExpense) {
recurringExpenseList.add(recurringExpense);

}


Expand Down Expand Up @@ -117,4 +122,17 @@ public String toString() {
}
return sb.toString();
}

/**
* Replace recurring expenses with {@code target} category with Misc object
* @param target
*/
public void replaceDeletedCategory(Category target) {
requireNonNull(target);
recurringExpenseList.forEach(recurringExpenseManager -> {
if (recurringExpenseManager.getExpenseCategory().equals(target)) {
recurringExpenseManager.setExpenseCategory(misc);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Objects;

import fasttrack.model.category.Category;

Expand Down Expand Up @@ -178,9 +179,10 @@ public void setNextExpenseDate(LocalDate nextExpenseDate) {

@Override
public String toString() {
return "Recurring Expense: " + expenseName + " Amount: " + amount + " Category: "
+ expenseCategory + " Start Date: " + startDate + " End Date: " + endDate
+ " Recurring Expense Type: " + recurringExpenseType;
String endStatus = endDate == null ? "Ongoing" : String.valueOf(endDate);
return "Recurring Expense: " + expenseName + ", Amount: " + amount + ", Category: "
+ expenseCategory + ", Start Date: " + startDate + ", End Date: " + endStatus
+ ", Recurring Expense Type: " + recurringExpenseType;
}

@Override
Expand All @@ -191,7 +193,7 @@ public boolean equals(Object obj) {
&& this.amount.equals(other.amount)
&& this.expenseCategory.equals(other.expenseCategory)
&& this.startDate.equals(other.startDate)
&& this.endDate == null ? other.endDate == null : this.endDate.equals(other.endDate)
&& Objects.equals(endDate, other.endDate)
&& this.recurringExpenseType.equals(other.recurringExpenseType);
}
return false;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/fasttrack/storage/JsonAdaptedCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

import fasttrack.commons.exceptions.IllegalValueException;
import fasttrack.model.category.Category;
import fasttrack.model.category.MiscellaneousCategory;
import fasttrack.model.category.UserDefinedCategory;



/**
* Jackson-friendly version of {@link Category}.
*/
Expand Down Expand Up @@ -53,6 +55,10 @@ public Category toModelType() throws IllegalValueException {
}

final String modelDescription = summary;

if (categoryName.equalsIgnoreCase("misc")) {
return new MiscellaneousCategory();
}
return new UserDefinedCategory(modelCategoryName, modelDescription);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import fasttrack.model.ExpenseTracker;
import fasttrack.model.ReadOnlyExpenseTracker;
import fasttrack.model.category.Category;
import fasttrack.model.category.MiscellaneousCategory;
import fasttrack.model.expense.Expense;
import fasttrack.model.expense.RecurringExpenseManager;

Expand Down Expand Up @@ -75,7 +76,9 @@ public ExpenseTracker toModelType() throws IllegalValueException {
RecurringExpenseManager expenseGenerator = jsonAdaptedGenerator.toModelType();
Category associatedCategory = getAssociatedCategoryForRecurring(expenseGenerator, expenseTracker);
if (associatedCategory == null) {
expenseTracker.addCategory(expenseGenerator.getExpenseCategory());
if (!(expenseGenerator.getExpenseCategory() instanceof MiscellaneousCategory)) {
expenseTracker.addCategory(expenseGenerator.getExpenseCategory());
}
} else {
expenseGenerator.setExpenseCategory(associatedCategory);
}
Expand All @@ -86,7 +89,9 @@ public ExpenseTracker toModelType() throws IllegalValueException {
Expense expense = jsonAdaptedExpense.toModelType();
Category associatedCategory = getAssociatedCategory(expense, expenseTracker);
if (associatedCategory == null) {
expenseTracker.addCategory(expense.getCategory());
if (!(expense.getCategory() instanceof MiscellaneousCategory)) {
expenseTracker.addCategory(expense.getCategory());
}
} else {
expense.setCategory(associatedCategory);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fasttrack/ui/HelpWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class HelpWindow extends UiPart<Stage> {
+ "CLEAR\n"
+ " - Clears all the expenses and categories stored in FastTrack\n\n";

public static final String HELP_MESSAGE = HELP_MESSAGE_COMMAND + "For more info, refer to the user guide: "
public static final String HELP_MESSAGE = "For more info, refer to the FastTrack user guide: "
+ USERGUIDE_URL;

private static final Logger logger = LogsCenter.getLogger(HelpWindow.class);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/fasttrack/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fasttrack.logic;

import static fasttrack.commons.core.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
import static fasttrack.commons.core.Messages.MESSAGE_INVALID_INDEX;
import static fasttrack.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND;
import static fasttrack.testutil.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void execute_invalidCommandFormat_throwsParseException() {
@Test
public void execute_commandExecutionError_throwsCommandException() {
String deleteCommand = "delete 9";
assertCommandException(deleteCommand, MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
assertCommandException(deleteCommand, MESSAGE_INVALID_INDEX);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/fasttrack/model/expense/ExpenseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void getCategory() {

@Test
public void toStringTest() {
assertEquals("Expense{name='test', amount=1.0, date=null, category='Miscellaneous'}", expense.toString());
assertEquals("Name: test, Amount: $1.0, Date: null, Category: Miscellaneous", expense.toString());
}

@Test
Expand Down

0 comments on commit de46f89

Please sign in to comment.