Skip to content

Commit

Permalink
Added the Survey, Question, and Response Entities and create a simple…
Browse files Browse the repository at this point in the history
… test to verify them.
  • Loading branch information
bardia-p committed Nov 3, 2023
1 parent 18207fa commit 0f1786f
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* The starting point for the application.
*/
@SpringBootApplication
public class OpinionOwlApplication {

Expand Down
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;
}

}
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 src/main/java/com/opinionowl/opinionowl/models/Question.java
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 src/main/java/com/opinionowl/opinionowl/models/QuestionType.java
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 src/main/java/com/opinionowl/opinionowl/models/RangeQuestion.java
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 src/main/java/com/opinionowl/opinionowl/models/Response.java
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 src/main/java/com/opinionowl/opinionowl/models/Survey.java
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;
}
}
Loading

0 comments on commit 0f1786f

Please sign in to comment.