-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the Survey, Question, and Response Entities and create a simple…
… test to verify them.
- Loading branch information
Showing
13 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/com/opinionowl/opinionowl/models/LongAnswerQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.opinionowl.opinionowl.models; | ||
|
||
import jakarta.persistence.Entity; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* The class used to describe the long answer questions. | ||
*/ | ||
@Entity | ||
@Getter | ||
@Setter | ||
public class LongAnswerQuestion extends Question{ | ||
// The character limit for the question. | ||
private int charLimit; | ||
|
||
/** | ||
* The default constructor for the class. | ||
*/ | ||
public LongAnswerQuestion(){ | ||
this("", 0); | ||
} | ||
|
||
/** | ||
* The constructor for the class. | ||
* @param prompt the prompt for the question | ||
* @param charLimit the character limit for the response. | ||
*/ | ||
public LongAnswerQuestion(String prompt, int charLimit){ | ||
super(prompt, QuestionType.LONG_ANSWER); | ||
this.charLimit = charLimit; | ||
} | ||
|
||
/** | ||
* @return the question in string form. | ||
*/ | ||
@Override | ||
public String toString(){ | ||
return super.toString() + " charLimit:" + charLimit; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/opinionowl/opinionowl/models/MultiChoiceQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.opinionowl.opinionowl.models; | ||
|
||
import jakarta.persistence.Entity; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* The class used to describe the multiple choice questions. | ||
*/ | ||
@Entity | ||
@Getter | ||
@Setter | ||
public class MultiChoiceQuestion extends Question { | ||
// The choices for the question. | ||
private String[] choices; | ||
|
||
/** | ||
* The default constructor for the class. | ||
*/ | ||
public MultiChoiceQuestion() { | ||
this("", new String[0]); | ||
} | ||
|
||
/** | ||
* The constructor for the class. | ||
* @param prompt the prompt for the question. | ||
* @param choices the choices for the question. | ||
*/ | ||
public MultiChoiceQuestion(String prompt, String[] choices){ | ||
super(prompt, QuestionType.MULTIPLE_CHOICE); | ||
this.choices = choices; | ||
} | ||
|
||
/** | ||
* @return the question in string form. | ||
*/ | ||
@Override | ||
public String toString(){ | ||
String res = super.toString() + " choices:["; | ||
if (choices.length > 0) { | ||
for (int i = 0; i < choices.length - 1; i++){ | ||
res += choices[i] + ", "; | ||
} | ||
res += choices[choices.length - 1]; | ||
} | ||
res += "]"; | ||
return res; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/opinionowl/opinionowl/models/Question.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.opinionowl.opinionowl.models; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* The Question class which keeps track of the survey questions. | ||
*/ | ||
@Entity | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) | ||
public class Question { | ||
// The id of the question. | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
// The prompt for the question. | ||
private String prompt; | ||
|
||
// The type of the question. | ||
private QuestionType type; | ||
|
||
/** | ||
* The constructor for the question class. | ||
* @param prompt the prompt for the question. | ||
* @param type the type of the question | ||
*/ | ||
public Question(String prompt, QuestionType type){ | ||
this.prompt = prompt; | ||
this.type = type; | ||
} | ||
|
||
/** | ||
* @return the question in string form. | ||
*/ | ||
@Override | ||
public String toString(){ | ||
return "Question id:" + id + " prompt:" + prompt; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/opinionowl/opinionowl/models/QuestionType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.opinionowl.opinionowl.models; | ||
|
||
/** | ||
* An Enum to keep track of the different question types. | ||
*/ | ||
public enum QuestionType { | ||
LONG_ANSWER, | ||
MULTIPLE_CHOICE, | ||
RANGE | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/opinionowl/opinionowl/models/RangeQuestion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.opinionowl.opinionowl.models; | ||
|
||
import jakarta.persistence.Entity; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* The class used to describe the range questions. | ||
*/ | ||
@Entity | ||
@Getter | ||
@Setter | ||
public class RangeQuestion extends Question { | ||
// The lower value for the range. | ||
float lower; | ||
|
||
// the upper value for the range. | ||
float upper; | ||
|
||
// the increment the range. | ||
float increment; | ||
|
||
/** | ||
* The default constructor for the class. | ||
*/ | ||
public RangeQuestion(){ | ||
this("", 0, 0); | ||
} | ||
|
||
/** | ||
* The constructor for the class without increment. | ||
* @param prompt the prompt for the question. | ||
* @param lower the lower value for the range. | ||
* @param upper the upper value for the range. | ||
*/ | ||
public RangeQuestion(String prompt, float lower, float upper){ | ||
this(prompt, lower, upper, 1); | ||
} | ||
|
||
/** | ||
* The constructor for the class with increment. | ||
* @param prompt the prompt for the question. | ||
* @param lower the lower value for the range. | ||
* @param upper the upper value for the range. | ||
* @param increment the increment value for the range. | ||
*/ | ||
public RangeQuestion(String prompt, float lower, float upper, float increment){ | ||
super(prompt, QuestionType.RANGE); | ||
this.lower = lower; | ||
this.upper = upper; | ||
this.increment = increment; | ||
} | ||
|
||
/** | ||
* @return the question in string form. | ||
*/ | ||
@Override | ||
public String toString(){ | ||
return super.toString() + " lower:" + lower + " upper:" + upper + " increment:" + increment; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/opinionowl/opinionowl/models/Response.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.opinionowl.opinionowl.models; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* The Response class in charge holding the survey response. | ||
*/ | ||
@Entity | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public class Response { | ||
// The id of the response. | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
// The hashmap for the answers of the response. | ||
@ElementCollection(fetch = FetchType.EAGER) | ||
@MapKeyColumn(name="question") | ||
@Column(name="answer") | ||
@CollectionTable(name="response_answers", joinColumns=@JoinColumn(name="response_id")) | ||
private Map<Long, String> answers; | ||
|
||
/** | ||
* The constructor for the Response class. | ||
* @param answers the answers in the Response. | ||
*/ | ||
public Response(HashMap<Long, String> answers){ | ||
this.answers = answers; | ||
} | ||
|
||
/** | ||
* @return the Response class in string form. | ||
*/ | ||
@Override | ||
public String toString(){ | ||
return "Response #" + id + " answers:" + answers; | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/com/opinionowl/opinionowl/models/Survey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.opinionowl.opinionowl.models; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* The survey class which contains all the information needed to create survey. | ||
*/ | ||
@Entity | ||
@Getter | ||
@Setter | ||
public class Survey { | ||
// Keeps track of the Id of the survey. | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
// Keeps track of the questions of the survey. | ||
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) | ||
private List<Question> questions; | ||
|
||
// Keeps track of the survey responses. | ||
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) | ||
private List<Response> responses; | ||
|
||
// Keeps track of whether the survey is open or closed. | ||
private boolean closed; | ||
|
||
/** | ||
* The default constructor for the survey. | ||
*/ | ||
public Survey(){ | ||
this.questions = new ArrayList<>(); | ||
this.responses = new ArrayList<>(); | ||
this.closed = false; | ||
} | ||
|
||
/** | ||
* Adds a question to the survey. | ||
* | ||
* @param question the question to add to the survey. | ||
*/ | ||
public void addQuestion(Question question){ | ||
this.questions.add(question); | ||
} | ||
|
||
/** | ||
* Removes a question from the survey. | ||
* @param questionId the Id of the question. | ||
* @return true if successfully removed the question, false otherwise. | ||
*/ | ||
public boolean removeQuestion(Long questionId){ | ||
return this.questions.removeIf(q -> q.getId() == questionId); | ||
} | ||
|
||
/** | ||
* Adds a response to the survey. | ||
* @param response the response to add to the survey. | ||
*/ | ||
public void addResponse(Response response){ | ||
this.responses.add(response); | ||
} | ||
|
||
/** | ||
* Removes the response from the survey. | ||
* @param responseId the id of the response to remove. | ||
* @return true if successfully remove, false otherwise. | ||
*/ | ||
public boolean removeResponse(Long responseId){ | ||
return this.responses.removeIf(r -> r.getId() == responseId); | ||
} | ||
|
||
/** | ||
* Returns a list of response for a specific question. | ||
* @param questionId the id of the question. | ||
* @return a list of the responses for that question. | ||
*/ | ||
public List<String> getResponsesForQuestion(Long questionId){ | ||
List<String> answers = new ArrayList<>(); | ||
for (Response r: responses){ | ||
answers.add(r.getAnswers().get(questionId)); | ||
} | ||
return answers; | ||
} | ||
|
||
/** | ||
* @return the survey in string form. | ||
*/ | ||
@Override | ||
public String toString(){ | ||
String res = "Survey #" + id + " Closed? " + closed; | ||
res += "\n-----Questions-----"; | ||
for (Question q: questions){ | ||
res += "\n" + q.toString(); | ||
} | ||
res += "\n-----Response-----"; | ||
for (Response r: responses){ | ||
res += "\n" + r.toString(); | ||
} | ||
return res; | ||
} | ||
} |
Oops, something went wrong.