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

Add new flag for viewloan and merge viewloans into viewloan #91

Merged
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
19 changes: 14 additions & 5 deletions src/main/java/seedu/address/logic/commands/ViewLoanCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
/**
* Represents a command to view the loans associated with a contact.
*/
public class ViewLoanCommand extends Command {
public static final String COMMAND_WORD = "viewloan";

public class ViewLoanCommand extends ViewLoanRelatedCommand {
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": View loans associated with the person identified by the index used in the displayed person list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
Expand All @@ -25,7 +23,14 @@
public static final String MESSAGE_SUCCESS = "Listed all loans associated with: %1$s";
private final Index targetIndex;

public ViewLoanCommand(Index targetIndex) {
/**
* Creates a ViewLoanCommand to view the loans associated with the person at the specified {@code Index}.
*
* @param targetIndex The index of the person to view loans for.
* @param isShowAllLoans Whether to show all loans or only active loans.
*/
public ViewLoanCommand(Index targetIndex, boolean isShowAllLoans) {
super(isShowAllLoans);

Check warning on line 33 in src/main/java/seedu/address/logic/commands/ViewLoanCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/ViewLoanCommand.java#L33

Added line #L33 was not covered by tests
this.targetIndex = targetIndex;
}

Expand All @@ -40,7 +45,11 @@

Person personToShowLoan = lastShownList.get(targetIndex.getZeroBased());
model.updateFilteredPersonList(person -> person.equals(personToShowLoan));
model.updateFilteredLoanList(loan -> loan.isAssignedTo(personToShowLoan) && loan.isActive());
if (isShowAllLoans) {
model.updateFilteredLoanList(loan -> loan.isAssignedTo(personToShowLoan));

Check warning on line 49 in src/main/java/seedu/address/logic/commands/ViewLoanCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/ViewLoanCommand.java#L49

Added line #L49 was not covered by tests
} else {
model.updateFilteredLoanList(loan -> loan.isAssignedTo(personToShowLoan) && loan.isActive());
}
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(personToShowLoan)),
false, false, true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package seedu.address.logic.commands;

/**
* Represents a command that is related to viewing loans.
*/
public abstract class ViewLoanRelatedCommand extends Command {
public static final String COMMAND_WORD = "viewloan";
final boolean isShowAllLoans;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: might be better to use explicit access modifier


protected ViewLoanRelatedCommand(boolean isShowAllLoans) {
this.isShowAllLoans = isShowAllLoans;
}

Check warning on line 12 in src/main/java/seedu/address/logic/commands/ViewLoanRelatedCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/ViewLoanRelatedCommand.java#L10-L12

Added lines #L10 - L12 were not covered by tests
}
12 changes: 10 additions & 2 deletions src/main/java/seedu/address/logic/commands/ViewLoansCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_ACTIVE_LOANS;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_LOANS;
import static seedu.address.model.Model.PREDICATE_SHOW_NO_PERSON;

import seedu.address.model.Model;

/**
* Lists all active loans in the address book to the user.
*/
public class ViewLoansCommand extends Command {
public class ViewLoansCommand extends ViewLoanRelatedCommand {
public static final String COMMAND_WORD = "viewloans";

public static final String MESSAGE_SUCCESS = "Listed all loans";

public ViewLoansCommand(boolean isShowAllLoans) {
super(isShowAllLoans);
}

Check warning on line 20 in src/main/java/seedu/address/logic/commands/ViewLoansCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/ViewLoansCommand.java#L19-L20

Added lines #L19 - L20 were not covered by tests

@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(PREDICATE_SHOW_NO_PERSON);
model.updateFilteredLoanList(PREDICATE_SHOW_ALL_ACTIVE_LOANS);
if (isShowAllLoans) {
model.updateFilteredLoanList(PREDICATE_SHOW_ALL_LOANS);

Check warning on line 27 in src/main/java/seedu/address/logic/commands/ViewLoansCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/ViewLoansCommand.java#L27

Added line #L27 was not covered by tests
} else {
model.updateFilteredLoanList(PREDICATE_SHOW_ALL_ACTIVE_LOANS);

Check warning on line 29 in src/main/java/seedu/address/logic/commands/ViewLoansCommand.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/commands/ViewLoansCommand.java#L29

Added line #L29 was not covered by tests
}
return new CommandResult(MESSAGE_SUCCESS, false, false, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import seedu.address.logic.commands.LinkLoanCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.MarkLoanCommand;
import seedu.address.logic.commands.ViewLoanCommand;
import seedu.address.logic.commands.ViewLoansCommand;
import seedu.address.logic.commands.ViewLoanRelatedCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -88,15 +87,12 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case ViewLoanCommand.COMMAND_WORD:
case ViewLoanRelatedCommand.COMMAND_WORD:
return new ViewLoanCommandParser().parse(arguments);

case MarkLoanCommand.COMMAND_WORD:
return new MarkLoanCommandParser().parse(arguments);

case ViewLoansCommand.COMMAND_WORD:
return new ViewLoansCommand();

case AnalyticsCommand.COMMAND_WORD:
return new AnalyticsCommandParser().parse(arguments);

Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public class CliSyntax {
public static final Prefix PREFIX_START_DATE = new Prefix("s/");
public static final Prefix PREFIX_RETURN_DATE = new Prefix("r/");
public static final Prefix PREFIX_LOAN_INDEX = new Prefix("l/");
public static final String FLAG_SHOW_ALL_LOANS = "-a";
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.FLAG_SHOW_ALL_LOANS;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.ViewLoanCommand;
import seedu.address.logic.commands.ViewLoanRelatedCommand;
import seedu.address.logic.commands.ViewLoansCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new ViewLoanCommand object
*/
public class ViewLoanCommandParser implements Parser<ViewLoanCommand> {
public class ViewLoanCommandParser implements Parser<ViewLoanRelatedCommand> {

Check warning on line 15 in src/main/java/seedu/address/logic/parser/ViewLoanCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/ViewLoanCommandParser.java#L15

Added line #L15 was not covered by tests

/**
* Parses the given {@code String} of arguments in the context of the ViewLoanCommand
* and returns a ViewLoanCommand object for execution.
* and returns a ViewLoanRelatedCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ViewLoanCommand parse(String args) throws ParseException {
public ViewLoanRelatedCommand parse(String args) throws ParseException {
boolean isShowAllLoans = false;
String modifiedArgs = args;

Check warning on line 24 in src/main/java/seedu/address/logic/parser/ViewLoanCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/ViewLoanCommandParser.java#L23-L24

Added lines #L23 - L24 were not covered by tests

// Check if the user wants to view all loans, active or not
if (modifiedArgs.contains(FLAG_SHOW_ALL_LOANS)) {
isShowAllLoans = true;
modifiedArgs = modifiedArgs.replace(FLAG_SHOW_ALL_LOANS, "").trim();

Check warning on line 29 in src/main/java/seedu/address/logic/parser/ViewLoanCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/ViewLoanCommandParser.java#L28-L29

Added lines #L28 - L29 were not covered by tests
}

// Check if the user wants to view all loans, i.e. no index is provided
if (modifiedArgs.isEmpty()) {
return new ViewLoansCommand(isShowAllLoans);

Check warning on line 34 in src/main/java/seedu/address/logic/parser/ViewLoanCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/ViewLoanCommandParser.java#L34

Added line #L34 was not covered by tests
}

// Check if the user wants to view a specific person's loans
try {
Index index = ParserUtil.parseIndex(args);
return new ViewLoanCommand(index);
Index index = ParserUtil.parseIndex(modifiedArgs);
return new ViewLoanCommand(index, isShowAllLoans);

Check warning on line 40 in src/main/java/seedu/address/logic/parser/ViewLoanCommandParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/logic/parser/ViewLoanCommandParser.java#L39-L40

Added lines #L39 - L40 were not covered by tests
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewLoanCommand.MESSAGE_USAGE), pe);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public interface Model {
*/
Predicate<Person> PREDICATE_SHOW_NO_PERSON = unused -> false;

/**
* {@code Predicate} that always evaluate to true
*/
Predicate<Loan> PREDICATE_SHOW_ALL_LOANS = unused -> true;

/**
* {@code Predicate} that evaluates to true if the loan is active
*/
Expand Down
Loading