Skip to content

Commit dcb3bc9

Browse files
authored
Merge pull request #79 from felixchanyy/branch-buyer-add-budget
Add Budget class for Buyer class
2 parents 0658640 + feaef10 commit dcb3bc9

21 files changed

+309
-67
lines changed

src/main/java/seedu/address/logic/commands/AddBuyerCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package seedu.address.logic.commands;
22

33
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_BUDGET;
45
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
56
import static seedu.address.logic.parser.CliSyntax.PREFIX_HOUSING_TYPE;
67
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
@@ -26,12 +27,14 @@ public class AddBuyerCommand extends Command {
2627
+ PREFIX_PHONE + "PHONE "
2728
+ PREFIX_EMAIL + "EMAIL "
2829
+ PREFIX_HOUSING_TYPE + "HOUSING_TYPE "
30+
+ PREFIX_BUDGET + "BUDGET "
2931
+ "[" + PREFIX_TAG + "TAG]...\n"
3032
+ "Example: " + COMMAND_WORD + " "
3133
+ PREFIX_NAME + "John Doe "
3234
+ PREFIX_PHONE + "98765432 "
3335
+ PREFIX_EMAIL + "johnd@example.com "
3436
+ PREFIX_HOUSING_TYPE + "HDB "
37+
+ PREFIX_BUDGET + "99999900 "
3538
+ PREFIX_TAG + "friends "
3639
+ PREFIX_TAG + "owesMoney";
3740

src/main/java/seedu/address/logic/parser/AddBuyerCommandParser.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package seedu.address.logic.parser;
22

33
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
4+
import static seedu.address.logic.parser.CliSyntax.PREFIX_BUDGET;
45
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
56
import static seedu.address.logic.parser.CliSyntax.PREFIX_HOUSING_TYPE;
67
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
@@ -12,6 +13,7 @@
1213

1314
import seedu.address.logic.commands.AddBuyerCommand;
1415
import seedu.address.logic.parser.exceptions.ParseException;
16+
import seedu.address.model.person.Budget;
1517
import seedu.address.model.person.Buyer;
1618
import seedu.address.model.person.Email;
1719
import seedu.address.model.person.Name;
@@ -31,21 +33,24 @@ public class AddBuyerCommandParser implements Parser<AddBuyerCommand> {
3133
public AddBuyerCommand parse(String args) throws ParseException {
3234
ArgumentMultimap argMultimap =
3335
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_HOUSING_TYPE,
34-
PREFIX_TAG);
36+
PREFIX_BUDGET, PREFIX_TAG);
3537

36-
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_HOUSING_TYPE)
38+
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_HOUSING_TYPE,
39+
PREFIX_BUDGET)
3740
|| !argMultimap.getPreamble().isEmpty()) {
3841
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddBuyerCommand.MESSAGE_USAGE));
3942
}
4043

41-
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_HOUSING_TYPE);
44+
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_HOUSING_TYPE,
45+
PREFIX_BUDGET);
4246
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
4347
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
4448
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
4549
String housingType = ParserUtil.parseHousing(argMultimap.getValue(PREFIX_HOUSING_TYPE).get());
4650
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
51+
Budget budget = ParserUtil.parseBudget(argMultimap.getValue(PREFIX_BUDGET).get());
4752

48-
Buyer buyer = new Buyer(name, phone, email, housingType, tagList);
53+
Buyer buyer = new Buyer(name, phone, email, housingType, budget, tagList);
4954
return new AddBuyerCommand(buyer);
5055
}
5156

src/main/java/seedu/address/logic/parser/CliSyntax.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ public class CliSyntax {
1616
public static final Prefix PREFIX_UNITNUMBER = new Prefix("unitNo/");
1717
public static final Prefix PREFIX_BLOCK = new Prefix("blk/");
1818
public static final Prefix PREFIX_POSTALCODE = new Prefix("postal/");
19+
public static final Prefix PREFIX_BUDGET = new Prefix("b/");
1920
}

