Skip to content

Commit

Permalink
Merge pull request #105 from ruihan00/code-cleanup
Browse files Browse the repository at this point in the history
Change fixed tags to enum
  • Loading branch information
JasonYapzx authored Oct 27, 2022
2 parents 4ee17b2 + da50aaa commit c89ddcd
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ public class SummaryCommand extends Command {
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_MONTH + "2022-10";

public static final String MESSAGE_SUCCESS = "Financials Summarized \n"
+ "Total Expenditure: %.2f\n"
+ "Total Income: %.2f\n"
+ "Total Balance: %.2f";
public static final String MESSAGE_SUCCESS = "Total Expenditure: $%.2f\n"
+ "Total Income: $%.2f\n"
+ "Total Balance: $%.2f";

private final Predicate<Entry> predicate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public ViewCommand parse(String args) throws ParseException {
viewEntriesDescriptor.setYearMonth(
ParserUtil.parseYearMonth(argMultimap.getValue(PREFIX_MONTH).get()));
}

} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE), pe);
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/seedu/pennywise/model/entry/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,15 @@ public boolean isSameEntry(Entry otherEntry) {
if (otherEntry == this) {
return true;
}
if (!(otherEntry instanceof Entry)) {
return false;
}

return otherEntry != null
&& otherEntry.getDescription().equals(getDescription());
Entry otherEntryCopy = (Entry) otherEntry;
return otherEntryCopy.getDescription().equals(getDescription())
&& otherEntryCopy.getDate().equals(getDate())
&& otherEntryCopy.getAmount().equals(getAmount())
&& otherEntryCopy.getTag().equals(getTag());
}

/**
Expand Down
140 changes: 113 additions & 27 deletions src/main/java/seedu/pennywise/model/entry/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static java.util.Objects.requireNonNull;
import static seedu.pennywise.commons.util.AppUtil.checkArgument;

import java.util.List;

/**
* Represents a Tag in the PennyWise application.
* Guarantees: immutable; name is valid as declared in {@link #isValidTagName(EntryType, String)}
Expand All @@ -13,26 +11,104 @@ public class Tag {

public static final String MESSAGE_CONSTRAINTS = "Tag names should be alphanumeric";
public static final String INCOME_CONSTRAINTS = "Income tag must only be one of the following: \n"
+ "Salary, Allowance, Profit, Investment, Gifts, Others";
+ IncomeTag.getAllTags();
public static final String EXPENDITURE_CONSTRAINTS = "Expenditure tag must only be one of the following: \n"
+ "Food, Groceries, Entertainment, Transport, Education, Housing, Others";
+ ExpenditureTag.getAllTags();
public static final String VALIDATION_REGEX = "\\p{Alnum}+";
public static final List<String> INCOME_TAGS = List.of(
"Salary",
"Allowance",
"Profit",
"Investment",
"Gifts",
"Others");
public static final List<String> EXPENDITURE_TAGS = List.of(
"Food",
"Groceries",
"Entertainment",
"Transport",
"Education",
"Housing",
"Others");
public final String tagName;

/**
* Wrapper for tags.
*/
public interface EntryTag {

}

/**
* Income tags that can be used.
*/
public enum IncomeTag implements EntryTag {
SALARY,
ALLOWANCE,
PROFIT,
INVESTMENT,
GIFTS,
OTHERS;

/**
* Checks that the input of {@code tag} is a valid income tag.
*/
public static boolean isValid(String tag) {
for (IncomeTag incomeTag: values()) {
if (incomeTag.name().equalsIgnoreCase(tag)) {
return true;
}
}
return false;
}

public static String getAllTags() {
StringBuilder sb = new StringBuilder();
for (IncomeTag incomeTag: values()) {
sb.append(incomeTag.toString());
if (!incomeTag.equals(IncomeTag.OTHERS)) {
sb.append(", ");
}
}
return sb.toString();
}


@Override
public String toString() {
String name = this.name();
String camelCaseName = name.substring(0, 1) + name.substring(1).toLowerCase();
return camelCaseName;
}

}
/**
* Expenditure tags that can be used.
*/
public enum ExpenditureTag implements EntryTag {
FOOD,
GROCERIES,
ENTERTAINMENT,
TRANSPORT,
EDUCATION,
HOUSING,
OTHERS;
/**
* Checks that the input of {@code tag} is a valid expenditure tag.
*/
public static boolean isValid(String tag) {
for (ExpenditureTag expenditureTag: values()) {
if (expenditureTag.name().equalsIgnoreCase(tag)) {
return true;
}
}
return false;
}

public static String getAllTags() {
StringBuilder sb = new StringBuilder();
for (ExpenditureTag expenditureTag: values()) {
sb.append(expenditureTag.toString());
if (!expenditureTag.equals(ExpenditureTag.OTHERS)) {
sb.append(", ");
}
}
return sb.toString();
}

@Override
public String toString() {
String name = this.name();
String camelCaseName = name.substring(0, 1) + name.substring(1).toLowerCase();
return camelCaseName;
}

}
private final EntryTag tag;

/**
* Constructs a {@code Tag}.
Expand All @@ -42,7 +118,17 @@ public class Tag {
public Tag(EntryType type, String tagName) {
requireNonNull(tagName);
checkArgument(isValidTagName(type, tagName), MESSAGE_CONSTRAINTS);
this.tagName = tagName;
switch (type.getEntryType()) {
case INCOME:
this.tag = IncomeTag.valueOf(tagName.toUpperCase());
break;
case EXPENDITURE:
this.tag = ExpenditureTag.valueOf(tagName.toUpperCase());
break;
default:
// Should not reach here
this.tag = null;
}
}

/**
Expand All @@ -56,12 +142,12 @@ public static boolean isValidTagName(EntryType type, String test) {
}
switch (type.getEntryType()) {
case INCOME:
if (!INCOME_TAGS.contains(test)) {
if (!IncomeTag.isValid(test)) {
return false;
}
break;
case EXPENDITURE:
if (!EXPENDITURE_TAGS.contains(test)) {
if (!ExpenditureTag.isValid(test)) {
return false;
}
break;
Expand All @@ -78,20 +164,20 @@ public static boolean isValidTagName(EntryType type, String test) {
* @return Name of the tag.
*/
public String getTagName() {
return tagName;
return tag.toString();
}


