Skip to content

Commit

Permalink
Make date optional and fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Cary-Xx committed Oct 14, 2019
1 parent f705ff8 commit c1b08ec
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected Config initConfig(Path configFilePath) {
initializedConfig = configOptional.orElse(new Config());
} catch (DataConversionException e) {
logger.warning("Config file at " + configFilePathUsed + " is not in the correct format. "
+ "Using default config properties");
+ "Using default config properties");
initializedConfig = new Config();
}

Expand Down Expand Up @@ -148,7 +148,7 @@ protected UserPrefs initPrefs(UserPrefsStorage storage) {
initializedPrefs = prefsOptional.orElse(new UserPrefs());
} catch (DataConversionException e) {
logger.warning("UserPrefs file at " + prefsFilePath + " is not in the correct format. "
+ "Using default user prefs");
+ "Using default user prefs");
initializedPrefs = new UserPrefs();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty ExpenseList");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public class Messages {
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_EXPENSE_DISPLAYED_INDEX = "The expense index provided is invalid";
public static final String MESSAGE_EXPENSES_LISTED_OVERVIEW = "%1$d expenses listed!";
public static final String MESSAGE_EXPENSE_LISTED_OVERVIEW = "%1$d expense listed!";

}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AddCommand extends Command {
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_AMOUNT + "AMOUNT "
+ PREFIX_DATE + "DATE "
+ "[" + PREFIX_DATE + "DATE] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "Textbook "
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class FindCommand extends Command {
public static final String COMMAND_WORD = "find";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all expenses whose names contain any of "
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " coffee";
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " coffee";

private final NameContainsKeywordsPredicate predicate;

Expand All @@ -29,14 +29,16 @@ public FindCommand(NameContainsKeywordsPredicate predicate) {
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredExpenseList(predicate);
int size = model.getFilteredExpenseList().size();
return new CommandResult(
String.format(Messages.MESSAGE_EXPENSES_LISTED_OVERVIEW, model.getFilteredExpenseList().size()));
String.format(size <= 1 ? Messages.MESSAGE_EXPENSE_LISTED_OVERVIEW
: Messages.MESSAGE_EXPENSES_LISTED_OVERVIEW, size));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof FindCommand // instanceof handles nulls
&& predicate.equals(((FindCommand) other).predicate)); // state check
|| (other instanceof FindCommand // instanceof handles nulls
&& predicate.equals(((FindCommand) other).predicate)); // state check
}
}
17 changes: 15 additions & 2 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

Expand All @@ -32,14 +35,24 @@ public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_AMOUNT, PREFIX_DATE, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_AMOUNT, PREFIX_DATE)
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_AMOUNT)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}

Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Amount amount = ParserUtil.parseAmount(argMultimap.getValue(PREFIX_AMOUNT).get());
Date date = ParserUtil.parseDate(argMultimap.getValue(PREFIX_DATE).get());

Optional<String> dateField = argMultimap.getValue(PREFIX_DATE);
Date date;
if (!dateField.isPresent()) {
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HHmm");
String dateS = currentDateTime.format(formatter);
date = ParserUtil.parseDate(dateS);
} else {
date = ParserUtil.parseDate(dateField.get());
}
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Expense expense = new Expense(name, amount, date, tagList);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static Date parseDate(String date) throws ParseException {
if (!Date.isValidDate(trimmedDate)) {
throw new ParseException(Date.MESSAGE_CONSTRAINTS);
}
return new Date(trimmedDate, true);
return new Date(trimmedDate);
}

/**
Expand Down
28 changes: 24 additions & 4 deletions src/main/java/seedu/address/model/expense/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,35 @@ public class Date {

/**
* Constructs an {@code Date}.
* @param date A valid date.
* @param ifConverted
*/
public Date() {
rawValue = null;
value = null;
}

