Skip to content

Commit 9ac86dc

Browse files
committed
studio ABSTRACTENTITY
1 parent 56e8db1 commit 9ac86dc

3 files changed

Lines changed: 75 additions & 63 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.launchcode.codingevents.models;
2+
3+
import jakarta.persistence.GeneratedValue;
4+
import jakarta.persistence.GenerationType;
5+
import jakarta.persistence.Id;
6+
import jakarta.persistence.MappedSuperclass;
7+
8+
import java.util.Objects;
9+
10+
@MappedSuperclass
11+
public abstract class AbstractEntity {
12+
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.IDENTITY)
15+
private int id;
16+
17+
public int getId() {
18+
return id;
19+
}
20+
21+
@Override
22+
public boolean equals(Object o) {
23+
if (this == o) return true;
24+
if (!(o instanceof AbstractEntity that)) return false;
25+
return getId() == that.getId();
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
return Objects.hash(getId());
31+
}
32+
}

src/main/java/org/launchcode/codingevents/models/Event.java

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
package org.launchcode.codingevents.models;
22

33
import jakarta.persistence.Entity;
4-
import jakarta.persistence.GeneratedValue;
5-
import jakarta.persistence.Id;
4+
import jakarta.persistence.EnumType;
5+
import jakarta.persistence.Enumerated;
66
import jakarta.validation.constraints.Email;
77
import jakarta.validation.constraints.NotBlank;
88
import jakarta.validation.constraints.Size;
99

10-
import java.util.Objects;
11-
1210
/**
13-
* Created by Chris Bay
11+
* Represents an event in the system. Inherits common entity properties like ID from AbstractEntity.
1412
*/
1513
@Entity
16-
public class Event {
17-
18-
@Id
19-
@GeneratedValue
20-
private int id;
14+
public class Event extends AbstractEntity {
2115

2216
@NotBlank(message = "Name is required")
2317
@Size(min = 3, max = 50, message = "Name must be between 3 and 50 characters")
@@ -30,16 +24,30 @@ public class Event {
3024
@Email(message = "Invalid email. Try again.")
3125
private String contactEmail;
3226

27+
@Enumerated(EnumType.STRING) // Assuming EventType is an enum. Adjust as necessary.
3328
private EventType type;
3429

30+
/**
31+
* Default constructor required by JPA. Do not use to create instances.
32+
*/
33+
public Event() {}
34+
35+
/**
36+
* Creates a new Event with specified details.
37+
*
38+
* @param name the name of the event, must not be null or empty
39+
* @param description the description of the event, can be empty but not null
40+
* @param contactEmail the contact email for the event, must be a valid email format
41+
* @param type the type of the event, represented by the EventType enum
42+
*/
3543
public Event(String name, String description, String contactEmail, EventType type) {
3644
this.name = name;
3745
this.description = description;
3846
this.contactEmail = contactEmail;
3947
this.type = type;
4048
}
4149

42-
public Event() {}
50+
// Getters and setters
4351

4452
public String getName() {
4553
return name;
@@ -73,25 +81,13 @@ public void setType(EventType type) {
7381
this.type = type;
7482
}
7583

76-
public int getId() {
77-
return id;
78-
}
79-
8084
@Override
8185
public String toString() {
82-
return name;
83-
}
84-
85-
@Override
86-
public boolean equals(Object o) {
87-
if (this == o) return true;
88-
if (o == null || getClass() != o.getClass()) return false;
89-
Event event = (Event) o;
90-
return id == event.id;
91-
}
92-
93-
@Override
94-
public int hashCode() {
95-
return Objects.hash(id);
86+
return "Event{" +
87+
"name='" + name + '\'' +
88+
", description='" + description + '\'' +
89+
", contactEmail='" + contactEmail + '\'' +
90+
", type=" + type +
91+
'}';
9692
}
97-
}
93+
}
Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
package org.launchcode.codingevents.models;
22

33
import jakarta.persistence.Entity;
4-
import jakarta.persistence.GeneratedValue;
5-
import jakarta.persistence.Id;
64
import jakarta.validation.constraints.Size;
75

8-
import java.util.Objects;
9-
106
/**
11-
* Created by Chris Bay
7+
* Represents a category for events in the system. Inherits ID and common entity behavior from AbstractEntity.
128
*/
139
@Entity
14-
public class EventCategory {
15-
16-
@Id
17-
@GeneratedValue
18-
private int id;
10+
public class EventCategory extends AbstractEntity {
1911

20-
@Size(min=3, message="Name must be at least 3 characters long")
12+
@Size(min = 3, message = "Name must be at least 3 characters long")
2113
private String name;
2214

23-
public EventCategory(@Size(min = 3, message = "Name must be at least 3 characters long") String name) {
15+
/**
16+
* Default constructor required by JPA. Do not use to create instances.
17+
*/
18+
public EventCategory() {}
19+
20+
/**
21+
* Creates a new EventCategory with the specified name.
22+
*
23+
* @param name the name of the category, must be at least 3 characters long
24+
*/
25+
public EventCategory(String name) {
2426
this.name = name;
2527
}
2628

27-
public EventCategory() {}
28-
2929
public String getName() {
3030
return name;
3131
}
@@ -34,26 +34,10 @@ public void setName(String name) {
3434
this.name = name;
3535
}
3636

37-
public int getId() {
38-
return id;
39-
}
40-
4137
@Override
4238
public String toString() {
43-
return name;
44-
}
45-
46-
@Override
47-
public boolean equals(Object o) {
48-
if (this == o) return true;
49-
if (o == null || getClass() != o.getClass()) return false;
50-
EventCategory that = (EventCategory) o;
51-
return id == that.id;
52-
}
53-
54-
@Override
55-
public int hashCode() {
56-
return Objects.hash(id);
39+
return "EventCategory{" +
40+
"name='" + name + '\'' +
41+
'}';
5742
}
5843
}
59-

0 commit comments

Comments
 (0)