Skip to content

Commit

Permalink
Merge branch 'master' into fix-ug-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeJuice7 committed Nov 4, 2018
2 parents 0186b75 + bd79d73 commit b08cf40
Show file tree
Hide file tree
Showing 18 changed files with 885 additions and 16 deletions.
77 changes: 77 additions & 0 deletions src/main/java/loanbook/logic/commands/DeleteBikeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package loanbook.logic.commands;

import static java.util.Objects.requireNonNull;
import static loanbook.logic.parser.CliSyntax.PREFIX_NAME;
import static loanbook.logic.parser.CliSyntax.PREFIX_PASSWORD;

import java.util.Optional;

import loanbook.commons.core.Messages;
import loanbook.logic.CommandHistory;
import loanbook.logic.commands.exceptions.CommandException;
import loanbook.model.Model;
import loanbook.model.Password;
import loanbook.model.bike.Bike;
import loanbook.model.loan.Name;

/**
* Deletes a bike identified using it's displayed index from the loan book.
*/
public class DeleteBikeCommand extends Command {

public static final String COMMAND_WORD = "deletebike";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the bike with the given name.\n"
+ "Requires a password for verification.\n"
+ "Parameters: " + PREFIX_NAME + "NAME " + PREFIX_PASSWORD + "PASSWORD\n"
+ "Example: " + COMMAND_WORD + " " + PREFIX_NAME + "Bike001 " + PREFIX_PASSWORD + "a12345";

public static final String MESSAGE_DELETE_BIKE_SUCCESS = "Deleted Bike: %1$s";
public static final String MESSAGE_BIKE_NOT_FOUND = "No bike with that name exists within the loan book";

private final Name bikeName;
private final Password targetPassword;

public DeleteBikeCommand(Name bikeName, Password pass) {
this.bikeName = bikeName;
targetPassword = pass;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {

requireNonNull(model);

Optional<Bike> actualBike = model.getBike(bikeName.value);
if (!actualBike.isPresent()) {
throw new CommandException(MESSAGE_BIKE_NOT_FOUND);
}

if (!Password.isSamePassword(model.getPass(), targetPassword)) {
throw new CommandException(Messages.MESSAGE_INVALID_PASSWORD);
}

model.deleteBike(actualBike.get());
model.commitLoanBook();
return new CommandResult(String.format(MESSAGE_DELETE_BIKE_SUCCESS, actualBike.get()));
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof DeleteBikeCommand)) {
return false;
}

// state check
DeleteBikeCommand otherCommand = (DeleteBikeCommand) other;
return bikeName.equals(otherCommand.bikeName)
&& targetPassword.equals(otherCommand.targetPassword);
}
}
151 changes: 151 additions & 0 deletions src/main/java/loanbook/logic/commands/EditBikeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package loanbook.logic.commands;

import static java.util.Objects.requireNonNull;
import static loanbook.logic.parser.CliSyntax.PREFIX_NAME;
import static loanbook.model.Model.PREDICATE_SHOW_ALL_BIKES;

import java.util.Optional;

import loanbook.commons.util.CollectionUtil;
import loanbook.logic.CommandHistory;
import loanbook.logic.commands.exceptions.CommandException;
import loanbook.model.Model;
import loanbook.model.bike.Bike;
import loanbook.model.loan.Name;

/**
* Edits the details of an existing bike in the loan book.
*/
public class EditBikeCommand extends Command {

public static final String COMMAND_WORD = "editbike";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the bike with the given name. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: NAME "
+ "[" + PREFIX_NAME + "NAME]\n"
+ "Example: " + COMMAND_WORD + " Bike001 "
+ PREFIX_NAME + "Bike002";

public static final String MESSAGE_EDIT_BIKE_SUCCESS = "Edited bike: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_BIKE_NOT_FOUND = "No bike with that name exists within the loan book.";

private final Name bikeName;
private final EditBikeDescriptor editBikeDescriptor;

/**
* @param bikeName The name of the bike in the filtered bike list to edit
* @param editBikeDescriptor details to edit the bike with
*/
public EditBikeCommand(Name bikeName, EditBikeDescriptor editBikeDescriptor) {
requireNonNull(bikeName);
requireNonNull(editBikeDescriptor);

this.bikeName = bikeName;
this.editBikeDescriptor = new EditBikeDescriptor(editBikeDescriptor);
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);

Optional<Bike> bikeToEdit = model.getBike(bikeName.value);
if (!bikeToEdit.isPresent()) {
throw new CommandException(MESSAGE_BIKE_NOT_FOUND);
}

Bike editedBike = createEditedBike(bikeToEdit.get(), editBikeDescriptor, model);

model.updateBike(bikeToEdit.get(), editedBike);
model.updateFilteredBikeList(PREDICATE_SHOW_ALL_BIKES);
model.commitLoanBook();
return new CommandResult(String.format(MESSAGE_EDIT_BIKE_SUCCESS, editedBike));
}

