Skip to content

Commit

Permalink
Create new class for type
Browse files Browse the repository at this point in the history
There were many tests that have a standard format.

I decided to add the class Type so that it is easier to follow these formats
  • Loading branch information
LimZiJia committed Mar 19, 2024
1 parent 2447cd2 commit 0ba8825
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 52 deletions.
23 changes: 19 additions & 4 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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 Down Expand Up @@ -102,11 +103,12 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
Type updatedType = editPersonDescriptor.getType().orElse(personToEdit.getType());

if (personToEdit.isClient()) {
return new Client(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags);
return new Client(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags, updatedType);
} else {
return new Housekeeper(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags);
return new Housekeeper(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags, updatedType);
}
}

Expand Down Expand Up @@ -144,6 +146,7 @@ public static class EditPersonDescriptor {
private Email email;
private Address address;
private Set<Tag> tags;
private Type type;

public EditPersonDescriptor() {}

Expand All @@ -157,13 +160,14 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setEmail(toCopy.email);
setAddress(toCopy.address);
setTags(toCopy.tags);
setType(toCopy.type);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags);
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags, type);
}

public void setName(Name name) {
Expand Down Expand Up @@ -215,6 +219,15 @@ public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}

public void setType(Type type) {
this.type = type;
}

public Optional<Type> getType() {
return Optional.ofNullable(type);
}


@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -231,7 +244,8 @@ public boolean equals(Object other) {
&& Objects.equals(phone, otherEditPersonDescriptor.phone)
&& Objects.equals(email, otherEditPersonDescriptor.email)
&& Objects.equals(address, otherEditPersonDescriptor.address)
&& Objects.equals(tags, otherEditPersonDescriptor.tags);
&& Objects.equals(tags, otherEditPersonDescriptor.tags)
&& Objects.equals(type, otherEditPersonDescriptor.type);
}

