Skip to content

Commit

Permalink
Add Budget class for Buyer class
Browse files Browse the repository at this point in the history
Addition of the Budget class is necessary to represent the budget
attribute for Buyer objects.

Implemented the Budget class to encapsulate the budget information
for buyers, allowing for better organization and management of
budget-related data.

Using a separate class for budget ensures separation of concerns and
follows the single responsibility principle, improving code
readability and maintainability.

This commit also includes necessary adjustments to integrate the
Budget class with the Buyer class.
  • Loading branch information
felixchanyy committed Mar 28, 2024
1 parent 0658640 commit feaef10
Show file tree
Hide file tree
Showing 21 changed files with 309 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BUDGET;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_HOUSING_TYPE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
Expand All @@ -26,12 +27,14 @@ public class AddBuyerCommand extends Command {
+ PREFIX_PHONE + "PHONE "
+ PREFIX_EMAIL + "EMAIL "
+ PREFIX_HOUSING_TYPE + "HOUSING_TYPE "
+ PREFIX_BUDGET + "BUDGET "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "98765432 "
+ PREFIX_EMAIL + "johnd@example.com "
+ PREFIX_HOUSING_TYPE + "HDB "
+ PREFIX_BUDGET + "99999900 "
+ PREFIX_TAG + "friends "
+ PREFIX_TAG + "owesMoney";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BUDGET;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_HOUSING_TYPE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
Expand All @@ -12,6 +13,7 @@

import seedu.address.logic.commands.AddBuyerCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Budget;
import seedu.address.model.person.Buyer;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand All @@ -31,21 +33,24 @@ public class AddBuyerCommandParser implements Parser<AddBuyerCommand> {
public AddBuyerCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_HOUSING_TYPE,
PREFIX_TAG);
PREFIX_BUDGET, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_HOUSING_TYPE)
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_HOUSING_TYPE,
PREFIX_BUDGET)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddBuyerCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_HOUSING_TYPE);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_HOUSING_TYPE,
PREFIX_BUDGET);
Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
String housingType = ParserUtil.parseHousing(argMultimap.getValue(PREFIX_HOUSING_TYPE).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Budget budget = ParserUtil.parseBudget(argMultimap.getValue(PREFIX_BUDGET).get());

Buyer buyer = new Buyer(name, phone, email, housingType, tagList);
Buyer buyer = new Buyer(name, phone, email, housingType, budget, tagList);
return new AddBuyerCommand(buyer);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public class CliSyntax {
public static final Prefix PREFIX_UNITNUMBER = new Prefix("unitNo/");
public static final Prefix PREFIX_BLOCK = new Prefix("blk/");
public static final Prefix PREFIX_POSTALCODE = new Prefix("postal/");
public static final Prefix PREFIX_BUDGET = new Prefix("b/");
}
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import seedu.address.model.house.Street;
import seedu.address.model.house.UnitNumber;
import seedu.address.model.person.Address;
import seedu.address.model.person.Budget;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
Expand Down Expand Up @@ -217,4 +218,19 @@ public static PostalCode parsePostalCode(String postalCode) throws ParseExceptio
}
return new PostalCode(trimmedPostalCode);
}

/**
* Parses a {@code String budget} into an {@code Budget}
* Loading and trailing whitespaces will be trimmed
*
* @throws ParseException if the given {@code budget} is invalid
*/
public static Budget parseBudget(String budget) throws ParseException {
requireNonNull(budget);
String trimmedBudget = budget.trim();
if (!Budget.isValidBudget(trimmedBudget)) {
throw new ParseException(Budget.MESSAGE_CONSTRAINTS);

Check warning on line 232 in src/main/java/seedu/address/logic/parser/ParserUtil.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L232 was not covered by tests
}
return new Budget(trimmedBudget);
}
}
58 changes: 58 additions & 0 deletions src/main/java/seedu/address/model/person/Budget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package seedu.address.model.person;

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

