Skip to content

Commit

Permalink
Merge branch 'master' into change-tag-colour
Browse files Browse the repository at this point in the history
  • Loading branch information
wn committed Oct 17, 2018
2 parents 1dbc908 + ec650dc commit 228b90d
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 17 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ lib/*
*.log
*.log.*
*.csv
config.json
src/test/data/sandbox/
preferences.json
.DS_Store
Expand Down
30 changes: 30 additions & 0 deletions _reposense/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"authors":
[
{
"githubId": "FongYuan",
"displayName": "Lim Fong Yuan",
"authorNames": ["Fong Yuan", "FongYuan"]
},
{
"githubId": "Kelly9373",
"displayName": "Liu Xiaohang",
"authorNames": ["Liu Xiaohang", "Kelly9373"]
},
{
"githubId": "prokarius",
"displayName": "David Kum",
"authorNames": ["David Kum", "prokarius"]
},
{
"githubId": "wn96",
"displayName": "Ang Wei Neng",
"authorNames": ["Ang Wei Neng", "wn96", "weineng"]
},
{
"githubId": "xantho09",
"displayName": "Muhammad Irham Rasyidi Bin Zainal",
"authorNames": ["xantho09", "Muhammad Irham Rasyidi Bin Zainal"]
}
]
}
92 changes: 92 additions & 0 deletions src/main/java/seedu/address/model/bike/Bike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package seedu.address.model.bike;

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Objects;

import seedu.address.model.loan.Name;

/**
* Represents a Bike in the loan book.
* Guarantees: details are present and not null, field values are validated, immutable.
*/
public class Bike {

//Identity fields
/**
* The name of the Bike also doubles as its ID, i.e. it should be unique as it is used to identify this bike.
* For now, borrow the Name class from Loan. It can be its own separate class once the difference matters.
*/
private final Name name;

// Data fields
private final BikeStatus status;

/**
* Every field must be present and not null.
*/
public Bike(Name name) {
requireAllNonNull(name);
this.name = name;

this.status = BikeStatus.AVAILABLE;
}

public Name getName() {
return name;
}

public BikeStatus getStatus() {
return status;
}

public boolean isAvailable() {
return this.getStatus().equals(BikeStatus.AVAILABLE);
}

/**
* Returns true iff both bikes have the same name.
* This defines a weaker notion of equality between two bikes.
*/
public boolean isSameBike(Bike otherBike) {
if (otherBike == this) {
return true;
}

return otherBike != null
&& otherBike.getName().equals(this.getName());
}

/**
* Returns true if both bikes have the same identity and data fields.
* This defines a stronger notion of equality between two bikes.
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

if (!(other instanceof Bike)) {
return false;
}

Bike otherBike = (Bike) other;
return otherBike.getName().equals(getName())
&& otherBike.getStatus().equals(getStatus());
}

@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(name, status);
}

@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append(getName())
.append(" Status: ").append(getStatus());
return builder.toString();
}
}
17 changes: 17 additions & 0 deletions src/main/java/seedu/address/model/bike/BikeStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package seedu.address.model.bike;

/**
* Represents the status of a bike in the loan book.
*/
public enum BikeStatus {
AVAILABLE {
public String toString() {
return "Available";
}
},
LOANED_OUT {
public String toString() {
return "Loaned Out";
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/seedu/address/model/loan/LoanTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class LoanTime extends DataField<Instant> {
.withResolverStyle(ResolverStyle.STRICT);

// Default pattern for DateTime
private static final String DEFAULT_DATETIME_PATTERN = "uuuu-MM-dd',' HH:mm";
private static final String DEFAULT_DATETIME_PATTERN = "uuuu-MM-dd HH:mm";

/**
* Constructs a {@code LoanTime} with value set at current time.
Expand All @@ -62,7 +62,7 @@ public LoanTime() {
* @param loanTime A string to be parsed into a LoanTime
*/
public LoanTime(String loanTime) {
super(MESSAGE_LOANTIME_CONSTRAINTS, VALIDITY_PREDICATE, LoanTime::parseLoanTime, loanTime);
super(MESSAGE_LOANTIME_CONSTRAINTS, VALIDITY_PREDICATE, LoanTime::parseInstant, loanTime);
}

/**
Expand Down Expand Up @@ -175,7 +175,7 @@ public static long loanTimeDifferenceMinutes(LoanTime currentTime, LoanTime othe
* @param objString The string to parse
* @return The parsed {@link Instant}
*/
private static Instant parseLoanTime(String objString) {
private static Instant parseInstant(String objString) {
// If the string is not valid, throw an exception.
if (!isValidLoanTime(objString)) {
throw new IllegalArgumentException(MESSAGE_LOANTIME_CONSTRAINTS);
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/seedu/address/model/bike/BikeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package seedu.address.model.bike;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import seedu.address.testutil.BikeBuilder;

public class BikeTest {

private static final Bike BIKE1 = new BikeBuilder().withName("B001").build();
private static final Bike BIKE2 = new BikeBuilder().withName("B002").build();

@Test
public void isSameBike() {
// same object -> returns true
assertTrue(BIKE1.isSameBike(BIKE1));

// null -> returns false
assertFalse(BIKE1.isSameBike(null));

// different name -> returns false
Bike editedBike1 = new BikeBuilder(BIKE1).withName("B002").build();
assertFalse(BIKE1.isSameBike(editedBike1));
}

@Test
public void equals() {
// same values -> returns true
Bike bike1Copy = new BikeBuilder(BIKE1).build();
assertTrue(BIKE1.equals(bike1Copy));

// same object -> returns true
assertTrue(BIKE1.equals(BIKE1));

// null -> returns false
assertFalse(BIKE1 == null);

// different type -> returns false
assertFalse(BIKE1.equals(5));

// different bike -> returns false
assertFalse(BIKE1.equals(BIKE2));

// different name -> returns false
Bike editedBike1 = new BikeBuilder(BIKE1).withName("B002").build();
assertFalse(BIKE1.equals(editedBike1));
}

@Test
public void toStringTest() {
assertEquals(BIKE1.toString(), "B001 Status: Available");
}
}
46 changes: 33 additions & 13 deletions src/test/java/seedu/address/model/loan/LoanTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@

public class LoanTimeTest {

// Note that this method does not run correctly on Appveyor.
// Tried and tested though. It works.
private static final DateTimeFormatter EXPECTED_DATE_FORMAT = DateTimeFormatter.ofPattern("uuuu-MM-dd");
private static final DateTimeFormatter EXPECTED_DATETIME_FORMAT = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm");

/**
* Tests for LoanTime object creation based on current system date.
*/
@Test
public void constructorInputStringFormatValue() {
LoanTime loanTime1 = new LoanTime("2001-02-03 19:06");
assertEquals("2001-02-03, 19:06", loanTime1.toString());
assertEquals("2001-02-03 19:06", loanTime1.toString());

LoanTime loanTime2 = new LoanTime("2021-12-24 02:06");
assertEquals("2021-12-24, 02:06", loanTime2.toString());
assertEquals("2021-12-24 02:06", loanTime2.toString());

LoanTime loanTime3 = new LoanTime("2103-01-01 21:03");
assertEquals("2103-01-01, 21:03", loanTime3.toString());
assertEquals("2103-01-01 21:03", loanTime3.toString());
}

/**
Expand All @@ -43,19 +44,19 @@ public void constructorInputCheckCurrentDate() {
// between the next two statements. Therefore, we'll also have the current
// date after the LoanTime construction. The constructed LoanTime must be
// equal to one of them.
String currentDateBeforeLoanTimeCreation = LocalDate.now().format(DateTimeFormatter.ofPattern("uuuu-MM-dd"));
String currentDateBeforeLoanTimeCreation = LocalDate.now().format(EXPECTED_DATE_FORMAT);
LoanTime loanTime1 = new LoanTime("00:25");
LoanTime loanTime2 = new LoanTime("21:03");
String currentDateAfterLoanTimeCreation = LocalDate.now().format(DateTimeFormatter.ofPattern("uuuu-MM-dd"));
String currentDateAfterLoanTimeCreation = LocalDate.now().format(EXPECTED_DATE_FORMAT);

String loanTime1ToString = loanTime1.toString();
String loanTime2ToString = loanTime2.toString();

assertTrue(loanTime1ToString.equals(currentDateBeforeLoanTimeCreation + ", 00:25")
|| loanTime1ToString.equals(currentDateAfterLoanTimeCreation + ", 00:25"));
assertTrue(loanTime1ToString.equals(currentDateBeforeLoanTimeCreation + " 00:25")
|| loanTime1ToString.equals(currentDateAfterLoanTimeCreation + " 00:25"));

assertTrue(loanTime2ToString.equals(currentDateBeforeLoanTimeCreation + ", 21:03")
|| loanTime2ToString.equals(currentDateAfterLoanTimeCreation + ", 21:03"));
assertTrue(loanTime2ToString.equals(currentDateBeforeLoanTimeCreation + " 21:03")
|| loanTime2ToString.equals(currentDateAfterLoanTimeCreation + " 21:03"));
}

/**
Expand All @@ -67,8 +68,7 @@ public void constructorInputCheckCurrentTime() {
LoanTime loanTime = new LoanTime();
LocalDateTime currentDateTimeAfterLoanTimeCreation = LocalDateTime.now().withSecond(0).withNano(0);

LocalDateTime loanTimeAsLocalDateTime = LocalDateTime.parse(loanTime.toString(),
DateTimeFormatter.ofPattern("uuuu-MM-dd',' HH:mm"));
LocalDateTime loanTimeAsLocalDateTime = LocalDateTime.parse(loanTime.toString(), EXPECTED_DATETIME_FORMAT);

// The expected results are that "dateTimeBeforeCreation <= loanTime <= dateTimeAfterCreation".
// Since the LocalDateTime class does not have a "isBeforeOrEquals" method, we will use the inverse,
Expand Down Expand Up @@ -186,4 +186,24 @@ public void constructorsDoesNotThrowErrorTest() {
LoanTime loanTime6 = new LoanTime("2002-01-02 12:05");
assertEquals(525600, LoanTime.loanTimeDifferenceMinutes(loanTime5, loanTime6)); // Minutes in a (non leap) year
}

@Test
public void toStringIsValidInputTest() {
String loanTimeString1 = "2103-01-03 21:03";
LoanTime loanTime1 = new LoanTime(loanTimeString1);
String loanTime1ToString = loanTime1.toString();

// The output of toString should match the initial input string.
assertEquals(loanTimeString1, loanTime1ToString);

// If used to create another LoanTime object, it should be equivalent.
LoanTime loanTime2 = new LoanTime(loanTime1ToString);
assertEquals(loanTime1, loanTime2);

// Test the same thing with a LoanTime created using only a time string.
LoanTime loanTime3 = new LoanTime("20:00");
LoanTime loanTime4 = new LoanTime(loanTime3.toString());

assertEquals(loanTime3, loanTime4);
}
}
36 changes: 36 additions & 0 deletions src/test/java/seedu/address/testutil/BikeBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package seedu.address.testutil;

import seedu.address.model.bike.Bike;
import seedu.address.model.loan.Name;

/**
* A utility class to help with building Bike objects.
*/
public class BikeBuilder {
public static final String DEFAULT_NAME = "BIKE001";

private Name name;

public BikeBuilder() {
name = new Name(DEFAULT_NAME);
}

/**
* Initializes the BikeBuilder with the data of {@code bikeToCopy}.
*/
public BikeBuilder(Bike bikeToCopy) {
name = bikeToCopy.getName();
}

/**
* Sets the {@code Name} of the {@code Bike} that we are building.
*/
public BikeBuilder withName(String name) {
this.name = new Name(name);
return this;
}

public Bike build() {
return new Bike(name);
}
}

0 comments on commit 228b90d

Please sign in to comment.