/**
* Constructs an {@code Date}.
*
* @param date A valid date.
*/
public Date(String date, boolean ifConverted) {
public Date(String date) {
requireNonNull(date);
checkArgument(isValidDate(date), MESSAGE_CONSTRAINTS);
rawValue = date;
value = ifConverted ? convertDate(date) : date;
value = convertDate(date);
}

/**
* Constructs an {@code Date}.
*
* @param rawDate A valid rawDate.
* @param ifConverted Status to determine whether to convert the date
*/
public Date(String rawDate, boolean ifConverted) {
requireNonNull(rawDate);
checkArgument(isValidDate(rawDate), MESSAGE_CONSTRAINTS);
rawValue = rawDate;
value = ifConverted ? convertDate(rawDate) : rawDate;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/expense/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static seedu.address.commons.util.AppUtil.checkArgument;

/**
* Represents the description of an expense in the MYMorise.
* Represents the description of an expense in the expense list.
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
*/
public class Name {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Expense toModelType() throws IllegalValueException {
if (!Date.isValidDate(date)) {
throw new IllegalValueException(Date.MESSAGE_CONSTRAINTS);
}
final Date modelDate = new Date(date, true);
final Date modelDate = new Date(date);

final Set<Tag> modelTags = new HashSet<>(expenseTags);
return new Expense(modelName, modelAmount, modelDate, modelTags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.commons.core.Messages.MESSAGE_EXPENSES_LISTED_OVERVIEW;
import static seedu.address.commons.core.Messages.MESSAGE_EXPENSE_LISTED_OVERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalExpenses.CHRISTMAS;
import static seedu.address.testutil.TypicalExpenses.SHOPPING;
Expand All @@ -24,15 +25,16 @@
* Contains integration tests (interaction with the Model) for {@code FindCommand}.
*/
public class FindCommandTest {

private Model model = new ModelManager(getTypicalExpenseList(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalExpenseList(), new UserPrefs());

@Test
public void equals() {
NameContainsKeywordsPredicate firstPredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("first"));
new NameContainsKeywordsPredicate(Collections.singletonList("first"));
NameContainsKeywordsPredicate secondPredicate =
new NameContainsKeywordsPredicate(Collections.singletonList("second"));
new NameContainsKeywordsPredicate(Collections.singletonList("second"));

FindCommand findFirstCommand = new FindCommand(firstPredicate);
FindCommand findSecondCommand = new FindCommand(secondPredicate);
Expand All @@ -56,7 +58,7 @@ public void equals() {

@Test
public void execute_zeroKeywords_noExpenseFound() {
String expectedMessage = String.format(MESSAGE_EXPENSES_LISTED_OVERVIEW, 0);
String expectedMessage = String.format(MESSAGE_EXPENSE_LISTED_OVERVIEW, 0);
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredExpenseList(predicate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ public void parse_compulsoryFieldMissing_failure() {
assertParseFailure(parser, NAME_DESC_RUM + VALID_AMOUNT_RUM + DATE_DESC_RUM,
expectedMessage);

// missing date prefix
assertParseFailure(parser, NAME_DESC_RUM + AMOUNT_DESC_RUM + VALID_DATE_RUM,
expectedMessage);

// all prefixes missing
assertParseFailure(parser, VALID_NAME_RUM + VALID_AMOUNT_RUM + VALID_DATE_RUM,
expectedMessage);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ public void parseDate_wrongValue_throwsParseException() {
@Test
public void parseDate_validValueWithWhitespace_returnsTrimmedDate() throws Exception {
String dateWithWhitespace = WHITESPACE + VALID_DATE + WHITESPACE;
Date expectedDate = new Date(VALID_DATE, true);
Date expectedDate = new Date(VALID_DATE);
assertEquals(expectedDate, ParserUtil.parseDate(dateWithWhitespace));
}

@Test
public void parseDate_validValueWithoutWhitespace_returnsConvertedDate() throws Exception {
Date expectedDate = new Date(VALID_DATE, true);
Date expectedDate = new Date(VALID_DATE);
assertEquals(expectedDate, ParserUtil.parseDate(VALID_DATE));
}

Expand Down
1 change: 1 addition & 0 deletions src/test/java/seedu/address/model/expense/AmountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void isValidAmount() {

// valid amounts
assertTrue(Amount.isValidAmount("1")); // exactly 1 digit
assertTrue(Amount.isValidAmount("1.1")); // exactly 1 decimal
assertTrue(Amount.isValidAmount("1.15")); // exactly 2 decimals
assertTrue(Amount.isValidAmount("1472745.15")); // less than 12 digits and exactly 2 decimals
assertTrue(Amount.isValidAmount("93121534")); // less than 12 digits
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/seedu/address/model/expense/DateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public class DateTest {

@Test
public void constructor_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new Date(null, false));
assertThrows(NullPointerException.class, () -> new Date(null));
}

@Test
public void constructor_invalidDate_throwsIllegalArgumentException() {
String invalidDate = "";
assertThrows(IllegalArgumentException.class, () -> new Date(invalidDate, false));
assertThrows(IllegalArgumentException.class, () -> new Date(invalidDate));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public EditExpenseDescriptorBuilder withAmount(String amount) {
* Sets the {@code Date} of the {@code EditExpenseDescriptor} that we are building.
*/
public EditExpenseDescriptorBuilder withDate(String date) {
descriptor.setDate(new Date(date, true));
descriptor.setDate(new Date(date));
return this;
}

Expand Down
15 changes: 11 additions & 4 deletions src/test/java/seedu/address/testutil/ExpenseBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ExpenseBuilder {
public ExpenseBuilder() {
name = new Name(DEFAULT_NAME);
amount = new Amount(DEFAULT_AMOUNT);
date = new Date(DEFAULT_DATE, true);
date = new Date(DEFAULT_DATE);
tags = new HashSet<>();
}

Expand All @@ -52,7 +52,7 @@ public ExpenseBuilder withName(String name) {
/**
* Parses the {@code tags} into a {@code Set<Tag>} and set it to the {@code Expense} that we are building.
*/
public ExpenseBuilder withTags(String ... tags) {
public ExpenseBuilder withTags(String... tags) {
this.tags = SampleDataUtil.getTagSet(tags);
return this;
}
Expand All @@ -65,16 +65,23 @@ public ExpenseBuilder withAmount(String amount) {
return this;
}

/**
* Sets empty {@code Date} of the {@code Expense} that we are building.
*/
public ExpenseBuilder withDate() {
this.date = new Date();
return this;
}

/**
* Sets the {@code Date} of the {@code Expense} that we are building.
*/
public ExpenseBuilder withDate(String date) {
this.date = new Date(date, true);
this.date = new Date(date);
return this;
}

public Expense build() {
return new Expense(name, amount, date, tags);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public RawExpenseBuilder withAmount(String amount) {
* Sets the {@code Date} of the {@code Expense} that we are building.
*/
public RawExpenseBuilder withDate(String date) {
this.date = new Date(date, true);
this.date = new Date(date);
return this;
}

Expand Down

0 comments on commit c1b08ec

Please sign in to comment.