Skip to content

Commit e51c6ec

Browse files
committed
Add enum for weekday and validate that closing time is after opening time
1 parent 1b0618e commit e51c6ec

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/main/java/org/example/entities/OpeningHours.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ public class OpeningHours {
1111
@GeneratedValue(strategy = GenerationType.IDENTITY)
1212
private Long id;
1313

14-
private String weekday;
14+
@Enumerated(EnumType.ORDINAL)
15+
private Weekday weekday;
16+
1517
private LocalTime openingTime;
1618
private LocalTime closingTime;
1719

18-
public OpeningHours(Long id, Restaurant restaurant, String weekday, LocalTime openingTime, LocalTime closingTime) {
20+
public OpeningHours(Long id, Weekday weekday, LocalTime openingTime, LocalTime closingTime) {
1921
this.id = id;
2022
this.weekday = weekday;
2123
this.openingTime = openingTime;
2224
this.closingTime = closingTime;
25+
if(openingTime.isAfter(closingTime))
26+
throw new RuntimeException("Opening time cannot be after closing time");
2327
}
2428

2529
public OpeningHours(){}
@@ -28,11 +32,11 @@ public Long getId() {
2832
return id;
2933
}
3034

31-
public String getWeekday() {
35+
public Weekday getWeekday() {
3236
return weekday;
3337
}
3438

35-
public void setWeekday(String weekday) {
39+
public void setWeekday(Weekday weekday) {
3640
this.weekday = weekday;
3741
}
3842

@@ -61,4 +65,14 @@ public String toString() {
6165
", closingTime=" + closingTime +
6266
'}';
6367
}
68+
69+
enum Weekday{
70+
MONDAY,
71+
TUESDAY,
72+
WEDNESDAY,
73+
THURSDAY,
74+
FRIDAY,
75+
SATURDAY,
76+
SUNDAY
77+
}
6478
}

0 commit comments

Comments
 (0)