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

Add phone model #62

Merged
merged 6 commits into from Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/main/java/seedu/address/model/phone/Brand.java
@@ -0,0 +1,57 @@
package seedu.address.model.phone;

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

/**
* Represents a Phone's brand in the SML.
* Guarantees: immutable; is valid as declared in {@link #isValidBrand(String)}
*/
public class Brand {

public static final String MESSAGE_CONSTRAINTS = "Brands can take any values, and should not be blank";

/*
* The first character of the brand 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 a {@code Brand}.
*
* @param brand A valid brand.
*/
public Brand(String brand) {
requireNonNull(brand);
checkArgument(isValidBrand(brand), MESSAGE_CONSTRAINTS);
value = brand;
}

/**
* Returns true if a given string is a valid brand.
*/
public static boolean isValidBrand(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 Brand // instanceof handles nulls
&& value.equals(((Brand) other).value)); // state check
}

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

}
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/phone/Capacity.java
@@ -0,0 +1,15 @@
package seedu.address.model.phone;

/**
* Represents a Phone's memory capacity in the SML.
*/
public enum Capacity {
SIZE_8GB,
yan-wl marked this conversation as resolved.
Show resolved Hide resolved
SIZE_16GB,
SIZE_32GB,
SIZE_64GB,
SIZE_128GB,
SIZE_256GB,
SIZE_512GB,
SIZE_1024GB
}
57 changes: 57 additions & 0 deletions src/main/java/seedu/address/model/phone/Colour.java
@@ -0,0 +1,57 @@
package seedu.address.model.phone;

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

/**
* Represents a Phone's colour in the SML.
* Guarantees: immutable; is valid as declared in {@link #isValidColour(String)}
*/
public class Colour {

public static final String MESSAGE_CONSTRAINTS = "Colours must be from the rainbow.";

/*
* 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 = "[^\\s].*";

public final String value;

/**
* Constructs a {@code Colour}.
*
* @param colour A valid colour.
*/
public Colour(String colour) {
requireNonNull(colour);
checkArgument(isValidColour(colour), MESSAGE_CONSTRAINTS);
value = colour;
}

/**
* Returns true if a given string is a valid colour.
*/
public static boolean isValidColour(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 Colour // instanceof handles nulls
&& value.equals(((Colour) other).value)); // state check
}

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

}
60 changes: 60 additions & 0 deletions src/main/java/seedu/address/model/phone/Cost.java
@@ -0,0 +1,60 @@
package seedu.address.model.phone;

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

import java.text.NumberFormat;
import java.text.ParseException;

/**
* Represents a Phone's cost in the SML.
* Guarantees: immutable; is valid as declared in {@link #isValidCost(String)}
*/
public class Cost {

public static final String MESSAGE_CONSTRAINTS =
"Costs must be non-negative, start with \'$\' and have at most 2 decimals.";

public final String value;

/**
* Constructs a {@code Cost}.
*
* @param cost A valid cost.
*/
public Cost(String cost) {
requireNonNull(cost);
checkArgument(isValidCost(cost), MESSAGE_CONSTRAINTS);
value = cost;
}

/**
* Returns true if a given string is a valid cost.
*/
public static boolean isValidCost(String cost) {
try {
Number number = NumberFormat.getCurrencyInstance().parse(cost);
return number != null;
} catch (ParseException e) {
return false;
}
}

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

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

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

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

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

/**
* Represents a Phone's name in the SML.
* 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();
}

}
141 changes: 141 additions & 0 deletions src/main/java/seedu/address/model/phone/Phone.java
@@ -0,0 +1,141 @@
package seedu.address.model.phone;

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 java.util.UUID;

import seedu.address.model.tag.Tag;

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

// Identity fields
private final UUID id;
private final Name name;
private final Brand brand;
private final Capacity capacity;
private final Colour colour;

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

public Phone(Name name, Brand brand, Capacity capacity, Colour colour, Cost cost,
Set<Tag> tags) {
requireAllNonNull(name, brand, capacity, colour, cost, tags);
this.id = UUID.randomUUID();
this.name = name;
this.brand = brand;
this.capacity = capacity;
this.colour = colour;
this.cost = cost;
this.tags.addAll(tags);
}

public UUID getId() {
return id;
}

public Name getName() {
return name;
}

public Brand getBrand() {
return brand;
}

public Capacity getCapacity() {
return capacity;
}

public Colour getColour() {
return colour;
}

public Cost getCost() {
return cost;
}

/**
* 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 phones have the same identity fields.
* This defines a weaker notion of equality between two phones.
*/
public boolean isSamePhone(Phone otherPhone) {
if (otherPhone == this) {
return true;
}

return otherPhone != null
&& otherPhone.getId().equals(getId())
&& otherPhone.getName().equals(getName())
&& otherPhone.getBrand().equals((getBrand()))
&& otherPhone.getCapacity().equals((getCapacity()))
&& otherPhone.getColour().equals((getColour()));
}

/**
* Returns true if both phones have the same identity and data fields.
* This defines a stronger notion of equality between two phones.
*/
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

Phone otherPhone = (Phone) other;
return otherPhone.getId().equals(getId())
&& otherPhone.getName().equals(getName())
&& otherPhone.getBrand().equals((getBrand()))
&& otherPhone.getCapacity().equals((getCapacity()))
&& otherPhone.getColour().equals((getColour()))
&& otherPhone.getCost().equals(getCost())
&& otherPhone.getTags().equals((getTags()));
}

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

@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append(" # ")
.append(getId())
.append(" Name: ")
.append(getName())
.append(" Brand: ")
.append(getBrand())
.append(" Capacity: ")
.append(getCapacity())
.append(" Color: ")
.append(getColour())
.append(" Cost: ")
.append(getCost())
.append(" Tags: ");
getTags().forEach(builder::append);
return builder.toString();
}

}