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 Meeting class and the corresponding test #74

Merged
merged 7 commits into from
Mar 15, 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
66 changes: 66 additions & 0 deletions src/main/java/staffconnect/model/meeting/Description.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package staffconnect.model.meeting;

import static java.util.Objects.requireNonNull;
import static staffconnect.commons.util.AppUtil.checkArgument;

/**
* Represents a Meeting's description in the staff book.
* Guarantees: immutable; is valid as declared in {@link #isValidDescription(String)}
*/
public class Description {

public static final String MESSAGE_CONSTRAINTS =
"Description should only contain alphanumeric characters and spaces, and it should not be blank";

/*
* The first character of the name must not be a whitespace,
* otherwise " " (a blank string) becomes a valid input.
*/
public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*";

public final String description;

/**
* Constructs a {@code Description}.
*
* @param textDescription A valid description.
*/
public Description(String textDescription) {
requireNonNull(textDescription);
checkArgument(isValidDescription(textDescription), MESSAGE_CONSTRAINTS);
description = textDescription;
}

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

@Override
public int hashCode() {
return description.hashCode();
}

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

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

Description otherDecription = (Description) other;
return description.equals(otherDecription.description);
}

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

}
66 changes: 66 additions & 0 deletions src/main/java/staffconnect/model/meeting/MeetDateTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package staffconnect.model.meeting;

import static java.util.Objects.requireNonNull;
import static staffconnect.commons.util.AppUtil.checkArgument;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* Represents a Meeting's starting time in the staff book.
* Guarantees: immutable; is valid as declared in {@link #isValidMeetDateTime(String)}
*/
public class MeetDateTime {

public static final String MESSAGE_CONSTRAINTS = "DateTime should be of the correct format dd/mm/yyyy HH:mm";
public static final String VALIDATION_REGEX = "\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}";

private static final DateTimeFormatter PROCESS_FORMAT = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");

private final LocalDateTime value;


/**
* Constructs a {@code MeetDateTime}.
*
* @param date A valid date.
*/
public MeetDateTime(String date) {
requireNonNull(date);
checkArgument(isValidMeetDateTime(date), MESSAGE_CONSTRAINTS);
value = java.time.LocalDateTime.parse(date, PROCESS_FORMAT);
}

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

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

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

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

MeetDateTime otherDate = (MeetDateTime) other;
return value.equals(otherDate.value);
}

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

}
65 changes: 65 additions & 0 deletions src/main/java/staffconnect/model/meeting/Meeting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package staffconnect.model.meeting;


import static staffconnect.commons.util.CollectionUtil.requireAllNonNull;

import java.util.Objects;

/**
* Represents a Meeting event in the staff book.
* Guarantees: details are present and not null, field values are validated, immutable.
*/
public class Meeting {

private final Description description;
private final MeetDateTime startDate;
Pluiexo marked this conversation as resolved.
Show resolved Hide resolved

/**
* Constructs a {@code Meeting}.
*
* @param description A valid meeting description.
* @param startDate A valid time and date for the meeting.
*/

public Meeting(Description description, MeetDateTime startDate) {
requireAllNonNull(description, startDate);
this.description = description;
this.startDate = startDate;
}

public Description getDescription() {
return description;

Check warning on line 31 in src/main/java/staffconnect/model/meeting/Meeting.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/model/meeting/Meeting.java#L31

Added line #L31 was not covered by tests
}

public MeetDateTime getStartDate() {
return startDate;

Check warning on line 35 in src/main/java/staffconnect/model/meeting/Meeting.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/model/meeting/Meeting.java#L35

Added line #L35 was not covered by tests
}

@Override
public int hashCode() {
return Objects.hash(description, startDate);
}

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

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

Meeting otherMeeting = (Meeting) other;
return description.equals(otherMeeting.description) && startDate.equals(otherMeeting.startDate);
}

/**
* Format state as text for viewing.
*/
public String toString() {
return startDate + ":" + description;
}

}
74 changes: 74 additions & 0 deletions src/test/java/staffconnect/model/meeting/DescriptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package staffconnect.model.meeting;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static staffconnect.testutil.Assert.assertThrows;

import org.junit.jupiter.api.Test;


class DescriptionTest {


@Test
public void constructor_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new Description(null));
}

@Test
public void constructor_invalidDescription_throwsIllegalArgumentException() {
String invalidDescription = "";
assertThrows(IllegalArgumentException.class, () -> new Description(invalidDescription));
}

