Skip to content

Commit

Permalink
Remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
potaotototo committed Oct 31, 2024
1 parent cf129f4 commit 4922dd1
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 47 deletions.
55 changes: 42 additions & 13 deletions src/main/java/tuteez/model/person/lesson/Day.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,62 @@
package tuteez.model.person.lesson;

import java.util.HashMap;
import java.util.Map;

/**
* Enum to represent the days of the week.
*/
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
MONDAY("mon"),
TUESDAY("tue"),
WEDNESDAY("wed"),
THURSDAY("thu"),
FRIDAY("fri"),
SATURDAY("sat"),
SUNDAY("sun");

private final String shortName;
private static final Map<String, Day> DAY_NAME_MAP = new HashMap<>();

static {
for (Day day : values()) {
DAY_NAME_MAP.put(day.name().toLowerCase(), day);
DAY_NAME_MAP.put(day.shortName, day);
}
}

/**
* Constructs a Day enum value with its corresponding short name.
*
* @param shortName The abbreviated name of the day (e.g., "mon" for Monday)
*/
private Day(String shortName) {
this.shortName = shortName;
}

/**
* Converts a string representation of a day into the corresponding {@code Day} enum value.
*
* <p>The string must be in lowercase (e.g., "monday", "tuesday"). If the string does not
* <p>The string must be in lowercase (e.g., "monday", "mon"). If the string does not
* match any of the valid days of the week, an exception is thrown.</p>
*
* @param dayInStr The string representing the day of the week (e.g., "monday", "mon").
* @return The {@code Day} enum corresponding to the input string.
* @throws IllegalArgumentException If the provided string does not match a valid day.
*/
public static Day convertDayToEnum(String dayInStr) {
return switch (dayInStr) {
case "monday", "mon" -> MONDAY;
case "tuesday", "tue" -> TUESDAY;
case "wednesday", "wed" -> WEDNESDAY;
case "thursday", "thu" -> THURSDAY;
case "friday", "fri" -> FRIDAY;
case "saturday", "sat" -> SATURDAY;
case "sunday", "sun" -> SUNDAY;
// Default case should not occur because input is validated before calling the method.
default -> throw new IllegalArgumentException("Invalid day string: " + dayInStr);
};
assert dayInStr!= null;
return DAY_NAME_MAP.get(dayInStr.toLowerCase());
}

/**
* Checks if the day provided is valid (i.e., part of the defined Days enum).
*
* @param day The day to check.
* @return true if the day is valid.
*/
public static boolean isValidDay(String day) {
return day != null && DAY_NAME_MAP.containsKey(day.toLowerCase());
}

@Override
Expand Down
35 changes: 1 addition & 34 deletions src/main/java/tuteez/model/person/lesson/Lesson.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


