Skip to content

Commit

Permalink
Merge branch 'master' into add-status-to-tag
Browse files Browse the repository at this point in the history
  • Loading branch information
xantho09 committed Nov 8, 2018
2 parents 06eede3 + 7e13f28 commit 6731af1
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 244 deletions.
2 changes: 1 addition & 1 deletion src/main/java/loanbook/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Messages {
+ "Please make sure you have network connected!";
public static final String MESSAGE_BAD_RUNTIME = "You execute your code in a Java runtime"
+ " that does not support UTF-8!";
public static final String MESSAGE_INVALID_INFO = "No loan contains both %1$s and %2$s!";
public static final String MESSAGE_INVALID_INFO = "There is no Loan with this id in the LoanBook!";
public static final String MESSAGE_LOAN_IS_DONE = "You do not need to send reminder, because the loan is %s";
public static final String MESSAGE_WRONG_OLDEMAIL = "The old email address is wrong!";
public static final String MESSAGE_DUPLICATE_FAILURE = "The old email and the new email cannot be the same!";
Expand Down
59 changes: 18 additions & 41 deletions src/main/java/loanbook/logic/commands/RemindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import static java.util.Objects.requireNonNull;
import static loanbook.commons.util.CollectionUtil.requireAllNonNull;
import static loanbook.logic.parser.CliSyntax.PREFIX_BIKE;
import static loanbook.logic.parser.CliSyntax.PREFIX_NAME;
import static loanbook.logic.parser.CliSyntax.PREFIX_PASSWORD;
import static loanbook.logic.parser.CliSyntax.PREFIX_EMAILPW;
import static loanbook.logic.parser.CliSyntax.PREFIX_ID;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Optional;

import javax.mail.AuthenticationFailedException;
import javax.mail.MessagingException;
Expand All @@ -17,10 +16,9 @@
import loanbook.logic.SendReminder;
import loanbook.logic.commands.exceptions.CommandException;
import loanbook.model.Model;
import loanbook.model.bike.Bike;
import loanbook.model.loan.Loan;
import loanbook.model.loan.LoanId;
import loanbook.model.loan.LoanStatus;
import loanbook.model.loan.Name;

/**
* Send a reminder email to the customer.
Expand All @@ -31,49 +29,41 @@ public class RemindCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Send a reminder email to the customer.\n"
+ "Parameter: "
+ PREFIX_PASSWORD + "EMAILPASSWORD "
+ PREFIX_NAME + "NAME "
+ PREFIX_BIKE + "BIKE\n"
+ PREFIX_EMAILPW + "EMAILPASSWORD "
+ PREFIX_ID + "ID "
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_PASSWORD + "samplepassword "
+ PREFIX_NAME + "John Doe "
+ PREFIX_BIKE + "BIKE001";
+ PREFIX_EMAILPW + "samplepassword "
+ PREFIX_ID + "0 ";

public static final String MESSAGE_SUCCESS = "Email sent!";

private final Name name;
private final Bike bike;
private final LoanId id;
private final String emailPassword;

/**
* Creates an RemindCommand to send a reminder email to customer's {@code Email}
* according to the {@code Name} and {@code BikeId} provided.
*/
public RemindCommand(String emailPassword, Name name, Bike bike) {
requireAllNonNull(emailPassword, name, bike);
this.name = name;
this.bike = bike;
public RemindCommand(String emailPassword, LoanId id) {
requireAllNonNull(emailPassword, id);
this.id = id;
this.emailPassword = emailPassword;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
List<Loan> lastShownList = model.getLoanBook().getLoanList();
Optional<Loan> targetLoan = model.getLoanById(id);

Loan targetLoan = getLoan(lastShownList, name, bike);

if (targetLoan == null) {
throw new CommandException(String.format(Messages.MESSAGE_INVALID_INFO, name, bike.getName()));
if (!targetLoan.isPresent()) {
throw new CommandException(Messages.MESSAGE_INVALID_INFO);
}

if (targetLoan.getLoanStatus().equals(LoanStatus.RETURNED)) {
if (targetLoan.get().getLoanStatus().equals(LoanStatus.RETURNED)) {
throw new CommandException(String.format(Messages.MESSAGE_LOAN_IS_DONE, LoanStatus.RETURNED.toString()));
} else if (targetLoan.getLoanStatus().equals(LoanStatus.DELETED)) {
throw new CommandException(String.format(Messages.MESSAGE_LOAN_IS_DONE, LoanStatus.DELETED.toString()));
}

SendReminder sendReminder = new SendReminder(model, emailPassword, targetLoan);
SendReminder sendReminder = new SendReminder(model, emailPassword, targetLoan.get());

try {
sendReminder.send();
Expand All @@ -87,24 +77,11 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
}
}

/**
* Returns the {@code Loan} Object with both the target name and bike id.
*/
private Loan getLoan(List<Loan> loanList, Name name, Bike bike) {
for (Loan loan: loanList) {
if (loan.getName().equals(name) && loan.getBike().equals(bike)) {
return loan;
}
}
return null;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof RemindCommand // instanceof handles nulls
&& emailPassword.equals(((RemindCommand) other).emailPassword)
&& name.equals(((RemindCommand) other).name)
&& bike.equals(((RemindCommand) other).bike)); // state check
&& id.equals(((RemindCommand) other).id)); // state check
}
}
3 changes: 2 additions & 1 deletion src/main/java/loanbook/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public class CliSyntax {
public static final Prefix PREFIX_PASSWORD = new Prefix("x/");
public static final Prefix PREFIX_PHONE = new Prefix("p/");
public static final Prefix PREFIX_TAG = new Prefix("t/");

public static final Prefix PREFIX_ID = new Prefix("id/");
public static final Prefix PREFIX_EMAILPW = new Prefix("pw/");
}
30 changes: 15 additions & 15 deletions src/main/java/loanbook/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ public static Index parseIndex(String oneBasedIndex) throws ParseException {
return Index.fromOneBased(Integer.parseInt(trimmedIndex));
}

/**
* Parses a {@code String id} into a {@code LoanId}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code id} is invalid.
*/
public static LoanId parseLoanId(String id) throws ParseException {
requireNonNull(id);
String trimmedLoanId = id.trim();
if (!LoanId.isValidLoanId(trimmedLoanId)) {
throw new ParseException(LoanId.MESSAGE_LOANID_CONSTRAINTS);
}
return new LoanId(trimmedLoanId);
}

/**
* Parses a {@code String name} into a {@code Name}.
* Leading and trailing whitespaces will be trimmed.
Expand Down Expand Up @@ -146,21 +161,6 @@ public static LoanRate parseLoanRate(String rate) throws ParseException {
return new LoanRate(trimmedLoanRate);
}

/**
* Parses a {@code String loanId} into a {@code LoanID}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code loanId} is invalid.
*/
public static LoanId parseLoanId(String loanId) throws ParseException {
requireNonNull(loanId);
String trimmedLoanId = loanId.trim();
if (!LoanId.isValidLoanId(trimmedLoanId)) {
throw new ParseException(LoanId.MESSAGE_LOANID_CONSTRAINTS);
}
return new LoanId(trimmedLoanId);
}

/**
* Parses a {@code String tag} into a {@code Tag}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
41 changes: 13 additions & 28 deletions src/main/java/loanbook/logic/parser/RemindCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,33 @@
package loanbook.logic.parser;

import static loanbook.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static loanbook.logic.parser.CliSyntax.PREFIX_BIKE;
import static loanbook.logic.parser.CliSyntax.PREFIX_NAME;
import static loanbook.logic.parser.CliSyntax.PREFIX_PASSWORD;
import static loanbook.logic.parser.CliSyntax.PREFIX_EMAILPW;
import static loanbook.logic.parser.CliSyntax.PREFIX_ID;

import java.util.stream.Stream;
import java.util.List;

import loanbook.logic.commands.RemindCommand;
import loanbook.logic.parser.exceptions.ParseException;
import loanbook.model.bike.Bike;
import loanbook.model.loan.Name;
import loanbook.model.loan.LoanId;

/**
* Parses input arguments and creates a new RemindCommand object.
*/
public class RemindCommandParser implements Parser<RemindCommand> {
public class RemindCommandParser extends ArgumentParser<RemindCommand> {

/**
* Parses the given {@code String} of arguments in the context of the RemindCommand
* and returns an RemindCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
* @throws ParseException if the user input does not conform the expected format.
*/
public RemindCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_PASSWORD, PREFIX_NAME, PREFIX_BIKE);
ArgumentMultimap argMultimap = getArgumentMultimap(args,
List.of(PREFIX_EMAILPW, PREFIX_ID),
List.of(),
RemindCommand.MESSAGE_USAGE);

if (!arePrefixesPresent(argMultimap, PREFIX_PASSWORD, PREFIX_NAME, PREFIX_BIKE)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemindCommand.MESSAGE_USAGE));
}
LoanId id = ParserUtil.parseLoanId(argMultimap.getValue(PREFIX_ID).get());
String password = argMultimap.getValue(PREFIX_EMAILPW).get();

