Skip to content

Commit

Permalink
Merge branch 'master' into person-testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
ReflectiveObsidian authored Mar 12, 2024
2 parents e517c27 + 08c6ce0 commit 9a56364
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 20 deletions.
41 changes: 40 additions & 1 deletion src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.tag.Tag;
Expand All @@ -15,6 +16,7 @@
* Guarantees: details are present and not null, field values are validated, immutable.
*/
public class Person {
private final UUID uuid;

// Identity fields
private final Name name;
Expand All @@ -26,6 +28,7 @@ public class Person {
private final Set<Tag> tags = new HashSet<>();

/**
* Constructs a person with a random UUID.
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
Expand All @@ -35,6 +38,7 @@ public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tag
this.email = email;
this.address = address;
this.tags.addAll(tags);
this.uuid = UUID.randomUUID();
}

public Name getName() {
Expand All @@ -60,6 +64,22 @@ public Address getAddress() {
public Set<Tag> getTags() {
return Collections.unmodifiableSet(tags);
}
/**
* Returns the uuid of the person.
*
* @return The uuid of the person.
*/
public UUID getUuid() {
return uuid;
}
/**
* Returns the uuid of the person as a string.
*
* @return The uuid of the person as a string.
*/
public String getUuidString() {
return uuid.toString();
}

/**
* Returns true if both persons have the same name.
Expand Down Expand Up @@ -96,16 +116,35 @@ public boolean equals(Object other) {
&& address.equals(otherPerson.address)
&& tags.equals(otherPerson.tags);
}
/**
* Returns true if the UUID of the person is the same as the UUID of the other object.
*
* @param other The object to compare with.
* @return True if the UUID of the person is the same as the UUID of the other object.
*/
public boolean equalsUuid(Object other) {
if (!(other instanceof Person)) {
return false;
}

if (other == this) {
return true;
}

Person otherPerson = (Person) other;
return uuid.equals(otherPerson.uuid);
}

@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, address, tags, uuid);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("uuid", uuid)
.add("name", name)
.add("phone", phone)
.add("email", email)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

abstract class Attribute {
protected String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

import java.time.LocalDate;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

import java.time.LocalDate;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

/**
* Integer attribute with integer value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

/**
* Name attribute with string value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

/**
* Phone number attribute with integer value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

// Gender attribute with enum
class SexAttribute extends Attribute {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

class StringAttribute extends Attribute {
private String value;
Expand Down
44 changes: 41 additions & 3 deletions src/test/java/seedu/address/model/person/PersonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ public void equals() {

@Test
public void toStringMethod() {
String expected = Person.class.getCanonicalName() + "{name=" + ALICE.getName() + ", phone=" + ALICE.getPhone()
+ ", email=" + ALICE.getEmail() + ", address=" + ALICE.getAddress() + ", tags=" + ALICE.getTags() + "}";
String expected = Person.class.getCanonicalName()
+ "{uuid=" + ALICE.getUuidString()
+ ", name=" + ALICE.getName()
+ ", phone=" + ALICE.getPhone()
+ ", email=" + ALICE.getEmail()
+ ", address=" + ALICE.getAddress()
+ ", tags=" + ALICE.getTags() + "}";
assertEquals(expected, ALICE.toString());
}
@Test
Expand All @@ -112,5 +117,38 @@ public void getEmail() {
public void getAddress() {
assertEquals(VALID_ADDRESS_BOB, BOB.getAddress().toString());
}

@Test
public void getUuidString() {
Person person = new PersonBuilder().build();
String personUuidString = person.getUuidString();
// Solution below adapted from Github Copilot
String uuidFormat = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";
assertTrue(personUuidString.matches(uuidFormat));
}
@Test
public void getUuidString_differentUuids() {
Person person1 = new PersonBuilder().build();
Person person2 = new PersonBuilder().build();
assertFalse(person1.getUuidString().equals(person2.getUuidString()));
}
@Test
public void equalsUuid_samePerson_true() {
Person person1 = new PersonBuilder().build();
assertTrue(person1.equalsUuid(person1));
}
@Test
public void equalsUuid_differentUuids_false() {
Person person1 = new PersonBuilder().build();
Person person2 = new PersonBuilder().build();
assertFalse(person1.equalsUuid(person2));
}
@Test
public void equalsUuid_notPerson_false() {
Person person1 = new PersonBuilder().build();
assertFalse(person1.equalsUuid("not a person"));
}
@Test
public void getUuid() {
assertTrue(ALICE.getUuid() != null);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seedu.address.model.person;
package seedu.address.model.person.attribute;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down

0 comments on commit 9a56364

Please sign in to comment.