Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create customer class #61

Merged
merged 12 commits into from Sep 27, 2019
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/ParserUtil.java
Expand Up @@ -51,7 +51,7 @@ public static Name parseName(String name) throws ParseException {
}

/**
* Parses a {@code String phone} into a {@code Phone}.
* Parses a {@code String phone} into a {@code phone}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code phone} is invalid.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/AddressBook.java
Expand Up @@ -10,7 +10,7 @@

/**
* Wraps all data at the address-book level
* Duplicates are not allowed (by .isSamePerson comparison)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be changed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will have to modify in the end, I'll just leave it for now

* Duplicates are not allowed (by .isSameCustomer comparison)
*/
public class AddressBook implements ReadOnlyAddressBook {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Expand Up @@ -115,7 +115,7 @@ public void setPerson(Person target, Person editedPerson) {
//=========== Filtered Person List Accessors =============================================================

/**
* Returns an unmodifiable view of the list of {@code Person} backed by the internal list of
* Returns an unmodifiable view of the list of {@code Customer} backed by the internal list of
* {@code versionedAddressBook}
*/
@Override
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/seedu/address/model/customer/ContactNumber.java
@@ -0,0 +1,53 @@
package seedu.address.model.customer;

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

/**
* Represents a Customer's phone number in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidContactNumber(String)}
*/
public class ContactNumber {


public static final String MESSAGE_CONSTRAINTS =
"ContactNumber numbers should only contain numbers, and it should be at least 3 digits long";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contact number should be 8 numbers instead i think

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

public static final String VALIDATION_REGEX = "\\d{3,}";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be \d{8,}

public final String value;

/**
* Constructs a {@code ContactNumber}.
*
* @param contactNumber A valid contactNumber number.
*/
public ContactNumber(String contactNumber) {
requireNonNull(contactNumber);
checkArgument(isValidContactNumber(contactNumber), MESSAGE_CONSTRAINTS);
value = contactNumber;
}

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

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

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ContactNumber // instanceof handles nulls
&& value.equals(((ContactNumber) other).value)); // state check
}

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

}
112 changes: 112 additions & 0 deletions src/main/java/seedu/address/model/customer/Customer.java
@@ -0,0 +1,112 @@
package seedu.address.model.customer;

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

import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import seedu.address.model.tag.Tag;

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

// Identity fields
private final Name name;
private final ContactNumber contactNumber;
private final Email email;

// Data fields
private final Set<Tag> tags = new HashSet<>();

/**
* Every field must be present and not null.
*/
public Customer(Name name, ContactNumber contactNumber, Email email, Set<Tag> tags) {
requireAllNonNull(name, contactNumber, email, tags);
this.name = name;
this.contactNumber = contactNumber;
this.email = email;
this.tags.addAll(tags);
}

public Name getName() {
return name;
}

public ContactNumber getContactNumber() {
return contactNumber;
}

public Email getEmail() {
return email;
}

/**
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
*/
public Set<Tag> getTags() {
return Collections.unmodifiableSet(tags);
}

/**
* Returns true if both persons of the same name have at least one other identity field that is the same.
EugeneTeu marked this conversation as resolved.
Show resolved Hide resolved
* This defines a weaker notion of equality between two persons.
*/
public boolean isSameCustomer(Customer otherCustomer) {
if (otherCustomer == this) {
return true;
}

return otherCustomer != null
&& otherCustomer.getName().equals(getName())
&& (otherCustomer.getContactNumber().equals(getContactNumber())
|| otherCustomer.getEmail().equals(getEmail()));
}

/**
* Returns true if both customers have the same identity and data fields.
* This defines a stronger notion of equality between two persons.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

javadoc comment change to customers

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

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

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

Customer otherCustomer = (Customer) other;
return otherCustomer.getName().equals(getName())
&& otherCustomer.getContactNumber().equals(getContactNumber())
&& otherCustomer.getEmail().equals(getEmail())
&& otherCustomer.getTags().equals(getTags());
}

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

@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append(getName())
.append(" ContactNumber: ")
.append(getContactNumber())
.append(" Email: ")
.append(getEmail())
.append(" Tags: ");
getTags().forEach(builder::append);
return builder.toString();
}

}
67 changes: 67 additions & 0 deletions src/main/java/seedu/address/model/customer/Email.java
@@ -0,0 +1,67 @@
package seedu.address.model.customer;

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

/**
* Represents a Customer's email in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidEmail(String)}
*/
public class Email {

private static final String SPECIAL_CHARACTERS = "!#$%&'*+/=?`{|}~^.-";
public static final String MESSAGE_CONSTRAINTS = "Emails should be of the format local-part@domain "
+ "and adhere to the following constraints:\n"
+ "1. The local-part should only contain alphanumeric characters and these special characters, excluding "
+ "the parentheses, (" + SPECIAL_CHARACTERS + ") .\n"
+ "2. This is followed by a '@' and then a domain name. "
+ "The domain name must:\n"
+ " - be at least 2 characters long\n"
+ " - start and end with alphanumeric characters\n"
+ " - consist of alphanumeric characters, a period or a hyphen for the characters in between, if any.";
// alphanumeric and special characters
private static final String LOCAL_PART_REGEX = "^[\\w" + SPECIAL_CHARACTERS + "]+";
private static final String DOMAIN_FIRST_CHARACTER_REGEX = "[^\\W_]"; // alphanumeric characters except underscore
private static final String DOMAIN_MIDDLE_REGEX = "[a-zA-Z0-9.-]*"; // alphanumeric, period and hyphen
private static final String DOMAIN_LAST_CHARACTER_REGEX = "[^\\W_]$";
public static final String VALIDATION_REGEX = LOCAL_PART_REGEX + "@"
+ DOMAIN_FIRST_CHARACTER_REGEX + DOMAIN_MIDDLE_REGEX + DOMAIN_LAST_CHARACTER_REGEX;

public final String value;

/**
* Constructs an {@code Email}.
*
* @param email A valid email address.
*/
public Email(String email) {
requireNonNull(email);
checkArgument(isValidEmail(email), MESSAGE_CONSTRAINTS);
value = email;
}

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

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

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Email // instanceof handles nulls
&& value.equals(((Email) other).value)); // state check
}

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

}
59 changes: 59 additions & 0 deletions src/main/java/seedu/address/model/customer/Name.java
@@ -0,0 +1,59 @@
package seedu.address.model.customer;

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

/**
* Represents a Customer's name in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
*/
public class Name {

public static final String MESSAGE_CONSTRAINTS =
"Names should only contain alphanumeric characters and spaces, and it should not be blank";

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

public final String fullName;

/**
* Constructs a {@code Name}.
*
* @param name A valid name.
*/
public Name(String name) {
requireNonNull(name);
checkArgument(isValidName(name), MESSAGE_CONSTRAINTS);
fullName = name;
}

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


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

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Name // instanceof handles nulls
&& fullName.equals(((Name) other).fullName)); // state check
}

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

}
@@ -0,0 +1,31 @@
package seedu.address.model.customer;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.StringUtil;

/**
* Tests that a {@code Person}'s {@code Name} matches any of the keywords given.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be Customer instead of Person

*/
public class NameContainsKeywordsPredicate implements Predicate<Customer> {
private final List<String> keywords;

public NameContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}

@Override
public boolean test(Customer customer) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(customer.getName().fullName, keyword));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof NameContainsKeywordsPredicate // instanceof handles nulls
&& keywords.equals(((NameContainsKeywordsPredicate) other).keywords)); // state check
}

}