String emailPassword = argMultimap.getValue(PREFIX_PASSWORD).get();
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Bike bike = ParserUtil.parseBike(argMultimap.getValue(PREFIX_BIKE).get());

return new RemindCommand(emailPassword, name, bike);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
return new RemindCommand(password, id);
}
}
3 changes: 3 additions & 0 deletions src/test/java/loanbook/logic/commands/CommandTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static loanbook.logic.parser.CliSyntax.PREFIX_BIKE;
import static loanbook.logic.parser.CliSyntax.PREFIX_EMAIL;
import static loanbook.logic.parser.CliSyntax.PREFIX_EMAILPW;
import static loanbook.logic.parser.CliSyntax.PREFIX_LOANRATE;
import static loanbook.logic.parser.CliSyntax.PREFIX_NAME;
import static loanbook.logic.parser.CliSyntax.PREFIX_NRIC;
Expand Down Expand Up @@ -87,6 +88,8 @@ public class CommandTestUtil {
public static final String USER_EMAIL2_DESC = " " + VALID_USER_EMAIL2;
public static final String USER_EMAIL3_DESC = " " + VALID_USER_EMAIL3;
public static final String USER_EMAIL4_DESC = " " + VALID_USER_EMAIL4;
public static final String EMAILPW1_DESC = " " + PREFIX_EMAILPW + PASSWORD1;
public static final String EMAILPW2_DESC = " " + PREFIX_EMAILPW + PASSWORD2;
public static final String PASSWORD1_DESC = " " + PREFIX_PASSWORD + PASSWORD1;
public static final String PASSWORD2_DESC = " " + PREFIX_PASSWORD + PASSWORD2;

Expand Down
Loading

0 comments on commit 6731af1

Please sign in to comment.