Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2324S1#82 from jx124/extract-client-l…
Browse files Browse the repository at this point in the history
…ead-tags

Extract "client" and "lead" tags into Type class
  • Loading branch information
garylow2001 authored Oct 19, 2023
2 parents 9f78542 + 9f54f30 commit 0bab3a7
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 42 deletions.
6 changes: 2 additions & 4 deletions src/main/java/seedu/address/model/person/Client.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package seedu.address.model.person;

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

import java.util.Set;

import seedu.address.model.tag.Tag;
Expand All @@ -10,13 +8,13 @@
* Represents a Client in the address book.
*/
public class Client extends Person {
public static final String TAG_CLIENT = "Client";
public static final String TYPE_CLIENT = "client";

/**
* Every field must be present and not null.
*/
public Client(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
super(name, phone, email, address, addItemToSet(tags, new Tag(TAG_CLIENT)));
super(name, phone, email, new Type(TYPE_CLIENT), address, tags);
}

@Override
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/seedu/address/model/person/Lead.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package seedu.address.model.person;

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

import java.util.Set;

import seedu.address.model.tag.Tag;
Expand All @@ -10,13 +8,13 @@
* Represents a Lead in the address book.
*/
public class Lead extends Person {
public static final String TAG_LEAD = "Lead";
public static final String TYPE_LEAD = "lead";

/**
* Every field must be present and not null.
*/
public Lead(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
super(name, phone, email, address, addItemToSet(tags, new Tag(TAG_LEAD)));
super(name, phone, email, new Type(TYPE_LEAD), address, tags);
}

@Override
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public abstract class Person {
private final Name name;
private final Phone phone;
private final Email email;
private final Type type;

// Data fields
private final Address address;
Expand All @@ -28,11 +29,12 @@ public abstract class Person {
/**
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
public Person(Name name, Phone phone, Email email, Type type, Address address, Set<Tag> tags) {
requireAllNonNull(name, phone, email, address, tags);
this.name = name;
this.phone = phone;
this.email = email;
this.type = type;
this.address = address;
this.tags.addAll(tags);
}
Expand All @@ -49,6 +51,10 @@ public Email getEmail() {
return email;
}

public Type getType() {
return type;
}

public Address getAddress() {
return address;
}
Expand Down Expand Up @@ -93,14 +99,15 @@ public boolean equals(Object other) {
return name.equals(otherPerson.name)
&& phone.equals(otherPerson.phone)
&& email.equals(otherPerson.email)
&& type.equals(otherPerson.type)
&& address.equals(otherPerson.address)
&& tags.equals(otherPerson.tags);
}

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

@Override
Expand All @@ -109,6 +116,7 @@ public String toString() {
.add("name", name)
.add("phone", phone)
.add("email", email)
.add("type", type)
.add("address", address)
.add("tags", tags)
.toString();
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/seedu/address/model/person/Type.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package seedu.address.model.person;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

/**
* Represents a Person's type in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidType(String)}
*/
public class Type {

public static final String MESSAGE_CONSTRAINTS = "Types can only be 'client' or 'lead'";

public static final String VALIDATION_REGEX = "client|lead";

public final String value;

/**
* Constructs a {@code Type}.
*
* @param type A valid type.
*/
public Type(String type) {
requireNonNull(type);
checkArgument(isValidType(type), MESSAGE_CONSTRAINTS);
value = type;
}

/**
* Returns true if a given string is a valid type.
*/
public static boolean isValidType(String test) {
return test.matches(VALIDATION_REGEX);
}

@Override
public String toString() {
return value;
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

Type otherType = (Type) other;
return value.equals(otherType.value);
}

@Override
public int hashCode() {
return value.hashCode();
}

}
23 changes: 14 additions & 9 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package seedu.address.storage;

import static seedu.address.model.person.Client.TAG_CLIENT;
import static seedu.address.model.person.Lead.TAG_LEAD;
import static seedu.address.model.person.Client.TYPE_CLIENT;
import static seedu.address.model.person.Lead.TYPE_LEAD;

import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -20,6 +20,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Type;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -28,12 +29,11 @@
class JsonAdaptedPerson {

public static final String MISSING_FIELD_MESSAGE_FORMAT = "Person's %s field is missing!";
public static final String MISSING_PERSON_TYPE_MESSAGE_FORMAT =
"Person's tag field must contain either '%s' or '%s'!";

private final String name;
private final String phone;
private final String email;
private final String type;
private final String address;
private final List<JsonAdaptedTag> tags = new ArrayList<>();

Expand All @@ -42,11 +42,12 @@ class JsonAdaptedPerson {
*/
@JsonCreator
public JsonAdaptedPerson(@JsonProperty("name") String name, @JsonProperty("phone") String phone,
@JsonProperty("email") String email, @JsonProperty("address") String address,
@JsonProperty("tags") List<JsonAdaptedTag> tags) {
@JsonProperty("email") String email, @JsonProperty("type") String type,
@JsonProperty("address") String address, @JsonProperty("tags") List<JsonAdaptedTag> tags) {
this.name = name;
this.phone = phone;
this.email = email;
this.type = type;
this.address = address;
if (tags != null) {
this.tags.addAll(tags);
Expand All @@ -61,6 +62,7 @@ public JsonAdaptedPerson(Person source) {
phone = source.getPhone().value;
email = source.getEmail().value;
address = source.getAddress().value;
type = source.getType().value;
tags.addAll(source.getTags().stream()
.map(JsonAdaptedTag::new)
.collect(Collectors.toList()));
Expand Down Expand Up @@ -111,12 +113,15 @@ public Person toModelType() throws IllegalValueException {

final Set<Tag> modelTags = new HashSet<>(personTags);

if (modelTags.contains(new Tag(TAG_CLIENT))) {
if (type == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, "Type"));
}
if (type.equals(TYPE_CLIENT)) {
return new Client(modelName, modelPhone, modelEmail, modelAddress, modelTags);
} else if (modelTags.contains(new Tag(TAG_LEAD))) {
} else if (type.equals(TYPE_LEAD)) {
return new Lead(modelName, modelPhone, modelEmail, modelAddress, modelTags);
} else {
throw new IllegalValueException(String.format(MISSING_PERSON_TYPE_MESSAGE_FORMAT, TAG_CLIENT, TAG_LEAD));
throw new IllegalValueException(Type.MESSAGE_CONSTRAINTS);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/ui/PersonCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public PersonCard(Person person, int displayedIndex) {
phone.setText(person.getPhone().value);
address.setText(person.getAddress().value);
email.setText(person.getEmail().value);

Label label = new Label(person.getType().value);
if (person.isClient()) {
label.getStyleClass().add("client-label");
} else {
label.getStyleClass().add("lead-label");
}
tags.getChildren().add(label);

person.getTags().stream()
.sorted(Comparator.comparing(tag -> tag.tagName))
.forEach(tag -> tags.getChildren().add(new Label(tag.tagName)));
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/view/DarkTheme.css
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,11 @@
-fx-background-radius: 2;
-fx-font-size: 11;
}

#tags .client-label {
-fx-background-color: #007011;
}

#tags .lead-label {
-fx-background-color: #de5900;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"name": "Alice Pauline",
"phone": "94351253",
"email": "[email protected]",
"type": "lead",
"address": "123, Jurong West Ave 6, #08-111",
"tags": [ "Client", "friends" ]
}, {
"name": "Alice Pauline",
"phone": "94351253",
"email": "[email protected]",
"type": "lead",
"address": "4th street",
"tags": [ "Client" ]
} ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"persons": [ {
"name": "Hans Muster",
"phone": "9482424",
"type": "invalidtype",
"email": "invalid@email!3e",
"address": "4th street"
} ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,50 @@
"name" : "Alice Pauline",
"phone" : "94351253",
"email" : "[email protected]",
"type": "client",
"address" : "123, Jurong West Ave 6, #08-111",
"tags" : [ "Client", "friends" ]
"tags" : [ "friends" ]
}, {
"name" : "Benson Meier",
"phone" : "98765432",
"email" : "[email protected]",
"type": "client",
"address" : "311, Clementi Ave 2, #02-25",
"tags" : [ "Client", "owesMoney", "friends" ]
"tags" : [ "owesMoney", "friends" ]
}, {
"name" : "Carl Kurz",
"phone" : "95352563",
"email" : "[email protected]",
"type": "client",
"address" : "wall street",
"tags" : [ "Client" ]
"tags" : []
}, {
"name" : "Daniel Meier",
"phone" : "87652533",
"email" : "[email protected]",
"type": "client",
"address" : "10th street",
"tags" : [ "Client", "friends" ]
"tags" : [ "friends" ]
}, {
"name" : "Elle Meyer",
"phone" : "9482224",
"email" : "[email protected]",
"type": "lead",
"address" : "michegan ave",
"tags" : [ "Lead" ]
"tags" : []
}, {
"name" : "Fiona Kunz",
"phone" : "9482427",
"email" : "[email protected]",
"type": "lead",
"address" : "little tokyo",
"tags" : [ "Lead" ]
"tags" : []
}, {
"name" : "George Best",
"phone" : "9482442",
"email" : "[email protected]",
"type": "lead",
"address" : "4th street",
"tags" : [ "Lead" ]
"tags" : []
} ]
}
9 changes: 5 additions & 4 deletions src/test/java/seedu/address/model/person/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ public void equals() {

@Test
public void toStringMethod() {
String expected = Client.class.getCanonicalName() + "{name=" + TypicalClients.ALICE.getName() + ""
String expected = Client.class.getCanonicalName() + "{name=" + TypicalClients.ALICE.getName()
+ ", phone=" + TypicalClients.ALICE.getPhone()
+ ", email=" + TypicalClients.ALICE.getEmail() + ", address="
+ TypicalClients.ALICE.getAddress() + ", "
+ "tags=" + TypicalClients.ALICE.getTags() + "}";
+ ", email=" + TypicalClients.ALICE.getEmail()
+ ", type=" + TypicalClients.ALICE.getType()
+ ", address=" + TypicalClients.ALICE.getAddress()
+ ", tags=" + TypicalClients.ALICE.getTags() + "}";
assertEquals(expected, TypicalClients.ALICE.toString());
}
@Test
Expand Down
9 changes: 7 additions & 2 deletions src/test/java/seedu/address/model/person/LeadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,13 @@ public void equals() {

@Test
public void toStringMethod() {
String expected = Lead.class.getCanonicalName() + "{name=" + ALICE.getName() + ", phone=" + ALICE.getPhone()
+ ", email=" + ALICE.getEmail() + ", address=" + ALICE.getAddress() + ", tags=" + ALICE.getTags() + "}";
String expected = Lead.class.getCanonicalName()
+ "{name=" + ALICE.getName()
+ ", phone=" + ALICE.getPhone()
+ ", email=" + ALICE.getEmail()
+ ", type=" + ALICE.getType()
+ ", address=" + ALICE.getAddress()
+ ", tags=" + ALICE.getTags() + "}";
assertEquals(expected, ALICE.toString());
}
}
3 changes: 2 additions & 1 deletion src/test/java/seedu/address/model/person/PersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public void equals() {
@Test
public void toStringMethod() {
String expected = Client.class.getCanonicalName() + "{name=" + ALICE.getName() + ", phone=" + ALICE.getPhone()
+ ", email=" + ALICE.getEmail() + ", address=" + ALICE.getAddress() + ", tags=" + ALICE.getTags() + "}";
+ ", email=" + ALICE.getEmail() + ", type=" + ALICE.getType() + ", address=" + ALICE.getAddress()
+ ", tags=" + ALICE.getTags() + "}";
assertEquals(expected, ALICE.toString());
}
}
Loading

0 comments on commit 0bab3a7

Please sign in to comment.