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

Implement Blank class to allow for optional Person fields #219

Merged
merged 10 commits into from
Nov 5, 2024
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
11 changes: 6 additions & 5 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,24 @@ public AddCommand parse(String args) throws ParseException {
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG,
PREFIX_GAME, PREFIX_PREFERREDTIME);

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

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS);
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());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());

Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).orElse(""));
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).orElse(""));
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).orElse(""));

Choose a reason for hiding this comment

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

I feel the modifications to ParserUtil is enough to handle these. Maybe can do something like:

Phone phone = ParserUtil.parsePhone(argumentMultimap.getValue(PREFIX_PHONE).orElse(""));

Copy link
Author

Choose a reason for hiding this comment

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

Hmm i didnt realise it was an optional

Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Map<String, Game> gameList = ParserUtil.parseGames(argMultimap.getAllValues(PREFIX_GAME));
Set<PreferredTime> preferredTimeList =
ParserUtil.parsePreferredTimes(argMultimap.getAllValues(PREFIX_PREFERREDTIME));

Person person = new Person(name, phone, email, address, tagList, gameList, preferredTimeList);

return new AddCommand(person);
}

Expand Down
10 changes: 10 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.game.SkillLevel;
import seedu.address.model.game.Username;
import seedu.address.model.person.Address;
import seedu.address.model.person.Blank;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
Expand Down Expand Up @@ -64,6 +65,9 @@ public static Name parseName(String name) throws ParseException {
* @throws ParseException if the given {@code phone} is invalid.
*/
public static Phone parsePhone(String phone) throws ParseException {
if (phone.equals("")) {
return new Phone(new Blank());
}
requireNonNull(phone);
String trimmedPhone = phone.trim();
if (!Phone.isValidPhone(trimmedPhone)) {
Expand All @@ -79,6 +83,9 @@ public static Phone parsePhone(String phone) throws ParseException {
* @throws ParseException if the given {@code address} is invalid.
*/
public static Address parseAddress(String address) throws ParseException {
if (address.equals("")) {
return new Address(new Blank());
}
requireNonNull(address);
String trimmedAddress = address.trim();
if (!Address.isValidAddress(trimmedAddress)) {
Expand All @@ -94,6 +101,9 @@ public static Address parseAddress(String address) throws ParseException {
* @throws ParseException if the given {@code email} is invalid.
*/
public static Email parseEmail(String email) throws ParseException {
if (email.equals("")) {
return new Email(new Blank());
}
requireNonNull(email);
String trimmedEmail = email.trim();
if (!Email.isValidEmail(trimmedEmail)) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/model/person/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public Address(String address) {
value = address;
}

/**
* Constructs an {@code Address}.
*
* @param blank A blank address.
*/
public Address(Blank blank) {
value = "";
}

/**
* Returns true if a given string is a valid email.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/model/person/Blank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package seedu.address.model.person;


/**
* Used to create blank version of {@code Person} fields
*/
public class Blank {
}
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/model/person/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public Email(String email) {
value = email;
}

/**
* Constructs an {@code Email}.
*
* @param blank A blank email.
*/
public Email(Blank blank) {
value = "";
}

/**
* Returns if a given string is a valid email.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/model/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public Phone(String phone) {
value = phone;
}

/**
* Constructs an {@code Phone}.
*
* @param blank A blank phone.
*/
public Phone(Blank blank) {
value = "";
}

/**
* Returns true if a given string is a valid phone number.
*/
Expand Down
56 changes: 33 additions & 23 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.game.Game;
import seedu.address.model.person.Address;
import seedu.address.model.person.Blank;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
Expand Down Expand Up @@ -42,10 +43,10 @@ class JsonAdaptedPerson {
*/
@JsonCreator
public JsonAdaptedPerson(@JsonProperty("name") String name, @JsonProperty("phone") String phone,
@JsonProperty("email") String email, @JsonProperty("address") String address,
@JsonProperty("tags") List<JsonAdaptedTag> tags,
@JsonProperty("games") List<JsonAdaptedGame> games,
@JsonProperty("preferred times") List<JsonAdaptedPreferredTime> preferredTimes) {
@JsonProperty("email") String email, @JsonProperty("address") String address,
@JsonProperty("tags") List<JsonAdaptedTag> tags,
@JsonProperty("games") List<JsonAdaptedGame> games,
@JsonProperty("preferred times") List<JsonAdaptedPreferredTime> preferredTimes) {
this.name = name;
this.phone = phone;
this.email = email;
Expand Down Expand Up @@ -95,7 +96,7 @@ public Person toModelType() throws IllegalValueException {
personGames.put(game.getGameName(), game.toModelType());
}
final List<PreferredTime> personPreferredTimes = new ArrayList<>();
for (JsonAdaptedPreferredTime preferredTime: preferredTimes) {
for (JsonAdaptedPreferredTime preferredTime : preferredTimes) {
personPreferredTimes.add(preferredTime.toModelType());
}

Expand All @@ -107,29 +108,38 @@ public Person toModelType() throws IllegalValueException {
}
final Name modelName = new Name(name);

if (phone == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Phone.class.getSimpleName()));
}
if (!Phone.isValidPhone(phone)) {
throw new IllegalValueException(Phone.MESSAGE_CONSTRAINTS);
}
final Phone modelPhone = new Phone(phone);

if (email == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Email.class.getSimpleName()));
final Phone modelPhone;
if (phone.isEmpty()) {
modelPhone = new Phone(new Blank());
} else {
if (!Phone.isValidPhone(phone)) {
throw new IllegalValueException(Phone.MESSAGE_CONSTRAINTS);
}
modelPhone = new Phone(phone);
}
if (!Email.isValidEmail(email)) {
throw new IllegalValueException(Email.MESSAGE_CONSTRAINTS);
}
final Email modelEmail = new Email(email);

if (address == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Address.class.getSimpleName()));
final Email modelEmail;
if (email.isEmpty()) {
System.out.println("help");
modelEmail = new Email(new Blank());
} else {
if (!Email.isValidEmail(email)) {
throw new IllegalValueException(Email.MESSAGE_CONSTRAINTS);
}
System.out.println("uh oh");
modelEmail = new Email(email);
}
if (!Address.isValidAddress(address)) {
throw new IllegalValueException(Address.MESSAGE_CONSTRAINTS);

final Address modelAddress;
if (address.isEmpty()) {
modelAddress = new Address(new Blank());
} else {
if (!Address.isValidAddress(address)) {
throw new IllegalValueException(Address.MESSAGE_CONSTRAINTS);
}
modelAddress = new Address(address);
}
final Address modelAddress = new Address(address);

final Set<Tag> modelTags = new HashSet<>(personTags);
final Map<String, Game> modelGames = new HashMap<>(personGames);
Expand Down
42 changes: 19 additions & 23 deletions src/main/java/seedu/address/ui/CommandBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Region;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
Expand Down Expand Up @@ -43,32 +42,29 @@ public CommandBox(CommandExecutor commandExecutor) {
commandTextField.textProperty().addListener((unused1, unused2, unused3) -> setStyleToDefault());
}


/**
* initialize() method from javaFx, used to check for arrow inputs inside the commandTextField
* Handles the Up button pressed event.
*/
@FXML
public void initialize() {
commandTextField.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.UP && !event.isShiftDown()) {
String previousCommand = commandHistory.getPreviousCommand();
if (previousCommand != null) {
commandTextField.setText(previousCommand);
commandTextField.positionCaret(previousCommand.length()); // Move cursor to end
}
}
if (event.getCode() == KeyCode.DOWN && !event.isShiftDown()) {
String nextCommand = commandHistory.getNextCommand();
if (nextCommand != null) {
commandTextField.setText(nextCommand);
commandTextField.positionCaret(nextCommand.length()); // Move cursor to end
}
}
if (event.getCode() == KeyCode.ENTER) {
handleCommandEntered();
}
});
public void handleUpEntered() {
String previousCommand = commandHistory.getPreviousCommand();
if (previousCommand != null) {
commandTextField.setText(previousCommand);
commandTextField.positionCaret(previousCommand.length()); // Move cursor to end
}
}
/**
* Handles the Down button pressed event.
*/
@FXML
public void handleDownEntered() {
String nextCommand = commandHistory.getNextCommand();
if (nextCommand != null) {
commandTextField.setText(nextCommand);
commandTextField.positionCaret(nextCommand.length()); // Move cursor to end
}
}


/**
* Handles the Enter button pressed event.
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/ui/CommandHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public String getNextCommand() {
currentHistoryIndex++;
return commandHistory.get(currentHistoryIndex);
}
if (currentHistoryIndex == commandHistory.size() - 1) {
currentHistoryIndex++;
}
return ""; // No next command
}
}
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/ui/PersonCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,23 @@ public PersonCard(Person person, int displayedIndex) {
id.setText(displayedIndex + ". ");
name.setText(person.getName().fullName);
phone.setText(person.getPhone().value);
if (phone.getText().isEmpty()) {
phone.setMaxHeight(0);
phone.setMinHeight(0);
phone.setPrefHeight(0);
}
address.setText(person.getAddress().value);
if (address.getText().isEmpty()) {
address.setMaxHeight(0);
address.setMinHeight(0);
address.setPrefHeight(0);
}
email.setText(person.getEmail().value);
if (email.getText().isEmpty()) {
email.setMaxHeight(0);
email.setMinHeight(0);
email.setPrefHeight(0);
}
person.getTags().stream()
.sorted(Comparator.comparing(tag -> tag.tagName))
.forEach(tag -> tags.getChildren().add(new Label(tag.tagName)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.SortedSet;
import java.util.stream.Collectors;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
Expand Down Expand Up @@ -43,7 +42,6 @@ public class AutoSuggestionTextField extends TextField {
private PopupControl suggestionPopup;
private ListView<TextFlow> suggestionList;
private CommandBox commandBox;

private ObservableList<TextFlow> textFlowItems;

/**
Expand All @@ -66,7 +64,7 @@ public AutoSuggestionTextField() {

// Listen for text changes
textProperty().addListener((obs, oldText, newText) -> {
if (!newText.isEmpty()) {
if (!newText.trim().isEmpty()) {
String[] text = newText.split("\\s+");
String command = text[0];
boolean hasParams = false;
Expand All @@ -81,6 +79,12 @@ public AutoSuggestionTextField() {

// Add key event handler to both the TextField and ListView
EventHandler<KeyEvent> keyEventHandler = event -> {
if (event.getCode() == KeyCode.UP && !event.isShiftDown()) {
commandBox.handleUpEntered();
}
if (event.getCode() == KeyCode.DOWN && !event.isShiftDown()) {
commandBox.handleDownEntered();
}
if (event.isShiftDown()) {
switch (event.getCode()) {
case UP:
Expand All @@ -97,6 +101,8 @@ public AutoSuggestionTextField() {
} else if (event.getCode() == KeyCode.TAB) {
handleTab();
event.consume();
} else if (event.getCode() == KeyCode.ENTER) {
handleCommandEntered();
}
};

Expand All @@ -106,19 +112,13 @@ public AutoSuggestionTextField() {
CommandTextFlow commandFlow = (CommandTextFlow) selectedTextFlow;
setText(commandFlow.getCommandText());
this.positionCaret(commandFlow.getCommandText().length());
suggestionPopup.hide();
}
});

// Add the handler to both the TextField and ListView
this.addEventHandler(KeyEvent.KEY_PRESSED, keyEventHandler);
suggestionList.addEventHandler(KeyEvent.KEY_PRESSED, keyEventHandler);

// Ensure the TextField keeps focus when showing popup
suggestionPopup.setOnShowing(event -> {
Platform.runLater(() -> this.requestFocus());
});

// Prevent ListView from handling its default key events
suggestionList.addEventFilter(KeyEvent.ANY, event -> {
if (event.isControlDown() || (event.isShiftDown() && event.getCode() == KeyCode.ENTER)) {
Expand Down Expand Up @@ -232,7 +232,7 @@ public void hidePopup() {
}

/**
* Sets the CommandBox object that processes commands entered in the TextField.
* Sets the CommandBox object that processes commands entered into the TextField.
*
* @param commandBox The CommandBox object to be set.
*/
Expand Down Expand Up @@ -353,6 +353,7 @@ public void populatePopup(List<String> filteredList, String searchRequest) {
}
}


/**
* Build TextFlow with selected text. Return "case" dependent.
*
Expand Down
Loading