/**
* Represents a Buyer's budget in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidBudget(String)}
*/
public class Budget {

public static final String MESSAGE_CONSTRAINTS = "Budget should be a positive number.";
public static final String VALIDATION_REGEX = "\\d+(\\.\\d+)?";
public final String value;

/**
* Constructs a {@code Budget}.
*
* @param budget A valid budget amount.
*/
public Budget(String budget) {
requireNonNull(budget);
checkArgument(isValidBudget(budget), MESSAGE_CONSTRAINTS);
value = budget;
}

/**
* Returns true if a given string is a valid budget amount.
*/
public static boolean isValidBudget(String test) {
return test.matches(VALIDATION_REGEX) && Double.parseDouble(test) >= 0;
}

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

@Override
public boolean equals(Object other) {
if (other == this) {
return true;

Check warning on line 42 in src/main/java/seedu/address/model/person/Budget.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Budget.java#L42

Added line #L42 was not covered by tests
}

// instanceof handles nulls
if (!(other instanceof Budget)) {
return false;

Check warning on line 47 in src/main/java/seedu/address/model/person/Budget.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Budget.java#L47

Added line #L47 was not covered by tests
}

Budget otherBudget = (Budget) other;
return value.equals(otherBudget.value);
}

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

Check warning on line 56 in src/main/java/seedu/address/model/person/Budget.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Budget.java#L56

Added line #L56 was not covered by tests
}
}
48 changes: 47 additions & 1 deletion src/main/java/seedu/address/model/person/Buyer.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package seedu.address.model.person;

import java.util.Objects;
import java.util.Set;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.tag.Tag;

/**
* Represents a buyer in the address book.
*/
public class Buyer extends Person {

private final Budget budget;

/**
* Constructs a new Buyer instance without specifying a house. Default constructor.
*
Expand All @@ -17,8 +21,50 @@ public class Buyer extends Person {
* @param email The email address of the buyer.
* @param housingType The type of housing the buyer wants.
* @param tags The tags associated with the buyer.
* @param budget The budget of the buyer.
*/
public Buyer(Name name, Phone phone, Email email, String housingType, Set<Tag> tags) {
public Buyer(Name name, Phone phone, Email email, String housingType, Budget budget, Set<Tag> tags) {
super(name, phone, email, housingType, tags);
this.budget = budget;
}

public Budget getBudget() {
return budget;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}

// instanceof handles nulls
if (!(other instanceof Buyer)) {
return false;
}

if (!super.equals(other)) {
return false;
}

Buyer buyer = (Buyer) other;
return Objects.equals(budget, buyer.budget);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), budget);

Check warning on line 56 in src/main/java/seedu/address/model/person/Buyer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Buyer.java#L56

Added line #L56 was not covered by tests
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("name", getName())
.add("phone", getPhone())
.add("email", getEmail())
.add("housingType", getHousingType())
.add("budget", budget)
.add("tags", getTags())
.toString();
}
}
7 changes: 4 additions & 3 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import seedu.address.model.house.PostalCode;
import seedu.address.model.house.Street;
import seedu.address.model.house.UnitNumber;
import seedu.address.model.person.Budget;
import seedu.address.model.person.Buyer;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand All @@ -28,13 +29,13 @@ public class SampleDataUtil {
public static Buyer[] getSampleBuyers() {
return new Buyer[] {
new Buyer(new Name("Alex Yeoh"), new Phone("87438807"),
new Email("alexyeoh@example.com"), "HDB",
new Email("alexyeoh@example.com"), "HDB", new Budget("100000"),
getTagSet("friends")),
new Buyer(new Name("Bernice Yu"), new Phone("99272758"),
new Email("berniceyu@example.com"), "Condominium",
new Email("berniceyu@example.com"), "Condominium", new Budget("200000"),
getTagSet("colleagues", "friends")),
new Buyer(new Name("Charlotte Oliveiro"), new Phone("93210283"),
new Email("charlotte@example.com"), "HDB",
new Email("charlotte@example.com"), "HDB", new Budget("300000"),
getTagSet("neighbours")),
};
}
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/seedu/address/storage/JsonAdaptedBuyer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.person.Budget;
import seedu.address.model.person.Buyer;
import seedu.address.model.person.Person;

Expand All @@ -15,6 +16,8 @@
*/
public class JsonAdaptedBuyer extends JsonAdaptedPerson {

private final String budget;

/**
* Constructs a {@code JsonAdaptedBuyer}, extends from JsonAdaptedPerson
*/
Expand All @@ -23,24 +26,36 @@ public JsonAdaptedBuyer(@JsonProperty("name") String name,
@JsonProperty("phone") String phone,
@JsonProperty("email") String email,
@JsonProperty("housingType") String housingType,
@JsonProperty("budget") String budget,
@JsonProperty("tags") List<JsonAdaptedTag> tags) {
super(name, phone, email, housingType, tags);
this.budget = budget;
}

/**
* Converts a given {@code JsonAdaptedBuyer} into this class for Jackson use.
*/
public JsonAdaptedBuyer(Buyer source) {
super(source);
budget = source.getBudget().value;
}

/**
* Converts this Jackson-friendly adapted buyer object into the model's {@code Buyer} object.
*/
@Override
public Buyer toModelType() throws IllegalValueException {

if (budget == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Budget.class.getSimpleName()));
}
if (!Budget.isValidBudget(budget)) {
throw new IllegalValueException(Budget.MESSAGE_CONSTRAINTS);
}
final Budget modelBudget = new Budget(budget);

Person person = super.toModelType();
return new Buyer(person.getName(), person.getPhone(), person.getEmail(),
person.getHousingType(), new HashSet<>(person.getTags()));
person.getHousingType(), modelBudget, new HashSet<>(person.getTags()));
}
}
7 changes: 7 additions & 0 deletions src/main/java/seedu/address/ui/PersonCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.model.house.Block;
import seedu.address.model.house.Level;
import seedu.address.model.house.NonLanded;
import seedu.address.model.person.Buyer;
import seedu.address.model.person.Person;
import seedu.address.model.person.Seller;