src/main/java/seedu/address/logic/parser/ParserUtil.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import seedu.address.model.house.Street;
1717
import seedu.address.model.house.UnitNumber;
1818
import seedu.address.model.person.Address;
19+
import seedu.address.model.person.Budget;
1920
import seedu.address.model.person.Email;
2021
import seedu.address.model.person.Name;
2122
import seedu.address.model.person.Phone;
@@ -217,4 +218,19 @@ public static PostalCode parsePostalCode(String postalCode) throws ParseExceptio
217218
}
218219
return new PostalCode(trimmedPostalCode);
219220
}
221+
222+
/**
223+
* Parses a {@code String budget} into an {@code Budget}
224+
* Loading and trailing whitespaces will be trimmed
225+
*
226+
* @throws ParseException if the given {@code budget} is invalid
227+
*/
228+
public static Budget parseBudget(String budget) throws ParseException {
229+
requireNonNull(budget);
230+
String trimmedBudget = budget.trim();
231+
if (!Budget.isValidBudget(trimmedBudget)) {
232+
throw new ParseException(Budget.MESSAGE_CONSTRAINTS);
233+
}
234+
return new Budget(trimmedBudget);
235+
}
220236
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package seedu.address.model.person;
2+
3+
import static java.util.Objects.requireNonNull;
4+
import static seedu.address.commons.util.AppUtil.checkArgument;
5+
6+
/**
7+
* Represents a Buyer's budget in the address book.
8+
* Guarantees: immutable; is valid as declared in {@link #isValidBudget(String)}
9+
*/
10+
public class Budget {
11+
12+
public static final String MESSAGE_CONSTRAINTS = "Budget should be a positive number.";
13+
public static final String VALIDATION_REGEX = "\\d+(\\.\\d+)?";
14+
public final String value;
15+
16+
/**
17+
* Constructs a {@code Budget}.
18+
*
19+
* @param budget A valid budget amount.
20+
*/
21+
public Budget(String budget) {
22+
requireNonNull(budget);
23+
checkArgument(isValidBudget(budget), MESSAGE_CONSTRAINTS);
24+
value = budget;
25+
}
26+
27+
/**
28+
* Returns true if a given string is a valid budget amount.
29+
*/
30+
public static boolean isValidBudget(String test) {
31+
return test.matches(VALIDATION_REGEX) && Double.parseDouble(test) >= 0;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return value;
37+
}
38+
39+
@Override
40+
public boolean equals(Object other) {
41+
if (other == this) {
42+
return true;
43+
}
44+
45+
// instanceof handles nulls
46+
if (!(other instanceof Budget)) {
47+
return false;
48+
}
49+
50+
Budget otherBudget = (Budget) other;
51+
return value.equals(otherBudget.value);
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return value.hashCode();
57+
}
58+
}
Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package seedu.address.model.person;
22

3+
import java.util.Objects;
34
import java.util.Set;
45

6+
import seedu.address.commons.util.ToStringBuilder;
57
import seedu.address.model.tag.Tag;
68

