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 Grade Parameter to Person #42

Merged
merged 5 commits into from
Mar 20, 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
27 changes: 25 additions & 2 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.grade.Grade;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand Down Expand Up @@ -100,8 +101,9 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail());
Address updatedAddress = editPersonDescriptor.getAddress().orElse(personToEdit.getAddress());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
Set<Grade> updatedGrades = editPersonDescriptor.getGrades().orElse(personToEdit.getGrades());

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags);
return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags, updatedGrades);
}

@Override
Expand Down Expand Up @@ -139,6 +141,8 @@ public static class EditPersonDescriptor {
private Address address;
private Set<Tag> tags;

private Set<Grade> grades;

public EditPersonDescriptor() {}

/**
Expand All @@ -151,13 +155,14 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setEmail(toCopy.email);
setAddress(toCopy.address);
setTags(toCopy.tags);
setGrades(toCopy.grades);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags);
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags, grades);
}

public void setName(Name name) {
Expand Down Expand Up @@ -209,6 +214,23 @@ public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}

/**
* Sets {@code grades} to this object's {@code grades}.
* A defensive copy of {@code grades} is used internally.
*/
public void setGrades(Set<Grade> grades) {
this.grades = (grades != null) ? new HashSet<>(grades) : null;
}

/**
* Returns an unmodifiable grades set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code grades} is null.
*/
public Optional<Set<Grade>> getGrades() {
return (grades != null) ? Optional.of(Collections.unmodifiableSet(grades)) : Optional.empty();
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down Expand Up @@ -236,6 +258,7 @@ public String toString() {
.add("email", email)
.add("address", address)
.add("tags", tags)
.add("grades", grades)
.toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRADE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
Expand All @@ -12,6 +13,7 @@

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.grade.Grade;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand All @@ -31,7 +33,8 @@ public class AddCommandParser implements Parser<AddCommand> {
*/
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG);
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE,
PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG, PREFIX_GRADE);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
Expand All @@ -44,8 +47,9 @@ public AddCommand parse(String args) throws ParseException {
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Set<Grade> gradeList = ParserUtil.parseGrades(argMultimap.getAllValues(PREFIX_GRADE));

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

return new AddCommand(person);
}
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 @@ -11,5 +11,6 @@ public class CliSyntax {
public static final Prefix PREFIX_EMAIL = new Prefix("e/");
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_GRADE = new Prefix("g/");

}
28 changes: 28 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.grade.Grade;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand Down Expand Up @@ -121,4 +122,31 @@
}
return tagSet;
}

/**
* Parses a {@code String grade} into a {@code grade}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code grade} is invalid.
*/
public static Grade parseGrade(String grade) throws ParseException {
requireNonNull(grade);
String trimmedGrade = grade.trim();

Check warning on line 134 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#L133-L134

Added lines #L133 - L134 were not covered by tests
if (!Grade.isValidGrade(trimmedGrade)) {
throw new ParseException(Tag.MESSAGE_CONSTRAINTS);

Check warning on line 136 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#L136

Added line #L136 was not covered by tests
}
return new Grade(trimmedGrade);

Check warning on line 138 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#L138

Added line #L138 was not covered by tests
}

/**
* Parses {@code Collection<String> grades} into a {@code Set<Grade>}.
*/
public static Set<Grade> parseGrades(Collection<String> grades) throws ParseException {
requireNonNull(grades);
final Set<Grade> gradeSet = new HashSet<>();
for (String grade : grades) {
gradeSet.add(parseGrade(grade));
}

Check warning on line 149 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#L148-L149

Added lines #L148 - L149 were not covered by tests
return gradeSet;
}
}
82 changes: 82 additions & 0 deletions src/main/java/seedu/address/model/grade/Grade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package seedu.address.model.grade;

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

import java.util.Objects;
/**
* Represents a Person's Grade in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidGrade(String)}
*/
public class Grade {

public static final String MESSAGE_CONSTRAINTS = "Grades should be of the format Test: Grade "
+ "and adhere to the following constraints:\n"
+ "1. The Test name should not be empty"
+ "2. This is followed by a ': ' and then a grade. \n"
+ "The grade must:\n"
+ " - represent the percentage gotten in the test rounded to the nearest whole number\n"
+ " - be between [0, 100]\n";

/*
* The first character of the test name must not be a whitespace,
* otherwise " " (a blank string) becomes a valid input.
* Grade should be an integer ranging from 0-100, representing the percentage.
*/
public static final String TEST_NAME_VALIDATION_REGEX = ".+?";
public static final String GRADE_VALIDATION_REGEX = "(100|[1-9]?[0-9])$";

Choose a reason for hiding this comment

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

(Styling) Extra line here can be removed

public static final String VALIDATION_REGEX = TEST_NAME_VALIDATION_REGEX + ":\\s*" + GRADE_VALIDATION_REGEX;

public final String testAndGrade;
public final String testName;
public final String grade;

/**
* Constructs an {@code Grade}.

Choose a reason for hiding this comment

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

Constructs an {@code Grade}.

an -> a

lol sorry😭

*
* @param testAndGrade A valid testAndGrade.
*/
public Grade(String testAndGrade) {
requireNonNull(testAndGrade);
checkArgument(isValidGrade(testAndGrade.trim()), MESSAGE_CONSTRAINTS);
String[] parts = testAndGrade.split(":", 2);
this.testName = parts[0].trim();
this.grade = parts[1].trim();
this.testAndGrade = testAndGrade;
}

/**
* Returns true if a given string is a valid grade.
*/
public static boolean isValidGrade(String test) {
return test.matches(VALIDATION_REGEX);
}


@Override
public String toString() {
return testName + ": " + grade;
}

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

Check warning on line 65 in src/main/java/seedu/address/model/grade/Grade.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/grade/Grade.java#L65

Added line #L65 was not covered by tests
}

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

