Skip to content

Commit

Permalink
Merge 96c6c46 into cac2c66
Browse files Browse the repository at this point in the history
  • Loading branch information
wn committed Nov 1, 2018
2 parents cac2c66 + 96c6c46 commit dee2b10
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/main/java/loanbook/model/loan/Loan.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public Loan(Name name,
Set<Tag> tags) {

requireAllNonNull(name, nric, phone, email, bike, rate, startTime, loanStatus, tags);

this.name = name;
this.nric = nric;
this.phone = phone;
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/loanbook/ui/LoanCard.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package loanbook.ui;

import java.util.HashSet;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import loanbook.model.loan.Loan;
import loanbook.model.tag.Tag;

/**
* An UI component that displays information of a {@code Loan}.
*/
public class LoanCard extends ListCard<Loan> {

private static final String FXML = "LoanListCard.fxml";
private static final String[] LOANSTATUS_TAG_COLOR_STYLES = {"red", "green", "orange"};
private static final String[] TAG_COLOR_STYLES =
{"teal", "red", "yellow", "blue", "orange", "brown", "green", "pink", "black", "grey"};
{"teal", "yellow", "blue", "brown", "pink", "black", "grey"};

@FXML
private Label name;
Expand Down Expand Up @@ -53,14 +57,26 @@ public LoanCard(Loan loan, int displayedIndex) {
private String getTagColorStyleFor(String tagName) {
// we use the hash code of the tag name to generate a random color, so that the color remain consistent
// between different runs of the program while still making it random enough between tags.
return TAG_COLOR_STYLES[Math.abs(tagName.hashCode()) % TAG_COLOR_STYLES.length];
switch (tagName) {
case "Ongoing":
return LOANSTATUS_TAG_COLOR_STYLES[2];
case "Returned":
return LOANSTATUS_TAG_COLOR_STYLES[1];
case "Deleted":
return LOANSTATUS_TAG_COLOR_STYLES[0];
default:
// All user defined tags are set to brown colour.
return TAG_COLOR_STYLES[3];
}
}

/**
* Creates the tag labels for {@code loan}.
*/
private void initTags(Loan loan) {
loan.getTags().forEach(tag -> {
HashSet<Tag> currentTags = new HashSet<>(loan.getTags());
currentTags.add(new Tag(loan.getLoanStatus().toString()));
currentTags.forEach(tag -> {
Label tagLabel = new Label(tag.value);
tagLabel.getStyleClass().add(getTagColorStyleFor(tag.value));
tags.getChildren().add(tagLabel);
Expand Down
7 changes: 1 addition & 6 deletions src/test/java/guitests/guihandles/LoanCardHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.util.List;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableMultiset;

import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.Region;
Expand Down Expand Up @@ -113,9 +111,6 @@ && getPhone().equals(loan.getPhone().getCensored())
&& getEmail().equals(loan.getEmail().getCensored())
&& getBike().equals(loan.getBike().getName().value)
&& getLoanRate().equals(loan.getLoanRate().toString())
&& getLoanStartTime().equals(loan.getLoanStartTime().toString())
&& ImmutableMultiset.copyOf(getTags()).equals(ImmutableMultiset.copyOf(loan.getTags().stream()
.map(tag -> tag.value)
.collect(Collectors.toList())));
&& getLoanStartTime().equals(loan.getLoanStartTime().toString());
}
}
22 changes: 15 additions & 7 deletions src/test/java/loanbook/ui/testutil/GuiTestAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -12,6 +13,7 @@
import guitests.guihandles.ResultDisplayHandle;
import loanbook.model.bike.Bike;
import loanbook.model.loan.Loan;
import loanbook.model.tag.Tag;
import loanbook.ui.LoanCard;

/**
Expand Down Expand Up @@ -76,19 +78,22 @@ public static void assertCardDisplaysLoan(Loan expectedLoan, LoanCardHandle actu
*/
private static String getTagColorStyleFor(String tagName) {
switch (tagName) {
case "classmates":
case "tag1":
case "owesMoney":
return "teal";
case "colleagues":
case "classmates":
case "neighbours":
return "yellow";
case "family":
case "friend":
return "orange";
case "friends":
return "brown";
case "husband":
return "grey";
return "brown";
case "Ongoing":
return "orange";
case "Returned":
return "green";
case "Deleted":
return "red";
default:
throw new AssertionError(tagName + " does not have a color assigned.");
}
Expand All @@ -97,9 +102,12 @@ private static String getTagColorStyleFor(String tagName) {
/**
* Asserts that the tags in {@code actualCard} matches all the tags in {@code expectedPerson} with the correct
* color.
*
*/
private static void assertTagsEqual(Loan expectedLoan, LoanCardHandle actualCard) {
List<String> expectedTags = expectedLoan.getTags().stream()
HashSet<Tag> newTags = new HashSet<>(expectedLoan.getTags());
newTags.add(new Tag(expectedLoan.getLoanStatus().toString()));
List<String> expectedTags = newTags.stream()
.map(tag -> tag.value).collect(Collectors.toList());
assertEquals(expectedTags, actualCard.getTags());
expectedTags.forEach(tag -> assertEquals(
Expand Down

0 comments on commit dee2b10

Please sign in to comment.