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 Budget class for Buyer class #79

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it is better to explicitly name the prefix as budget/ ?

Not really a big issue, just a personal preference.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fine I feel, like name is also "n/". We can discuss during meeting.

}
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 @@
}
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can a budget be $0? And is there a minimum and maximum a budget can be?

Things to consider, depends on our team requirement (need to ask them).

}

@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 @@
* @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 @@
@FXML
private Label housingType;
@FXML
private Label budget;
@FXML
private Label email;
@FXML
private Label postalCode;
Expand All @@ -59,6 +62,8 @@

// 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 @@
} 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
Loading