@Override
Expand All @@ -242,6 +256,7 @@ public String toString() {
.add("email", email)
.add("address", address)
.add("tags", tags)
.add("type", type)
.toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -41,20 +42,20 @@ public AddCommand parse(String args) throws ParseException {
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS);
String type = argMultimap.getPreamble();
Type type = ParserUtil.parseType(argMultimap.getPreamble());
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

Person person;
switch (type) {
switch (type.toString()) {
case "client":
person = new Client(name, phone, email, address, tagList);
person = new Client(name, phone, email, address, tagList, type);
break;
case "housekeeper":
person = new Housekeeper(name, phone, email, address, tagList);
person = new Housekeeper(name, phone, email, address, tagList, type);
break;
default:
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.person.Type;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -110,6 +111,15 @@ public static Tag parseTag(String tag) throws ParseException {
return new Tag(trimmedTag);
}

public static Type parseType(String email) throws ParseException {
requireNonNull(email);
String trimmedType = email.trim();
if (!Type.isValidType(trimmedType)) {
throw new ParseException(Type.MESSAGE_CONSTRAINTS);
}
return new Type(trimmedType);
}

/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>}.
*/
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/seedu/address/model/person/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class Client extends Person {
* @param address
* @param tags
*/
public Client(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
super(name, phone, email, address, tags);
public Client(Name name, Phone phone, Email email, Address address, Set<Tag> tags, Type type) {
super(name, phone, email, address, tags, type);
}

/**
Expand All @@ -44,7 +44,8 @@ public boolean equals(Object other) {
&& this.getPhone().equals(otherPerson.getPhone())
&& this.getEmail().equals(otherPerson.getEmail())
&& this.getAddress().equals(otherPerson.getAddress())
&& this.getTags().equals(otherPerson.getTags());
&& this.getTags().equals(otherPerson.getTags())
&& this.getType().equals(otherPerson.getType());
}

@Override
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/seedu/address/model/person/Housekeeper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class Housekeeper extends Person {
* @param address
* @param tags
*/
public Housekeeper(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
super(name, phone, email, address, tags);
public Housekeeper(Name name, Phone phone, Email email, Address address, Set<Tag> tags, Type type) {
super(name, phone, email, address, tags, type);
}

/**
Expand All @@ -43,7 +43,8 @@ public boolean equals(Object other) {
&& this.getPhone().equals(otherPerson.getPhone())
&& this.getEmail().equals(otherPerson.getEmail())
&& this.getAddress().equals(otherPerson.getAddress())
&& this.getTags().equals(otherPerson.getTags());
&& this.getTags().equals(otherPerson.getTags())
&& this.getType().equals(otherPerson.getType());
}

@Override
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ public abstract class Person {
// Data fields
private final Address address;
private final Set<Tag> tags = new HashSet<>();
private final Type type;

/**
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
requireAllNonNull(name, phone, email, address, tags);
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags, Type type) {
requireAllNonNull(name, phone, email, address, tags, type);
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
this.tags.addAll(tags);
this.type = type;
}

public Name getName() {
Expand All @@ -53,6 +55,10 @@ public Address getAddress() {
return address;
}

public Type getType() {
return type;
}

/**
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
Expand Down Expand Up @@ -88,6 +94,7 @@ public String toString() {
.add("email", email)
.add("address", address)
.add("tags", tags)
.add("type", type)
.toString();
}

Expand Down
66 changes: 66 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,66 @@
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 = "Type can either be \"client\" or \"housekeeper\"";

/*
* The first character of the type must not be a whitespace,
* otherwise " " (a blank string) becomes a valid input.
*/
public static final String VALIDATION_REGEX = "[^\\s].*";

public final String value;

/**
* Constructs an {@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();
}

}

20 changes: 7 additions & 13 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@

import seedu.address.model.AddressBook;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.person.Address;
import seedu.address.model.person.Client;
import seedu.address.model.person.Email;
import seedu.address.model.person.Housekeeper;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.person.*;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -23,22 +17,22 @@ public static Person[] getSamplePersons() {
return new Person[] {
new Client(new Name("Alex Yeoh"), new Phone("87438807"), new Email("[email protected]"),
new Address("Blk 30 Geylang Street 29, #06-40"),
getTagSet("friends")),
getTagSet("friends"), new Type("client")),
new Client(new Name("Bernice Yu"), new Phone("99272758"), new Email("[email protected]"),
new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"),
getTagSet("colleagues", "friends")),
getTagSet("colleagues", "friends"), new Type("client")),
new Client(new Name("Charlotte Oliveiro"), new Phone("93210283"), new Email("[email protected]"),
new Address("Blk 11 Ang Mo Kio Street 74, #11-04"),
getTagSet("neighbours")),
getTagSet("neighbours"), new Type("client")),
new Housekeeper(new Name("David Li"), new Phone("91031282"), new Email("[email protected]"),
new Address("Blk 436 Serangoon Gardens Street 26, #16-43"),
getTagSet("family")),
getTagSet("family"), new Type("client")),
new Housekeeper(new Name("Irfan Ibrahim"), new Phone("92492021"), new Email("[email protected]"),
new Address("Blk 47 Tampines Street 20, #17-35"),
getTagSet("classmates")),
getTagSet("classmates"), new Type("client")),
new Housekeeper(new Name("Roy Balakrishnan"), new Phone("92624417"), new Email("[email protected]"),
new Address("Blk 45 Aljunied Street 85, #11-31"),
getTagSet("colleagues"))
getTagSet("colleagues"), new Type("client"))
};
}

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 Down Expand Up @@ -110,12 +111,17 @@ public Person toModelType() throws IllegalValueException {
final Set<Tag> modelTags = new HashSet<>(personTags);

if (type == null) {
throw new IllegalValueException("Type field is missing!");
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Type.class.getSimpleName()));
}
if (!Type.isValidType(type)) {
throw new IllegalValueException(Type.MESSAGE_CONSTRAINTS);
}
final Type modelType = new Type(type);

if (type.equals("client")) {
return new Client(modelName, modelPhone, modelEmail, modelAddress, modelTags);
return new Client(modelName, modelPhone, modelEmail, modelAddress, modelTags, modelType);
} else {
return new Housekeeper(modelName, modelPhone, modelEmail, modelAddress, modelTags);
return new Housekeeper(modelName, modelPhone, modelEmail, modelAddress, modelTags, modelType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class CommandTestUtil {
public static final String VALID_ADDRESS_BOB = "Block 123, Bobby Street 3";
public static final String VALID_TAG_HUSBAND = "husband";
public static final String VALID_TAG_FRIEND = "friend";
public static final String VALID_TYPE_AMY = "client";
public static final String VALID_TYPE_BOB = "housekeeper";

public static final String NAME_DESC_AMY = " " + PREFIX_NAME + VALID_NAME_AMY;
public static final String NAME_DESC_BOB = " " + PREFIX_NAME + VALID_NAME_BOB;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public void toStringMethod() {
+ editPersonDescriptor.getPhone().orElse(null) + ", email="
+ editPersonDescriptor.getEmail().orElse(null) + ", address="
+ editPersonDescriptor.getAddress().orElse(null) + ", tags="
+ editPersonDescriptor.getTags().orElse(null) + "}";
+ editPersonDescriptor.getTags().orElse(null) + ", type="
+ editPersonDescriptor.getType().orElse(null) + "}";
assertEquals(expected, editPersonDescriptor.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.Messages.MESSAGE_UNKNOWN_COMMAND;
import static seedu.address.logic.parser.CliSyntax.ALLOWED_PREAMBLES;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;

Expand Down Expand Up @@ -84,7 +85,10 @@ public void parseCommand_help() throws Exception {

@Test
public void parseCommand_list() throws Exception {
assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD) instanceof ListCommand);
assertTrue(parser.parseCommand(
ListCommand.COMMAND_WORD + " " + ALLOWED_PREAMBLES[0]) instanceof ListCommand);
assertTrue(parser.parseCommand(
ListCommand.COMMAND_WORD + " " + ALLOWED_PREAMBLES[1]) instanceof ListCommand);
assertTrue(parser.parseCommand(ListCommand.COMMAND_WORD + " 3") instanceof ListCommand);
}

Expand Down
Loading

0 comments on commit 0ba8825

Please sign in to comment.