@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Tag // instanceof handles nulls
&& getTagName().equals(((Tag) other).getTagName())); // state check
&& tag.equals(((Tag) other).tag)); // state check
}

@Override
public int hashCode() {
return tagName.hashCode();
return tag.hashCode();
}

/**
Expand All @@ -100,7 +186,7 @@ public int hashCode() {
* @return Formatted tag.
*/
public String toString() {
return getTagName();
return tag.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public JsonAdaptedEntry(Entry source) {
description = source.getDescription().fullDescription;
amount = source.getAmount().toString();
date = source.getDate().toString();
tagged = source.getTag().tagName;
tagged = source.getTag().getTagName();
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/pennywise/ui/EntryCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public EntryCard(Entry entry, int displayedIndex) {
description.setText(entry.getDescription().fullDescription);
amount.setText(entry.getAmount().toFormattedString());
date.setText(entry.getDate().toString());
tags.getChildren().add(new Label(entry.getTag().tagName));
tags.getChildren().add(new Label(entry.getTag().getTagName()));
// entry.getTag().stream()
// .sorted(Comparator.comparing(tag -> tag.tagName))
// .forEach(tag -> tags.getChildren().add(new Label(tag.tagName)));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/view/Extensions.css
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
}

#resultDisplay {
-fx-font-size: 12px;
-fx-font-size: 15px;
-fx-font-family: "Poppins Regular";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void parse_missingParts_throwsParseException() {
parser,
"",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE));

assertParseFailure(
parser,
TYPE_MONTH,
Expand Down
18 changes: 4 additions & 14 deletions src/test/java/seedu/pennywise/model/PennyWiseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.pennywise.logic.commands.CommandTestUtil.VALID_AMT_DINNER;
import static seedu.pennywise.logic.commands.CommandTestUtil.VALID_AMT_INVESTMENT;
import static seedu.pennywise.logic.commands.CommandTestUtil.VALID_TAG_DINNER;
import static seedu.pennywise.logic.commands.CommandTestUtil.VALID_TAG_INVESTMENT;
import static seedu.pennywise.testutil.Assert.assertThrows;
import static seedu.pennywise.testutil.TypicalEntry.ALLOWANCE;
import static seedu.pennywise.testutil.TypicalEntry.LUNCH;
Expand Down Expand Up @@ -53,16 +49,10 @@ public void resetData_withValidReadOnlyPennyWise_replacesData() {
@Test
public void resetData_withDuplicateExpenditure_throwsDuplicateEntryException() {
// Two entries with the same identity fields
Entry editedLunch = new ExpenditureBuilder(LUNCH)
.withAmount(VALID_AMT_DINNER)
.withTag(VALID_TAG_DINNER)
.build();
Entry editedAllowance = new IncomeBuilder(ALLOWANCE)
.withAmount(VALID_AMT_INVESTMENT)
.withTag(VALID_TAG_INVESTMENT)
.build();
List<Entry> newExpenditures = Arrays.asList(LUNCH, editedLunch);
List<Entry> newIncomes = Arrays.asList(ALLOWANCE, editedAllowance);
Entry lunchCopy = new ExpenditureBuilder(LUNCH).build();
Entry allowanceCopy = new IncomeBuilder(ALLOWANCE).build();
List<Entry> newExpenditures = Arrays.asList(LUNCH, lunchCopy);
List<Entry> newIncomes = Arrays.asList(ALLOWANCE, allowanceCopy);
PennyWiseStub newData = new PennyWiseStub(newExpenditures, newIncomes);
assertThrows(DuplicateEntryException.class, () -> pennyWise.resetData(newData));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class JavaAdaptedIncomeTest {
private static final String VALID_DESCRIPTION = ALLOWANCE.getDescription().fullDescription;
private static final String VALID_AMOUNT = ALLOWANCE.getAmount().toString();
private static final String VALID_DATE = ALLOWANCE.getDate().toString();
private static final String VALID_TAG = ALLOWANCE.getTag().tagName;
private static final String VALID_TAG = ALLOWANCE.getTag().getTagName();


@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class JsonAdaptedExpenditureTest {
private static final String VALID_DESCRIPTION = LUNCH.getDescription().fullDescription;
private static final String VALID_AMOUNT = LUNCH.getAmount().toString();
private static final String VALID_DATE = LUNCH.getDate().toString();
private static final String VALID_TAG = LUNCH.getTag().tagName;
private static final String VALID_TAG = LUNCH.getTag().getTagName();


@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/seedu/pennywise/testutil/EntryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static String getEditEntryDescriptorDetails(EditCommand.EditEntryDescript
descriptor.getDate().ifPresent(date -> sb.append(PREFIX_DATE).append(date).append(" "));
descriptor.getDescription().ifPresent(description -> sb.append(PREFIX_DESCRIPTION)
.append(description.fullDescription).append(" "));
descriptor.getTag().ifPresent(tag -> sb.append(PREFIX_TAG).append(tag.tagName).append(" "));
descriptor.getTag().ifPresent(tag -> sb.append(PREFIX_TAG).append(tag.getTagName()).append(" "));
return sb.toString();
}

Expand Down

0 comments on commit c89ddcd

Please sign in to comment.