Skip to content

Commit e1cb0c7

Browse files
authored
Merge pull request #75 from AY1920S2-CS2103T-W12-4/main-kevin
Add Recipe model and Name, Ingredients & Instructions fields
2 parents 15356ce + 6fa8001 commit e1cb0c7

File tree

8 files changed

+173
-85
lines changed

8 files changed

+173
-85
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package seedu.address.model.recipe;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.commons.util.AppUtil.checkArgument;
5+
6+
/**
7+
* Represents a Recipe's ingredients in the recipe book.
8+
* Guarantees: immutable; is valid as declared in {@link #isValidIngredients(String)}
9+
*/
10+
public class Ingredients {
11+
12+
public static final String MESSAGE_CONSTRAINTS = "Ingredients can take any values, and it should not be blank";
13+
14+
/*
15+
* The first character of the ingredients must not be a whitespace,
16+
* otherwise " " (a blank string) becomes a valid input.
17+
*/
18+
public static final String VALIDATION_REGEX = "[^\\s].*";
19+
20+
public final String value;
21+
22+
/**
23+
* Constructs an {@code Ingredients}.
24+
*
25+
* @param ingredients A valid ingredient string.
26+
*/
27+
public Ingredients(String ingredients) {
28+
requireNonNull(ingredients);
29+
checkArgument(isValidIngredients(ingredients), MESSAGE_CONSTRAINTS);
30+
value = ingredients;
31+
}
32+
33+
/**
34+
* Returns true if a given string is a valid email.
35+
*/
36+
public static boolean isValidIngredients(String test) {
37+
return test.matches(VALIDATION_REGEX);
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return value;
43+
}
44+
45+
@Override
46+
public boolean equals(Object other) {
47+
return other == this // short circuit if same object
48+
|| (other instanceof Ingredients // instanceof handles nulls
49+
&& value.equals(((Ingredients) other).value)); // state check
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return value.hashCode();
55+
}
56+
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package seedu.address.model.recipe;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.commons.util.AppUtil.checkArgument;
5+
6+
/**
7+
* Represents a Recipe's instructions in the recipe book.
8+
* Guarantees: immutable; is valid as declared in {@link #isValidInstructions(String)}
9+
*/
10+
public class Instructions {
11+
12+
public static final String MESSAGE_CONSTRAINTS = "Instructions can take any values, and it should not be blank";
13+
14+
/*
15+
* The first character of the instructions must not be a whitespace,
16+
* otherwise " " (a blank string) becomes a valid input.
17+
*/
18+
public static final String VALIDATION_REGEX = "[^\\s].*";
19+
20+
public final String value;
21+
22+
/**
23+
* Constructs an {@code Instructions}.
24+
*
25+
* @param instructions A valid instruction string.
26+
*/
27+
public Instructions(String instructions) {
28+
requireNonNull(instructions);
29+
checkArgument(isValidInstructions(instructions), MESSAGE_CONSTRAINTS);
30+
value = instructions;
31+
}
32+
33+
/**
34+
* Returns true if a given string is a valid instruction.
35+
*/
36+
public static boolean isValidInstructions(String test) {
37+
return test.matches(VALIDATION_REGEX);
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return value;
43+
}
44+
45+
@Override
46+
public boolean equals(Object other) {
47+
return other == this // short circuit if same object
48+
|| (other instanceof Instructions // instanceof handles nulls
49+
&& value.equals(((Instructions) other).value)); // state check
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return value.hashCode();
55+
}
56+
57+
}

src/main/java/seedu/address/model/recipe/Name.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import static seedu.address.commons.util.AppUtil.checkArgument;
55

66
/**
7-
* Represents a Recipe's name in the address book.
7+
* Represents a Recipe's name in the recipe book.
88
* Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
99
*/
1010
public class Name {
@@ -13,12 +13,12 @@ public class Name {
1313
"Names should only contain alphanumeric characters and spaces, and it should not be blank";
1414

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

21-
public final String fullName;
21+
public final String name;
2222

2323
/**
2424
* Constructs a {@code Name}.
@@ -28,7 +28,7 @@ public class Name {
2828
public Name(String name) {
2929
requireNonNull(name);
3030
checkArgument(isValidName(name), MESSAGE_CONSTRAINTS);
31-
fullName = name;
31+
this.name = name;
3232
}
3333

3434
/**
@@ -41,19 +41,19 @@ public static boolean isValidName(String test) {
4141

4242
@Override
4343
public String toString() {
44-
return fullName;
44+
return name;
4545
}
4646

4747
@Override
4848
public boolean equals(Object other) {
4949
return other == this // short circuit if same object
5050
|| (other instanceof Name // instanceof handles nulls
51-
&& fullName.equals(((Name) other).fullName)); // state check
51+
&& name.equals(((Name) other).name)); // state check
5252
}
5353

5454
@Override
5555
public int hashCode() {
56-
return fullName.hashCode();
56+
return name.hashCode();
5757
}
5858

5959
}

src/main/java/seedu/address/model/recipe/Recipe.java

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,55 @@
22

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

5-
import java.util.Collections;
6-
import java.util.HashSet;
75
import java.util.Objects;
8-
import java.util.Set;
9-
10-
import seedu.address.model.tag.Tag;
116

127
/**
13-
* Represents a Recipe in the address book.
8+
* Represents a Recipe in the recipe book.
149
* Guarantees: details are present and not null, field values are validated, immutable.
1510
*/
1611
public class Recipe {
17-
1812
// Identity fields
1913
private final Name name;
20-
private final Phone phone;
21-
private final Email email;
2214

2315
// Data fields
24-
private final Address address;
25-
private final Set<Tag> tags = new HashSet<>();
16+
private final Ingredients ingredients;
17+
private final Instructions instructions;
2618

27-
/**
28-
* Every field must be present and not null.
29-
*/
30-
public Recipe(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
31-
requireAllNonNull(name, phone, email, address, tags);
19+
20+
public Recipe(Name name, Ingredients ingredients, Instructions instructions) {
21+
requireAllNonNull(name, ingredients, instructions);
3222
this.name = name;
33-
this.phone = phone;
34-
this.email = email;
35-
this.address = address;
36-
this.tags.addAll(tags);
23+
this.ingredients = ingredients;
24+
this.instructions = instructions;
3725
}
3826

3927
public Name getName() {
4028
return name;
4129
}
4230

43-
public Phone getPhone() {
44-
return phone;
45-
}
46-
47-
public Email getEmail() {
48-
return email;
49-
}
50-
51-
public Address getAddress() {
52-
return address;
31+
public Ingredients getIngredients() {
32+
return ingredients;
5333
}
5434

55-
/**
56-
* Returns an immutable tag set, which throws {@code UnsupportedOperationException}
57-
* if modification is attempted.
58-
*/
59-
public Set<Tag> getTags() {
60-
return Collections.unmodifiableSet(tags);
35+
public Instructions getInstructions() {
36+
return instructions;
6137
}
6238

6339
/**
64-
* Returns true if both persons of the same name have at least one other identity field that is the same.
65-
* This defines a weaker notion of equality between two persons.
40+
* Returns true if both recipes have the same name.
6641
*/
67-
public boolean isSamePerson(Recipe otherRecipe) {
42+
public boolean isSameRecipe(Recipe otherRecipe) {
6843
if (otherRecipe == this) {
6944
return true;
7045
}
7146

7247
return otherRecipe != null
73-
&& otherRecipe.getName().equals(getName())
74-
&& (otherRecipe.getPhone().equals(getPhone()) || otherRecipe.getEmail().equals(getEmail()));
48+
&& otherRecipe.getName().equals(getName());
7549
}
7650

7751
/**
78-
* Returns true if both persons have the same identity and data fields.
79-
* This defines a stronger notion of equality between two persons.
52+
* Returns true if both recipes have the same identity and data fields.
53+
* This defines a stronger notion of equality between two recipes.
8054
*/
8155
@Override
8256
public boolean equals(Object other) {
@@ -90,31 +64,24 @@ public boolean equals(Object other) {
9064

9165
Recipe otherRecipe = (Recipe) other;
9266
return otherRecipe.getName().equals(getName())
93-
&& otherRecipe.getPhone().equals(getPhone())
94-
&& otherRecipe.getEmail().equals(getEmail())
95-
&& otherRecipe.getAddress().equals(getAddress())
96-
&& otherRecipe.getTags().equals(getTags());
67+
&& otherRecipe.getIngredients().equals(getIngredients())
68+
&& otherRecipe.getInstructions().equals(getInstructions());
9769
}
9870

9971
@Override
10072
public int hashCode() {
10173
// use this method for custom fields hashing instead of implementing your own
102-
return Objects.hash(name, phone, email, address, tags);
74+
return Objects.hash(name, ingredients, instructions);
10375
}
10476

10577
@Override
10678
public String toString() {
10779
final StringBuilder builder = new StringBuilder();
10880
builder.append(getName())
109-
.append(" Phone: ")
110-
.append(getPhone())
111-
.append(" Email: ")
112-
.append(getEmail())
113-
.append(" Address: ")
114-
.append(getAddress())
115-
.append(" Tags: ");
116-
getTags().forEach(builder::append);
81+
.append(" Ingredients: ")
82+
.append(getIngredients())
83+
.append(" Instructions: ")
84+
.append(getInstructions());
11785
return builder.toString();
11886
}
119-
12087
}

src/main/java/seedu/address/model/recipe/UniqueRecipeList.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
import seedu.address.model.recipe.exceptions.RecipeNotFoundException;
1313

1414
/**
15-
* A list of persons that enforces uniqueness between its elements and does not allow nulls.
16-
* A recipe is considered unique by comparing using {@code Recipe#isSamePerson(Recipe)}. As such, adding and updating of
17-
* persons uses Recipe#isSamePerson(Recipe) for equality so as to ensure that the recipe being added or updated is
15+
* A list of recipes that enforces uniqueness between its elements and does not allow nulls.
16+
* A recipe is considered unique by comparing using {@code Recipe#isSameRecipe(Recipe)}. As such, adding and updating of
17+
* recipes uses Recipe#isSameRecipe(Recipe) for equality so as to ensure that the recipe being added or updated is
1818
* unique in terms of identity in the UniqueRecipeList. However, the removal of a recipe uses Recipe#equals(Object) so
1919
* as to ensure that the recipe with exactly the same fields will be removed.
2020
*
2121
* Supports a minimal set of list operations.
2222
*
23-
* @see Recipe#isSamePerson(Recipe)
23+
* @see Recipe#isSameRecipe(Recipe)
2424
*/
2525
public class UniqueRecipeList implements Iterable<Recipe> {
2626

@@ -33,7 +33,7 @@ public class UniqueRecipeList implements Iterable<Recipe> {
3333
*/
3434
public boolean contains(Recipe toCheck) {
3535
requireNonNull(toCheck);
36-
return internalList.stream().anyMatch(toCheck::isSamePerson);
36+
return internalList.stream().anyMatch(toCheck::isSameRecipe);
3737
}
3838

3939
/**
@@ -53,15 +53,15 @@ public void add(Recipe toAdd) {
5353
* {@code target} must exist in the list.
5454
* The recipe identity of {@code editedRecipe} must not be the same as another existing recipe in the list.
5555
*/
56-
public void setPerson(Recipe target, Recipe editedRecipe) {
56+
public void setRecipe(Recipe target, Recipe editedRecipe) {
5757
requireAllNonNull(target, editedRecipe);
5858

5959
int index = internalList.indexOf(target);
6060
if (index == -1) {
6161
throw new RecipeNotFoundException();
6262
}
6363

64-
if (!target.isSamePerson(editedRecipe) && contains(editedRecipe)) {
64+
if (!target.isSameRecipe(editedRecipe) && contains(editedRecipe)) {
6565
throw new DuplicateRecipeException();
6666
}
6767

@@ -79,7 +79,7 @@ public void remove(Recipe toRemove) {
7979
}
8080
}
8181

82-
public void setPersons(UniqueRecipeList replacement) {
82+
public void setRecipes(UniqueRecipeList replacement) {
8383
requireNonNull(replacement);
8484
internalList.setAll(replacement.internalList);
8585
}
@@ -88,9 +88,9 @@ public void setPersons(UniqueRecipeList replacement) {
8888
* Replaces the contents of this list with {@code recipes}.
8989
* {@code recipes} must not contain duplicate recipes.
9090
*/
91-
public void setPersons(List<Recipe> recipes) {
91+
public void setRecipes(List<Recipe> recipes) {
9292
requireAllNonNull(recipes);
93-
if (!personsAreUnique(recipes)) {
93+
if (!recipesAreUnique(recipes)) {
9494
throw new DuplicateRecipeException();
9595
}
9696

@@ -113,7 +113,7 @@ public Iterator<Recipe> iterator() {
113113
public boolean equals(Object other) {
114114
return other == this // short circuit if same object
115115
|| (other instanceof UniqueRecipeList // instanceof handles nulls
116-
&& internalList.equals(((UniqueRecipeList) other).internalList));
116+
&& internalList.equals(((UniqueRecipeList) other).internalList));
117117
}
118118

119119
@Override
@@ -124,10 +124,10 @@ public int hashCode() {
124124
/**
125125
* Returns true if {@code recipes} contains only unique recipes.
126126
*/
127-
private boolean personsAreUnique(List<Recipe> recipes) {
127+
private boolean recipesAreUnique(List<Recipe> recipes) {
128128
for (int i = 0; i < recipes.size() - 1; i++) {
129129
for (int j = i + 1; j < recipes.size(); j++) {
130-
if (recipes.get(i).isSamePerson(recipes.get(j))) {
130+
if (recipes.get(i).isSameRecipe(recipes.get(j))) {
131131
return false;
132132
}
133133
}

0 commit comments

Comments
 (0)