Skip to content

Commit

Permalink
Check existing bikes when adding a loan (#135)
Browse files Browse the repository at this point in the history
* Add getBike() in UniqueBikeList

* Add getBike() to LoanBook

* Remove non-static initialization block in LoanBook.java

* Remove ModelStub from SetPasswordCommandTest.java

* Remove ModelStub from AddCommandTest.java

* Fix SetPasswordCommandTest.java

* Add getBike() to Model

* Add getBike check in AddCommand and update tests

* Update AddBikeCommand messages

* Fix ClearCommand

* Rename resetData to replaceData in Model
  • Loading branch information
OrangeJuice7 committed Oct 26, 2018
1 parent 90c3076 commit 951f7a4
Show file tree
Hide file tree
Showing 32 changed files with 370 additions and 396 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public class AddBikeCommand extends Command {

public static final String COMMAND_WORD = "addbike";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a bike to the address book. "
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a bike to the loan book. "
+ "Parameters: "
+ PREFIX_NAME + "NAME\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "Bike001";

public static final String MESSAGE_SUCCESS = "New bike added: %1$s";
public static final String MESSAGE_DUPLICATE_BIKE = "A bike with the same name already exists in the address book";
public static final String MESSAGE_DUPLICATE_BIKE = "A bike with the same name already exists in the loan book";

private final Bike toAdd;

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.Optional;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.bike.Bike;
import seedu.address.model.loan.Loan;

/**
Expand Down Expand Up @@ -45,6 +48,7 @@ public class AddCommand extends Command {

public static final String MESSAGE_SUCCESS = "New loan added: %1$s";
public static final String MESSAGE_DUPLICATE_LOAN = "This loan already exists in the loan book";
public static final String MESSAGE_BIKE_NOT_FOUND = "No bike with that name exists within the loan book";

private final Loan toAdd;

Expand All @@ -64,9 +68,15 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
throw new CommandException(MESSAGE_DUPLICATE_LOAN);
}

model.addLoan(toAdd);
Optional<Bike> actualBike = model.getBike(toAdd.getBike().getName().value);
if (!actualBike.isPresent()) {
throw new CommandException(MESSAGE_BIKE_NOT_FOUND);
}
Loan actualLoan = new Loan(toAdd, actualBike.get());

model.addLoan(actualLoan);
model.commitLoanBook();
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
return new CommandResult(String.format(MESSAGE_SUCCESS, actualLoan));
}

@Override
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import static java.util.Objects.requireNonNull;

import seedu.address.logic.CommandHistory;
import seedu.address.model.LoanBook;
import seedu.address.model.Model;

/**
* Clears the loan book.
* Clears the loans and resets the loan ID in the loan book.
*/
public class ClearCommand extends Command {

Expand All @@ -18,7 +17,7 @@ public class ClearCommand extends Command {
@Override
public CommandResult execute(Model model, CommandHistory history) {
requireNonNull(model);
model.resetData(new LoanBook());
model.resetLoans();
model.commitLoanBook();
return new CommandResult(MESSAGE_SUCCESS);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public static Bike parseBike(String bike) throws ParseException {
if (!Name.isValidName(trimmedBike)) {
throw new ParseException(Name.MESSAGE_NAME_CONSTRAINTS);
}
// return a dummy Bike object that contains the real bike's name. This will be converted to an actual
// existing Bike in AddCommand.execute().
return new Bike(new Name(trimmedBike));
}

Expand Down
56 changes: 36 additions & 20 deletions src/main/java/seedu/address/model/LoanBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.Optional;

import javafx.collections.ObservableList;
import seedu.address.model.bike.Bike;
Expand All @@ -23,27 +24,21 @@ public class LoanBook implements ReadOnlyLoanBook {
private final UniqueLoanList loans;
private final LoanIdManager loanIdManager;

/*
* The 'unusual' code block below is an non-static initialization block, sometimes used to avoid duplication
* between constructors. See https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
*
* Note that non-static init blocks are not recommended to use. There are other ways to avoid duplication
* among constructors.
/**
* Default constructor.
*/
{
public LoanBook() {
bikes = new UniqueBikeList();
loans = new UniqueLoanList();
loanIdManager = new LoanIdManager();
}

public LoanBook() {}

/**
* Creates an LoanBook using the Bikes and Loans in the {@code toBeCopied}
*/
public LoanBook(ReadOnlyLoanBook toBeCopied) {
this();
resetData(toBeCopied);
replaceData(toBeCopied);
}

//// list overwrite operations
Expand Down Expand Up @@ -72,9 +67,9 @@ public void setLoanIdManager(LoanIdManager loanIdManager) {
}

/**
* Resets the existing data of this {@code LoanBook} with {@code newData}.
* Replaces the existing data of this {@code LoanBook} with {@code newData}.
*/
public void resetData(ReadOnlyLoanBook newData) {
public void replaceData(ReadOnlyLoanBook newData) {
requireNonNull(newData);

setBikes(newData.getBikeList());
Expand All @@ -92,6 +87,13 @@ public boolean hasBike(Bike bike) {
return bikes.contains(bike);
}

/**
* Returns a bike in the list whose name matches {@code bikeName}.
*/
public Optional<Bike> getBike(String bikeName) {
return bikes.getBike(bikeName);
}

/**
* Adds a bike to the loan book.
* The bike must not already exist in the loan book.
Expand Down Expand Up @@ -174,12 +176,20 @@ public boolean hasNextAvailableLoanId() {
return loanIdManager.hasNextAvailableLoanId();
}

/**
* Resets the Loan ID Manager.
*/
public void resetId() {
this.loanIdManager.reset();
}

//// util methods

@Override
public String toString() {
return loans.asUnmodifiableObservableList().size() + " loans"
+ bikes.asUnmodifiableObservableList().size() + " bikes";
return loans.asUnmodifiableObservableList().size() + " loans, "
+ bikes.asUnmodifiableObservableList().size() + " bikes, "
+ "LoanIdManager: " + loanIdManager.toString();
// TODO: refine later
}

Expand All @@ -200,16 +210,22 @@ public LoanIdManager getLoanIdManager() {

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof LoanBook // instanceof handles nulls
&& loans.equals(((LoanBook) other).loans)
&& bikes.equals(((LoanBook) other).bikes))
&& loanIdManager.equals(((LoanBook) other).loanIdManager);
if (other == this) {
return true; // short circuit if same object
}
if (!(other instanceof LoanBook)) { // instanceof handles nulls
return false;
}
LoanBook otherLoanBook = (LoanBook) other;

return loans.equals(otherLoanBook.loans)
&& bikes.equals(otherLoanBook.bikes)
&& loanIdManager.equals(otherLoanBook.loanIdManager);
}

@Override
public int hashCode() {
// Use Objects.hash()
return hash(bikes, loans);
return hash(bikes, loans, loanIdManager);
}
}
31 changes: 30 additions & 1 deletion src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.model;

import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import javafx.collections.ObservableList;
Expand All @@ -17,7 +19,7 @@ public interface Model {
Predicate<Loan> PREDICATE_SHOW_ALL_LOANS = unused -> true;

/** Clears existing backing model and replaces with the provided new data. */
void resetData(ReadOnlyLoanBook newData);
void replaceData(ReadOnlyLoanBook newData);

/** Returns the LoanBook */
ReadOnlyLoanBook getLoanBook();
Expand All @@ -27,6 +29,11 @@ public interface Model {
*/
boolean hasBike(Bike bike);

/**
* Returns a bike in the list whose name matches {@code bikeName}.
*/
Optional<Bike> getBike(String bikeName);

/**
* Adds the given bike.
* {@code bike} must not already exist in the loan book.
Expand All @@ -49,6 +56,12 @@ public interface Model {
/** Returns an unmodifiable view of the filtered bike list */
ObservableList<Bike> getFilteredBikeList();

/**
* Replaces the contents of the bike list with {@code bikes}.
* {@code bikes} must not contain duplicate bikes.
*/
void setBikes(List<Bike> bikes);

/**
* Updates the filter of the filtered bike list to filter by the given {@code predicate}.
* @throws NullPointerException if {@code predicate} is null.
Expand Down Expand Up @@ -80,6 +93,11 @@ public interface Model {
*/
boolean hasNextAvailableId();

/**
* Resets the Loan ID Manager.
*/
void resetId();

/**
* Adds the given loan.
* {@code loan} must not already exist in the loan book.
Expand All @@ -99,6 +117,17 @@ public interface Model {
*/
void updateLoan(Loan target, Loan editedLoan);

/**
* Replaces the contents of the loan list with {@code loans}.
* {@code loans} must not contain duplicate loans.
*/
void setLoans(List<Loan> loans);

/**
* Clears the loan list and resets the loan ID.
*/
void resetLoans();

/** Returns an unmodifiable view of the filtered loan list */
ObservableList<Loan> getFilteredLoanList();

Expand Down
55 changes: 47 additions & 8 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.logging.Logger;

Expand Down Expand Up @@ -47,8 +50,8 @@ public ModelManager() {
}

@Override
public void resetData(ReadOnlyLoanBook newData) {
versionedLoanBook.resetData(newData);
public void replaceData(ReadOnlyLoanBook newData) {
versionedLoanBook.replaceData(newData);
indicateLoanBookChanged();
}

Expand All @@ -62,14 +65,19 @@ private void indicateLoanBookChanged() {
raise(new LoanBookChangedEvent(versionedLoanBook));
}

//=========== Bike List Mutators =============================================================
//=========== Bike List Accessors and Mutators =============================================================

@Override
public boolean hasBike(Bike bike) {
requireNonNull(bike);
return versionedLoanBook.hasBike(bike);
}

@Override
public Optional<Bike> getBike(String bikeName) {
return versionedLoanBook.getBike(bikeName);
}

@Override
public void addBike(Bike bike) {
versionedLoanBook.addBike(bike);
Expand All @@ -90,6 +98,12 @@ public void updateBike(Bike target, Bike editedBike) {
indicateLoanBookChanged();
}

@Override
public void setBikes(List<Bike> bikes) {
versionedLoanBook.setBikes(bikes);
indicateLoanBookChanged();
}

//=========== Filtered Bike List Accessors =============================================================

/**
Expand All @@ -107,7 +121,7 @@ public void updateFilteredBikeList(Predicate<Bike> predicate) {
filteredBikes.setPredicate(predicate);
}

//=========== Loan List Mutators =============================================================
//=========== Loan List Accessors and Mutators =============================================================

@Override
public boolean hasLoan(Loan loan) {
Expand All @@ -129,13 +143,19 @@ public void deleteLoan(Loan target) {
}

@Override
public void setPass(Password pass) {
preference.setPass(pass);
public void setLoans(List<Loan> loans) {
versionedLoanBook.setLoans(loans);
indicateLoanBookChanged();
}

/**
* Clears the loan list and resets the loan ID.
*/
@Override
public String getPass() {
return preference.getPass();
public void resetLoans() {
setLoans(Collections.emptyList());
resetId();
// Change has already been indicated in the above commands
}

@Override
Expand Down Expand Up @@ -178,6 +198,13 @@ public boolean hasNextAvailableId() {
return versionedLoanBook.hasNextAvailableLoanId();
}

@Override

public void resetId() {
versionedLoanBook.resetId();
indicateLoanBookChanged();
}

//=========== Undo/Redo =================================================================================

@Override
Expand Down Expand Up @@ -207,6 +234,18 @@ public void commitLoanBook() {
versionedLoanBook.commit();
}

//=========== Password =================================================================================

@Override
public void setPass(Password pass) {
preference.setPass(pass);
}

@Override
public String getPass() {
return preference.getPass();
}

@Override
public boolean equals(Object obj) {
// short circuit if same object
Expand Down
Loading

0 comments on commit 951f7a4

Please sign in to comment.