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 1 commit
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
29 changes: 19 additions & 10 deletions src/main/java/seedu/address/model/phone/Cost.java
@@ -1,49 +1,58 @@
package seedu.address.model.phone;

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

import static seedu.address.commons.util.AppUtil.checkArgument;
yan-wl marked this conversation as resolved.
Show resolved Hide resolved

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

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

public final double value;
public final String value;

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

/**
* Returns true if a given double is a valid cost.
* Returns true if a given string is a valid cost.
*/
public static boolean isValidCost(double cost) {
return cost >= 0;
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;
return value;
}

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

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

}
31 changes: 20 additions & 11 deletions src/main/java/seedu/address/model/phone/Price.java
@@ -1,49 +1,58 @@
package seedu.address.model.phone;

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

import static seedu.address.commons.util.AppUtil.checkArgument;

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

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

public final double value;
public final String value;

/**
* Constructs a {@code Price}.
*
* @param price A valid price.
*/
public Price(double price) {
public Price(String price) {
checkArgument(isValidPrice(price), MESSAGE_CONSTRAINTS);
value = price;
}

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

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

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

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

}