/**
* Creates and returns a {@code Bike} with the details of {@code bikeToEdit}
* edited with {@code editBikeDescriptor}.
*/
private static Bike createEditedBike(
Bike bikeToEdit,
EditBikeDescriptor editBikeDescriptor,
Model model) throws CommandException {
assert bikeToEdit != null;

Name updatedName = editBikeDescriptor.getName().orElse(bikeToEdit.getName());

return new Bike(
updatedName
);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EditBikeCommand)) {
return false;
}

// state check
EditBikeCommand e = (EditBikeCommand) other;
return bikeName.equals(e.bikeName)
&& editBikeDescriptor.equals(e.editBikeDescriptor);
}

/**
* Stores the details to edit the bike with. Each non-empty field value will replace the
* corresponding field value of the bike.
*/
public static class EditBikeDescriptor {
private Name name;

public EditBikeDescriptor() {}

/**
* Copy constructor.
* A defensive copy of {@code tags} is used internally.
*/
public EditBikeDescriptor(EditBikeDescriptor toCopy) {
setName(toCopy.name);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name);
}

public void setName(Name name) {
this.name = name;
}

public Optional<Name> getName() {
return Optional.ofNullable(name);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EditBikeDescriptor)) {
return false;
}

// state check
EditBikeDescriptor e = (EditBikeDescriptor) other;

return getName().equals(e.getName());
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/loanbook/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public FindCommand(NameContainsKeywordsPredicate predicate) {
@Override
public CommandResult execute(Model model, CommandHistory history) {
requireNonNull(model);
model.updateFilteredLoanList(predicate);
model.updateFilteredLoanList(predicate.forLoans());
return new CommandResult(
String.format(Messages.MESSAGE_LOANS_LISTED_OVERVIEW, model.getFilteredLoanList().size()));
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/loanbook/logic/parser/DeleteBikeCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package loanbook.logic.parser;

import static loanbook.logic.parser.CliSyntax.PREFIX_NAME;
import static loanbook.logic.parser.CliSyntax.PREFIX_PASSWORD;

import java.util.List;

import loanbook.logic.commands.DeleteBikeCommand;
import loanbook.logic.parser.exceptions.ParseException;
import loanbook.model.Password;
import loanbook.model.loan.Name;

/**
* Parses input arguments and creates a new DeleteBikeCommand object.
*/
public class DeleteBikeCommandParser extends ArgumentParser<DeleteBikeCommand> {

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

Name bikeName = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Password password = ParserUtil.parsePass(argMultimap.getValue(PREFIX_PASSWORD).get());

return new DeleteBikeCommand(bikeName, password);
}

}
47 changes: 47 additions & 0 deletions src/main/java/loanbook/logic/parser/EditBikeCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package loanbook.logic.parser;

import static java.util.Objects.requireNonNull;
import static loanbook.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static loanbook.logic.parser.CliSyntax.PREFIX_NAME;

import loanbook.logic.commands.EditBikeCommand;
import loanbook.logic.commands.EditBikeCommand.EditBikeDescriptor;
import loanbook.logic.parser.exceptions.ParseException;
import loanbook.model.loan.Name;

/**
* Parses input arguments and creates a new EditCommand object
*/
public class EditBikeCommandParser implements Parser<EditBikeCommand> {

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

Name bikeName;

try {
bikeName = ParserUtil.parseName(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditBikeCommand.MESSAGE_USAGE), pe);
}

EditBikeDescriptor editBikeDescriptor = new EditBikeDescriptor();
if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
editBikeDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()));
}

if (!editBikeDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditBikeCommand.MESSAGE_NOT_EDITED);
}

return new EditBikeCommand(bikeName, editBikeDescriptor);
}

}
8 changes: 8 additions & 0 deletions src/main/java/loanbook/logic/parser/LoanBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import loanbook.logic.commands.CheckEmailCommand;
import loanbook.logic.commands.ClearCommand;
import loanbook.logic.commands.Command;
import loanbook.logic.commands.DeleteBikeCommand;
import loanbook.logic.commands.DeleteCommand;
import loanbook.logic.commands.EditBikeCommand;
import loanbook.logic.commands.EditCommand;
import loanbook.logic.commands.ExitCommand;
import loanbook.logic.commands.FindCommand;
Expand Down Expand Up @@ -62,6 +64,9 @@ public Command parseCommand(String userInput) throws ParseException {
case AddCommand.COMMAND_WORD:
return new AddCommandParser().parse(arguments);

case EditBikeCommand.COMMAND_WORD:
return new EditBikeCommandParser().parse(arguments);

case EditCommand.COMMAND_WORD:
return new EditCommandParser().parse(arguments);

Expand All @@ -71,6 +76,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ReturnCommand.COMMAND_WORD:
return new ReturnCommandParser().parse(arguments);

case DeleteBikeCommand.COMMAND_WORD:
return new DeleteBikeCommandParser().parse(arguments);

case DeleteCommand.COMMAND_WORD:
return new DeleteCommandParser().parse(arguments);

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/loanbook/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,14 @@ public boolean equals(Object obj) {
&& filteredLoans.equals(other.filteredLoans);
}

@Override
public String toString() {
return logger
+ ", " + versionedLoanBook
+ ", " + filteredBikes
+ ", " + filteredLoans
+ ", " + preference;

}

}
Loading

0 comments on commit b08cf40

Please sign in to comment.