Expand All @@ -27,27 +25,6 @@ public class Lesson {
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HHmm");
private static final String VALID_TIME_RANGE_REGEX = "([01]?[0-9]|2[0-3])[0-5][0-9]-([01]?[0-9]|2[0-3])[0-5][0-9]";
private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HHmm");
/**
* Maps various string representations of days (e.g., "monday", "mon") to their corresponding Day enum values.
* All keys are stored in lowercase.
*/
private static final Map<String, Day> DAY_NAME_MAP = new HashMap<>();
static {
DAY_NAME_MAP.put("monday", Day.MONDAY);
DAY_NAME_MAP.put("mon", Day.MONDAY);
DAY_NAME_MAP.put("tuesday", Day.TUESDAY);
DAY_NAME_MAP.put("tue", Day.TUESDAY);
DAY_NAME_MAP.put("wednesday", Day.WEDNESDAY);
DAY_NAME_MAP.put("wed", Day.WEDNESDAY);
DAY_NAME_MAP.put("thursday", Day.THURSDAY);
DAY_NAME_MAP.put("thu", Day.THURSDAY);
DAY_NAME_MAP.put("friday", Day.FRIDAY);
DAY_NAME_MAP.put("fri", Day.FRIDAY);
DAY_NAME_MAP.put("saturday", Day.SATURDAY);
DAY_NAME_MAP.put("sat", Day.SATURDAY);
DAY_NAME_MAP.put("sunday", Day.SUNDAY);
DAY_NAME_MAP.put("sun", Day.SUNDAY);
}
private final Day lessonDay;
private final LocalTime startTime;
private final LocalTime endTime;
Expand All @@ -67,16 +44,6 @@ public Lesson(String lesson) {
this.endTime = LocalTime.parse(timeArr[1], timeFormatter);
}

/**
* Validates if the day provided is valid (i.e., part of the defined Days enum).
*
* @param day The day to check.
* @return true if the day is valid.
*/
private static boolean isValidDay(String day) {
return DAY_NAME_MAP.containsKey(day.toLowerCase());
}

/**
* Validates the time range format (should be HHMM-HHMM 24-hour format).
*
Expand Down Expand Up @@ -122,7 +89,7 @@ public static boolean isValidLesson(String lesson) {
}
String day = parts[0];
String timeRange = parts[1];
return isValidDay(day) && isValidTimeRange(timeRange) && isValidTimeOrder(timeRange);
return Day.isValidDay(day) && isValidTimeRange(timeRange) && isValidTimeOrder(timeRange);
}

/**
Expand Down
84 changes: 84 additions & 0 deletions src/test/java/tuteez/model/person/lesson/DayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package tuteez.model.person.lesson;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class DayTest {

@Test
public void isValidDay_validInputs_returnsTrue() {
assertTrue(Day.isValidDay("monday"));
assertTrue(Day.isValidDay("tuesday"));
assertTrue(Day.isValidDay("wednesday"));
assertTrue(Day.isValidDay("thursday"));
assertTrue(Day.isValidDay("friday"));
assertTrue(Day.isValidDay("saturday"));
assertTrue(Day.isValidDay("sunday"));

assertTrue(Day.isValidDay("mon"));
assertTrue(Day.isValidDay("tue"));
assertTrue(Day.isValidDay("wed"));
assertTrue(Day.isValidDay("thu"));
assertTrue(Day.isValidDay("fri"));
assertTrue(Day.isValidDay("sat"));
assertTrue(Day.isValidDay("sun"));

assertTrue(Day.isValidDay("Monday"));
assertTrue(Day.isValidDay("TUESDAY"));
assertTrue(Day.isValidDay("wEdNeSdAy"));
assertTrue(Day.isValidDay("Thu"));
assertTrue(Day.isValidDay("FRI"));
}

@Test
public void isValidDay_invalidInputs_returnsFalse() {
assertFalse(Day.isValidDay("not a day"));
assertFalse(Day.isValidDay("mon day"));
assertFalse(Day.isValidDay("mondays"));
assertFalse(Day.isValidDay("tues"));
assertFalse(Day.isValidDay("thur"));

assertFalse(Day.isValidDay(""));
assertFalse(Day.isValidDay(" "));
assertFalse(Day.isValidDay("\t"));

assertFalse(Day.isValidDay(null));
}

@Test
public void convertDayToEnum_validInputs_returnsCorrectEnum() {
assertEquals(Day.MONDAY, Day.convertDayToEnum("monday"));
assertEquals(Day.TUESDAY, Day.convertDayToEnum("tuesday"));
assertEquals(Day.WEDNESDAY, Day.convertDayToEnum("wednesday"));
assertEquals(Day.THURSDAY, Day.convertDayToEnum("thursday"));
assertEquals(Day.FRIDAY, Day.convertDayToEnum("friday"));
assertEquals(Day.SATURDAY, Day.convertDayToEnum("saturday"));
assertEquals(Day.SUNDAY, Day.convertDayToEnum("sunday"));

assertEquals(Day.MONDAY, Day.convertDayToEnum("mon"));
assertEquals(Day.TUESDAY, Day.convertDayToEnum("tue"));
assertEquals(Day.WEDNESDAY, Day.convertDayToEnum("wed"));
assertEquals(Day.THURSDAY, Day.convertDayToEnum("thu"));
assertEquals(Day.FRIDAY, Day.convertDayToEnum("fri"));
assertEquals(Day.SATURDAY, Day.convertDayToEnum("sat"));
assertEquals(Day.SUNDAY, Day.convertDayToEnum("sun"));

assertEquals(Day.MONDAY, Day.convertDayToEnum("Monday"));
assertEquals(Day.TUESDAY, Day.convertDayToEnum("TUESDAY"));
assertEquals(Day.WEDNESDAY, Day.convertDayToEnum("wEdNeSdAy"));
}

@Test
public void toString_returnsUpperCase() {
assertEquals("MONDAY", Day.MONDAY.toString());
assertEquals("TUESDAY", Day.TUESDAY.toString());
assertEquals("WEDNESDAY", Day.WEDNESDAY.toString());
assertEquals("THURSDAY", Day.THURSDAY.toString());
assertEquals("FRIDAY", Day.FRIDAY.toString());
assertEquals("SATURDAY", Day.SATURDAY.toString());
assertEquals("SUNDAY", Day.SUNDAY.toString());
}
}
4 changes: 4 additions & 0 deletions src/test/java/tuteez/model/person/lesson/LessonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ public void equals() {
Lesson l1 = new Lesson("Friday 1300-1400");
Lesson l2 = new Lesson("Friday 1300-1400");
Lesson l3 = new Lesson("friday 1300-1400");
Lesson l4 = new Lesson("Saturday 1300-1400");
Lesson l5 = new Lesson("Friday 1300-1430");

Assertions.assertNotEquals(l1, new Object());
assertEquals(l1, l1);
assertEquals(l1, l2);
assertEquals(l2, l3);
assertFalse(l1.equals(l4));
assertFalse(l1.equals(l5));
}

@Test
Expand Down

0 comments on commit 4922dd1

Please sign in to comment.