@Test
public void isValidDescription() {
// null Description
assertThrows(NullPointerException.class, () -> Description.isValidDescription(null));

// invalid Description
assertFalse(Description.isValidDescription("")); // empty string
assertFalse(Description.isValidDescription(" ")); // spaces only
assertFalse(Description.isValidDescription("^")); // only non-alphanumeric characters
assertFalse(Description.isValidDescription("peter meeting*")); // contains non-alphanumeric characters

// valid Description
assertTrue(Description.isValidDescription("meeting")); // alphabets only
assertTrue(Description.isValidDescription("12345")); // numbers only
assertTrue(Description.isValidDescription("meeting at for 2nd finals")); // alphanumeric characters
assertTrue(Description.isValidDescription("Crush the exam")); // with capital letters
assertTrue(Description.isValidDescription("Super hard midterm with finals and project combined 2nd"));
}

@Test
public void equals() {
Description description = new Description("Valid Description");
MeetDateTime testDate = new MeetDateTime("12/04/2023 12:00");

// same values -> returns true
assertEquals(description, new Description("Valid Description"));

// same object -> returns true
assertEquals(description, description);

// null -> returns false
assertNotEquals(null, description);

// different types -> returns false
assertNotEquals(5.0f, description);

//Different object type -> returns false
assertFalse(description.equals(testDate));

// different values -> returns false
assertNotEquals(description, new Description("Other valid description"));
}
@Test
public void asSymmetricHashcode() {
Description first = new Description("Valid Description");
Description second = new Description("Valid Description");
assertEquals(first.hashCode(), second.hashCode());
}
}
80 changes: 80 additions & 0 deletions src/test/java/staffconnect/model/meeting/MeetDateTimeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package staffconnect.model.meeting;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static staffconnect.testutil.Assert.assertThrows;

import org.junit.jupiter.api.Test;


class MeetDateTimeTest {

@Test
public void constructor_null_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> new MeetDateTime(null));
}

@Test
public void constructor_invalidModule_throwsIllegalArgumentException() {
String invalidDate = "";
assertThrows(IllegalArgumentException.class, () -> new MeetDateTime(invalidDate));
}

@Test
public void isValidMeetDateTime() {
// null Date
assertThrows(NullPointerException.class, () -> MeetDateTime.isValidMeetDateTime(null));

// invalid Meeting Date
assertFalse(MeetDateTime.isValidMeetDateTime("")); // empty string
assertFalse(MeetDateTime.isValidMeetDateTime(" ")); // spaces only
assertFalse(MeetDateTime.isValidMeetDateTime("ABCD")); // letters only
assertFalse(MeetDateTime.isValidMeetDateTime("1234")); // numbers only
assertFalse(MeetDateTime.isValidMeetDateTime("120420231200")); // missing separator no space
assertFalse(MeetDateTime.isValidMeetDateTime("12/0420231200")); // only 1 separator no space
assertFalse(MeetDateTime.isValidMeetDateTime("1204/2023 1200")); // only 1 separator
assertFalse(MeetDateTime.isValidMeetDateTime("12-04-2023 12:00")); // wrong separator
assertFalse(MeetDateTime.isValidMeetDateTime("12/04/23 12:00")); // wrong digits for year
assertFalse(MeetDateTime.isValidMeetDateTime("12/4/2023 12:00")); // wrong number digits for month
assertFalse(MeetDateTime.isValidMeetDateTime("1/04/2023 12:00")); // wrong number digits for day
tsulim marked this conversation as resolved.
Show resolved Hide resolved

// valid meeting Date
assertTrue(MeetDateTime.isValidMeetDateTime("12/04/2023 12:00")); // dd/MM/yyyy HH:mm
assertTrue(MeetDateTime.isValidMeetDateTime("15/02/2024 12:00")); // dd/MM/yyyy HH:mm
}

@Test
public void equals() {
MeetDateTime date = new MeetDateTime("20/01/2023 12:00");
Description testDescription = new Description("Valid Description");

// same values -> returns true
assertEquals(date, new MeetDateTime("20/01/2023 12:00"));

// same object -> returns true
assertEquals(date, date);

// null -> returns false
assertNotEquals(null, date);

// different types -> returns false
assertNotEquals(1234, date);

//Different object type -> returns false
assertFalse(date.equals(testDescription));

// different values -> returns false
assertNotEquals(date, new MeetDateTime("15/02/2024 12:00"));
}

@Test
public void asSymmetricHashcode() {
MeetDateTime first = new MeetDateTime("12/04/2023 12:00");
MeetDateTime second = new MeetDateTime("12/04/2023 12:00");
assertEquals(first.hashCode(), second.hashCode());
}


}
Loading
Loading