79
/**
810
* Represents a buyer in the address book.
911
*/
1012
public class Buyer extends Person {
1113

14+
private final Budget budget;
15+
1216
/**
1317
* Constructs a new Buyer instance without specifying a house. Default constructor.
1418
*
@@ -17,8 +21,50 @@ public class Buyer extends Person {
1721
* @param email The email address of the buyer.
1822
* @param housingType The type of housing the buyer wants.
1923
* @param tags The tags associated with the buyer.
24+
* @param budget The budget of the buyer.
2025
*/
21-
public Buyer(Name name, Phone phone, Email email, String housingType, Set<Tag> tags) {
26+
public Buyer(Name name, Phone phone, Email email, String housingType, Budget budget, Set<Tag> tags) {
2227
super(name, phone, email, housingType, tags);
28+
this.budget = budget;
29+
}
30+
31+
public Budget getBudget() {
32+
return budget;
33+
}
34+
35+
@Override
36+
public boolean equals(Object other) {
37+
if (this == other) {
38+
return true;
39+
}
40+
41+
// instanceof handles nulls
42+
if (!(other instanceof Buyer)) {
43+
return false;
44+
}
45+
46+
if (!super.equals(other)) {
47+
return false;
48+
}
49+
50+
Buyer buyer = (Buyer) other;
51+
return Objects.equals(budget, buyer.budget);
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return Objects.hash(super.hashCode(), budget);
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return new ToStringBuilder(this)
62+
.add("name", getName())
63+
.add("phone", getPhone())
64+
.add("email", getEmail())
65+
.add("housingType", getHousingType())
66+
.add("budget", budget)
67+
.add("tags", getTags())
68+
.toString();
2369
}
2470
}

src/main/java/seedu/address/model/util/SampleDataUtil.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import seedu.address.model.house.PostalCode;
1515
import seedu.address.model.house.Street;
1616
import seedu.address.model.house.UnitNumber;
17+
import seedu.address.model.person.Budget;
1718
import seedu.address.model.person.Buyer;
1819
import seedu.address.model.person.Email;
1920
import seedu.address.model.person.Name;
@@ -28,13 +29,13 @@ public class SampleDataUtil {
2829
public static Buyer[] getSampleBuyers() {
2930
return new Buyer[] {
3031
new Buyer(new Name("Alex Yeoh"), new Phone("87438807"),
31-
new Email("alexyeoh@example.com"), "HDB",
32+
new Email("alexyeoh@example.com"), "HDB", new Budget("100000"),
3233
getTagSet("friends")),
3334
new Buyer(new Name("Bernice Yu"), new Phone("99272758"),
34-
new Email("berniceyu@example.com"), "Condominium",
35+
new Email("berniceyu@example.com"), "Condominium", new Budget("200000"),
3536
getTagSet("colleagues", "friends")),
3637
new Buyer(new Name("Charlotte Oliveiro"), new Phone("93210283"),
37-
new Email("charlotte@example.com"), "HDB",
38+
new Email("charlotte@example.com"), "HDB", new Budget("300000"),
3839
getTagSet("neighbours")),
3940
};
4041
}

src/main/java/seedu/address/storage/JsonAdaptedBuyer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.annotation.JsonProperty;
88

99
import seedu.address.commons.exceptions.IllegalValueException;
10+
import seedu.address.model.person.Budget;
1011
import seedu.address.model.person.Buyer;
1112
import seedu.address.model.person.Person;
1213

@@ -15,6 +16,8 @@
1516
*/
1617
public class JsonAdaptedBuyer extends JsonAdaptedPerson {
1718

19+
private final String budget;
20+
1821
/**
1922
* Constructs a {@code JsonAdaptedBuyer}, extends from JsonAdaptedPerson
2023
*/
@@ -23,24 +26,36 @@ public JsonAdaptedBuyer(@JsonProperty("name") String name,
2326
@JsonProperty("phone") String phone,
2427
@JsonProperty("email") String email,
2528
@JsonProperty("housingType") String housingType,
29+
@JsonProperty("budget") String budget,
2630
@JsonProperty("tags") List<JsonAdaptedTag> tags) {
2731
super(name, phone, email, housingType, tags);
32+
this.budget = budget;
2833
}
2934

3035
/**
3136
* Converts a given {@code JsonAdaptedBuyer} into this class for Jackson use.
3237
*/
3338
public JsonAdaptedBuyer(Buyer source) {
3439
super(source);
40+
budget = source.getBudget().value;
3541
}
3642

3743
/**
3844
* Converts this Jackson-friendly adapted buyer object into the model's {@code Buyer} object.
3945
*/
4046
@Override
4147
public Buyer toModelType() throws IllegalValueException {
48+
49+
if (budget == null) {
50+
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Budget.class.getSimpleName()));
51+
}
52+
if (!Budget.isValidBudget(budget)) {
53+
throw new IllegalValueException(Budget.MESSAGE_CONSTRAINTS);
54+
}
55+
final Budget modelBudget = new Budget(budget);
56+
4257
Person person = super.toModelType();
4358
return new Buyer(person.getName(), person.getPhone(), person.getEmail(),
44-
person.getHousingType(), new HashSet<>(person.getTags()));
59+
person.getHousingType(), modelBudget, new HashSet<>(person.getTags()));
4560
}
4661
}