Check warning on line 70 in src/main/java/seedu/address/model/grade/Grade.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/grade/Grade.java#L70

Added line #L70 was not covered by tests
}

Grade otherGrade = (Grade) other;
return testName.equals(otherGrade.testName) && grade.equals(otherGrade.grade);
}

@Override
public int hashCode() {
return Objects.hash(testName, grade);
}

}
22 changes: 18 additions & 4 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Set;

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

/**
Expand All @@ -25,16 +26,19 @@
private final Address address;
private final Set<Tag> tags = new HashSet<>();

private final Set<Grade> grades = new HashSet<>();

/**
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
requireAllNonNull(name, phone, email, address, tags);
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags, Set<Grade> grades) {
requireAllNonNull(name, phone, email, address, tags, grades);
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
this.tags.addAll(tags);
this.grades.addAll(grades);
}

public Name getName() {
Expand All @@ -61,6 +65,14 @@
return Collections.unmodifiableSet(tags);
}

/**
* Returns an immutable grade set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
*/
public Set<Grade> getGrades() {
return Collections.unmodifiableSet(grades);
}

/**
* Returns true if both persons have the same name.
* This defines a weaker notion of equality between two persons.
Expand Down Expand Up @@ -94,13 +106,14 @@
&& phone.equals(otherPerson.phone)
&& email.equals(otherPerson.email)
&& address.equals(otherPerson.address)
&& tags.equals(otherPerson.tags);
&& tags.equals(otherPerson.tags)
&& grades.equals(otherPerson.grades);
}

@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(name, phone, email, address, tags);
return Objects.hash(name, phone, email, address, tags, grades);

Check warning on line 116 in src/main/java/seedu/address/model/person/Person.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/seedu/address/model/person/Person.java#L116

Added line #L116 was not covered by tests
}

@Override
Expand All @@ -111,6 +124,7 @@
.add("email", email)
.add("address", address)
.add("tags", tags)
.add("grades", grades)
.toString();
}

Expand Down
22 changes: 16 additions & 6 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import seedu.address.model.AddressBook;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.grade.Grade;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
Expand All @@ -21,22 +22,22 @@
return new Person[] {
new Person(new Name("Alex Yeoh"), new Phone("87438807"), new Email("[email protected]"),
new Address("Blk 30 Geylang Street 29, #06-40"),
getTagSet("friends")),
getTagSet("friends"), getGradeSet("ca1: 90")),

Check warning on line 25 in src/main/java/seedu/address/model/util/SampleDataUtil.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L25 was not covered by tests
new Person(new Name("Bernice Yu"), new Phone("99272758"), new Email("[email protected]"),
new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"),
getTagSet("colleagues", "friends")),
getTagSet("colleagues", "friends"), getGradeSet("ca1: 50", "ca2: 80")),

Check warning on line 28 in src/main/java/seedu/address/model/util/SampleDataUtil.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L28 was not covered by tests
new Person(new Name("Charlotte Oliveiro"), new Phone("93210283"), new Email("[email protected]"),
new Address("Blk 11 Ang Mo Kio Street 74, #11-04"),
getTagSet("neighbours")),
getTagSet("neighbours"), getGradeSet("eoy: 70")),

Check warning on line 31 in src/main/java/seedu/address/model/util/SampleDataUtil.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L31 was not covered by tests
new Person(new Name("David Li"), new Phone("91031282"), new Email("[email protected]"),
new Address("Blk 436 Serangoon Gardens Street 26, #16-43"),
getTagSet("family")),
getTagSet("family"), getGradeSet("midterms: 0")),

Check warning on line 34 in src/main/java/seedu/address/model/util/SampleDataUtil.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L34 was not covered by tests
new Person(new Name("Irfan Ibrahim"), new Phone("92492021"), new Email("[email protected]"),
new Address("Blk 47 Tampines Street 20, #17-35"),
getTagSet("classmates")),
getTagSet("classmates"), getGradeSet("finals: 100")),

Check warning on line 37 in src/main/java/seedu/address/model/util/SampleDataUtil.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L37 was not covered by tests
new Person(new Name("Roy Balakrishnan"), new Phone("92624417"), new Email("[email protected]"),
new Address("Blk 45 Aljunied Street 85, #11-31"),
getTagSet("colleagues"))
getTagSet("colleagues"), getGradeSet("ca2: 39"))

Check warning on line 40 in src/main/java/seedu/address/model/util/SampleDataUtil.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L40 was not covered by tests
};
}

Expand All @@ -57,4 +58,13 @@
.collect(Collectors.toSet());
}

/**
* Returns a grade set containing the list of strings given.
*/
public static Set<Grade> getGradeSet(String... strings) {
return Arrays.stream(strings)
.map(Grade::new)
.collect(Collectors.toSet());
}

}
Loading
Loading