Skip to content

Commit

Permalink
Merge pull request #8 from bardia-p/milestone1_issue3_model_survey_an…
Browse files Browse the repository at this point in the history
…d_questions

Resolves #3 Added the Survey, Question, and Response
  • Loading branch information
bardia-p authored Nov 3, 2023
2 parents 907d08c + eddfb65 commit 8db6e6e
Show file tree
Hide file tree
Showing 15 changed files with 563 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<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
51 changes: 51 additions & 0 deletions src/main/java/com/opinionowl/opinionowl/models/Answer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.opinionowl.opinionowl.models;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* The class in charge of holding the indivdual answers for each response.
*/
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Answer {
// The id of the answer.
@Id
@GeneratedValue
private Long id;

// The response which the answer belongs to.
@ManyToOne
private Response response;

// The id of the question.
private Long question;

// The contents of the response.
private String content;

/**
* The constructor for answer.
* @param response the response for which the answe belongs to.
* @param question the question id.
* @param content the content of the reply.
*/
public Answer(Response response, Long question, String content){
this.response = response;
this.question = question;
this.content = content;
}

/**
* @return the answer in string form.
*/
@Override
public String toString(){
return "Answer#" + id + " value:" + content;
}

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

}
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(strategy = GenerationType.AUTO)
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,
RADIO_CHOICE,
RANGE
}
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 RadioChoiceQuestion extends Question {
// The choices for the question.
private String[] choices;

/**
* The default constructor for the class.
*/
public RadioChoiceQuestion() {
this("", new String[0]);
}

/**
* The constructor for the class.
* @param prompt the prompt for the question.
* @param choices the choices for the question.
*/
public RadioChoiceQuestion(String prompt, String[] choices){
super(prompt, QuestionType.RADIO_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;
}
}
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.
int lower;

// the upper value for the range.
int upper;

// the increment the range.
int 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, int lower, int 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, int lower, int upper, int 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;
}
}
56 changes: 56 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,56 @@
package com.opinionowl.opinionowl.models;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
* The Response class in charge holding the survey response.
*/
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Response {
// The id of the response.
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

// The survey object used for the response.
@ManyToOne
private Survey survey;

// The list of the questions for the response.
@OneToMany(mappedBy = "response", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Answer> answers;

/**
* The constructor for the Response class.
*/
public Response(Survey survey) {
this.survey = survey;
this.answers = new ArrayList<>();
}

/**
* Adds an answer to the survey.
* @param question the question id.
* @param content the content of the reply.
*/
public void addAnswer(Long question, String content){
this.answers.add(new Answer(this, question, content));
}

/**
* @return the Response class in string form.
*/
@Override
public String toString(){
return "Response #" + id + " answers:" + answers;
}
}
Loading

0 comments on commit 8db6e6e

Please sign in to comment.