forked from nus-cs2103-AY1920S1/addressbook-level3
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6b6f261
create customer class
EugeneTeu 27b80dd
fix developer guide naming
EugeneTeu 275378e
add CustomerCard (UI)
EugeneTeu 315aa10
fix changes after Wei liang's review, restore docs to original
EugeneTeu 10f6951
fix change affected by careless refactoring
EugeneTeu 4535c24
fix erroneous test
EugeneTeu 23ed7ae
remove address field as per discussion
EugeneTeu 8c3039a
customer implements cloneable
EugeneTeu acd9665
fix errors
EugeneTeu 5172ef7
fix validation regex
EugeneTeu e5e2308
fix comments
EugeneTeu 83c9f1b
fix person comments
EugeneTeu File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/seedu/address/model/customer/ContactNumber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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 8 digits long"; | ||
public static final String VALIDATION_REGEX = "\\d{8,}"; | ||
public final String value; | ||
|
||
/** | ||
* Constructs a {@code ContactNumber}. | ||
* @param contactNumber A valid contactNumber. | ||
*/ | ||
public ContactNumber(String contactNumber) { | ||
requireNonNull(contactNumber); | ||
checkArgument(isValidContactNumber(contactNumber), MESSAGE_CONSTRAINTS); | ||
value = contactNumber; | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid contactNumber. | ||
*/ | ||
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
112
src/main/java/seedu/address/model/customer/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 customers. | ||
*/ | ||
@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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/seedu/address/model/customer/NameContainsKeywordsPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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