src/main/java/seedu/address/ui/PersonCard.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import seedu.address.model.house.Block;
1212
import seedu.address.model.house.Level;
1313
import seedu.address.model.house.NonLanded;
14+
import seedu.address.model.person.Buyer;
1415
import seedu.address.model.person.Person;
1516
import seedu.address.model.person.Seller;
1617

@@ -34,6 +35,8 @@ public class PersonCard extends UiPart<Region> {
3435
@FXML
3536
private Label housingType;
3637
@FXML
38+
private Label budget;
39+
@FXML
3740
private Label email;
3841
@FXML
3942
private Label postalCode;
@@ -59,6 +62,8 @@ public PersonCard(Person person, int displayedIndex) {
5962

6063
// Check if person is a Seller and display houses (For now, we assume only have seller have house)
6164
if (person instanceof Seller) {
65+
// Show no budget
66+
budget.setVisible(false);
6267
Seller seller = (Seller) person;
6368
if (seller.getHouses() != null) {
6469
seller.getHouses().forEach(house -> {
@@ -92,6 +97,8 @@ public PersonCard(Person person, int displayedIndex) {
9297
} else {
9398
// We assumed that buyer does not have a house for now
9499
housesContainer.setVisible(false);
100+
Buyer buyer = (Buyer) person;
101+
budget.setText("$" + buyer.getBudget().toString());
95102
}
96103
}
97104
}

src/main/resources/view/PersonListCard.fxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<Label fx:id="phone" styleClass="cell_small_label" text="\$phone"/>
3333
<Label fx:id="email" styleClass="cell_small_label" text="\$email"/>
3434
<Label fx:id="housingType" styleClass="cell_small_label" text="\$housingType"/>
35+
<Label fx:id="budget" styleClass="cell_small_label" text="\$budget"/>
3536
</VBox>
3637
<!-- New VBox for displaying houses -->
3738
<VBox fx:id="housesContainer" alignment="TOP_LEFT" GridPane.columnIndex="1">

src/test/data/JsonSerializableAddressBookTest/duplicatePersonAddressBook.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"phone" : "94351253",
2222
"email" : "alice@example.com",
2323
"housingType" : "HDB",
24+
"budget": "22200",
2425
"tags" : [ "friends" ]
2526
} ]
2627
}

src/test/data/JsonSerializableAddressBookTest/typicalPersonsAddressBook.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@
114114
} ],
115115
"buyers" : [ {
116116
"name" : "Ben Ten",
117-
"phone" : "22222222",
118-
"email" : "bob@example.com",
117+
"phone" : "33333333",
118+
"email" : "ben@example.com",
119119
"housingType" : "Condominium",
120+
"budget": "123000",
120121
"tags" : [ "friend", "husband" ]
121122
} ]
122123
}

src/test/java/seedu/address/logic/commands/AddBuyerCommandTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import static org.junit.jupiter.api.Assertions.assertFalse;
66
import static org.junit.jupiter.api.Assertions.assertTrue;
77
import static seedu.address.testutil.Assert.assertThrows;
8-
import static seedu.address.testutil.TypicalPersons.ALI;
8+
import static seedu.address.testutil.TypicalPersons.ALI_BUYER;
99

1010
import java.nio.file.Path;
1111
import java.util.ArrayList;
@@ -79,8 +79,8 @@ public void equals() {
7979

8080
@Test
8181
public void toStringMethod() {
82-
AddBuyerCommand addBuyerCommand = new AddBuyerCommand(ALI);
83-
String expected = AddBuyerCommand.class.getCanonicalName() + "{buyerToAdd=" + ALI + "}";
82+
AddBuyerCommand addBuyerCommand = new AddBuyerCommand(ALI_BUYER);
83+
String expected = AddBuyerCommand.class.getCanonicalName() + "{buyerToAdd=" + ALI_BUYER + "}";
8484
assertEquals(expected, addBuyerCommand.toString());
8585
}
8686

0 commit comments

Comments
 (0)