forked from nus-cs2103-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix-ug-issues
- Loading branch information
Showing
18 changed files
with
885 additions
and
16 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/main/java/loanbook/logic/commands/DeleteBikeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
151
src/main/java/loanbook/logic/commands/EditBikeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/loanbook/logic/parser/DeleteBikeCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
src/main/java/loanbook/logic/parser/EditBikeCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.