Expand All @@ -34,6 +35,8 @@ public class PersonCard extends UiPart<Region> {
@FXML
private Label housingType;
@FXML
private Label budget;
@FXML
private Label email;
@FXML
private Label postalCode;
Expand All @@ -59,6 +62,8 @@ public PersonCard(Person person, int displayedIndex) {

// Check if person is a Seller and display houses (For now, we assume only have seller have house)
if (person instanceof Seller) {
// Show no budget
budget.setVisible(false);

Check warning on line 66 in src/main/java/seedu/address/ui/PersonCard.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L66 was not covered by tests
Seller seller = (Seller) person;
if (seller.getHouses() != null) {
seller.getHouses().forEach(house -> {
Expand Down Expand Up @@ -92,6 +97,8 @@ public PersonCard(Person person, int displayedIndex) {
} else {
// We assumed that buyer does not have a house for now
housesContainer.setVisible(false);
Buyer buyer = (Buyer) person;
budget.setText("$" + buyer.getBudget().toString());

Check warning on line 101 in src/main/java/seedu/address/ui/PersonCard.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/ui/PersonCard.java#L100-L101

Added lines #L100 - L101 were not covered by tests
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/view/PersonListCard.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<Label fx:id="phone" styleClass="cell_small_label" text="\$phone"/>
<Label fx:id="email" styleClass="cell_small_label" text="\$email"/>
<Label fx:id="housingType" styleClass="cell_small_label" text="\$housingType"/>
<Label fx:id="budget" styleClass="cell_small_label" text="\$budget"/>
</VBox>
<!-- New VBox for displaying houses -->
<VBox fx:id="housesContainer" alignment="TOP_LEFT" GridPane.columnIndex="1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"phone" : "94351253",
"email" : "alice@example.com",
"housingType" : "HDB",
"budget": "22200",
"tags" : [ "friends" ]
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@
} ],
"buyers" : [ {
"name" : "Ben Ten",
"phone" : "22222222",
"email" : "bob@example.com",
"phone" : "33333333",
"email" : "ben@example.com",
"housingType" : "Condominium",
"budget": "123000",
"tags" : [ "friend", "husband" ]
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.Assert.assertThrows;
import static seedu.address.testutil.TypicalPersons.ALI;
import static seedu.address.testutil.TypicalPersons.ALI_BUYER;

import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -79,8 +79,8 @@ public void equals() {

@Test
public void toStringMethod() {
AddBuyerCommand addBuyerCommand = new AddBuyerCommand(ALI);
String expected = AddBuyerCommand.class.getCanonicalName() + "{buyerToAdd=" + ALI + "}";
AddBuyerCommand addBuyerCommand = new AddBuyerCommand(ALI_BUYER);
String expected = AddBuyerCommand.class.getCanonicalName() + "{buyerToAdd=" + ALI_BUYER + "}";
assertEquals(expected, addBuyerCommand.toString());
}

Expand Down
Loading

0 comments on commit feaef10

Please sign in to comment.