Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,40 @@
<name>Module3Quest Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.85</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0-M2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>Module3Quest</finalName>
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/quest/Question.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package quest;

import java.util.Objects;

public class Question {
private String question;
private final String answerOption1;
private final String answerOption2;
private final String answerOption3;
private final String answerOption4;
private final int rightAnswer;

public Question(String question, String answerOption1, String answerOption2, String answerOption3, String answerOption4, int rightAnswer) {
this.question = question;
this.answerOption1 = answerOption1;
this.answerOption2 = answerOption2;
this.answerOption3 = answerOption3;
this.answerOption4 = answerOption4;
this.rightAnswer = rightAnswer;
}

public String getQuestion() {
return question;
}

public String getAnswerOption1() {
return answerOption1;
}

public String getAnswerOption2() {
return answerOption2;
}

public String getAnswerOption3() {
return answerOption3;
}

public String getAnswerOption4() {
return answerOption4;
}

public int getRightAnswer() {
return rightAnswer;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Question question1 = (Question) o;
return rightAnswer == question1.rightAnswer && Objects.equals(question, question1.question) && Objects.equals(answerOption1, question1.answerOption1) && Objects.equals(answerOption2, question1.answerOption2) && Objects.equals(answerOption3, question1.answerOption3) && Objects.equals(answerOption4, question1.answerOption4);
}

@Override
public int hashCode() {
return Objects.hash(question, answerOption1, answerOption2, answerOption3, answerOption4, rightAnswer);
}
}
101 changes: 101 additions & 0 deletions src/main/java/quest/QuestionBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package quest;

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

public class QuestionBase {
private static QuestionBase instance;
private List<Question> questions;
private int currentQuestionIndex = 0;

private QuestionBase() {
questions = new ArrayList<>();
questions.add(new Question("img/1.png",
"BMW",
"Bentley",
"Lexus",
"Audi", 2));
questions.add(new Question("img/2.png",
"Brilliance",
"Skoda",
"Lexus",
"Dodge", 1));
questions.add(new Question("img/3.png",
"Skoda",
"Alfa Romeo",
"Acura",
"Dodge", 4));
questions.add(new Question("img/4.png",
"Mazda",
"Audi",
"Brilliance",
"Dodge", 2));
questions.add(new Question("img/5.png",
"Daihatsu",
"Bentley",
"BMW",
"Jaguar", 1));
questions.add(new Question("img/6.png",
"Alfa Romeo",
"Acura",
"Citroen",
"Lexus", 2));
questions.add(new Question("img/7.png",
"Alfa Romeo",
"Porsche",
"Mercedes",
"Volkswagen", 1));
questions.add(new Question("img/8.png",
"Volkswagen",
"Citroen",
"Jaguar",
"BMW", 4));
questions.add(new Question("img/9.png",
"Chevrolet",
"Porsche",
"Lexus",
"BMW", 1));
questions.add(new Question("img/10.png",
"Alfa Romeo",
"Porsche",
"Daewoo",
"Acura", 3));
questions.add(new Question("img/11.png",
"BMW",
"Mercedes",
"Ford",
"Aston Martin", 2));
questions.add(new Question("img/12.png",
"Ford",
"Citroen",
"Jaguar",
"Cadillac", 4));
questions.add(new Question("img/13.png",
"Ferrari",
"Acura",
"Porsche",
"Mercedes", 1));
questions.add(new Question("img/14.png",
"Ferrari",
"BMW",
"Chevrolet",
"Aston Martin", 4));
questions.add(new Question("img/15.png",
"Daewoo",
"Volkswagen",
"Citroen",
"BMW", 3));

currentQuestionIndex = 0;
}
public static synchronized QuestionBase getInstance(){
if (instance == null){
instance = new QuestionBase();
}
return instance;
}

public List<Question> getQuestions() {
return questions;
}
}
92 changes: 92 additions & 0 deletions src/main/java/quest/QuizControllerServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package quest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/quiz")
public class QuizControllerServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
String level = request.getParameter("level");
String startNewGame = request.getParameter("newGame");

if ("true".equals(startNewGame)) {
session.removeAttribute("currentQuestion");
session.removeAttribute("currentQuestionIndex");
session.removeAttribute("message");
}

List<Question> questions = QuestionBase.getInstance().getQuestions();

if (!questions.isEmpty()) {
session.setAttribute("currentQuestion", questions.get(0));
session.setAttribute("currentQuestionIndex", 0);
session.setAttribute("level", level);
session.setAttribute("totalQuestions", questions.size());
getServletContext().getRequestDispatcher("/quiz.jsp").forward(request, response);
} else {
response.getWriter().println("Помилка: Немає доступних питань.");
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
Question currentQuestion = (Question) session.getAttribute("currentQuestion");
String userAnswerStr = request.getParameter("userAnswer");

if (userAnswerStr != null && !userAnswerStr.isEmpty()) {
try {
int userAnswer = Integer.parseInt(userAnswerStr);

if (userAnswer == currentQuestion.getRightAnswer()) {
session.setAttribute("message", "Correct!");
} else {
session.setAttribute("message", "Incorrect!");
}
} catch (NumberFormatException e) {
session.setAttribute("message", "Error");
}
}

List<Question> questions = QuestionBase.getInstance().getQuestions();
int currentQuestionIndex = (Integer) session.getAttribute("currentQuestionIndex");

if ("true".equals(request.getParameter("restartButton"))) {
restartGame(session);
response.sendRedirect("quiz.jsp");
return;
}

session.setAttribute("currentQuestionIndex", currentQuestionIndex);
session.setAttribute("totalQuestions", questions.size());

if (currentQuestionIndex < questions.size() - 1) {
Question nextQuestion = questions.get(currentQuestionIndex + 1);
session.setAttribute("currentQuestion", nextQuestion);
session.setAttribute("currentQuestionIndex", currentQuestionIndex + 1);
response.sendRedirect("quiz.jsp");
} else {
response.sendRedirect("quizResult.jsp");
}
}

private void restartGame(HttpSession session) {
session.removeAttribute("currentQuestion");
session.removeAttribute("currentQuestionIndex");
session.removeAttribute("message");

List<Question> questions = QuestionBase.getInstance().getQuestions();
Question firstQuestion = questions.get(0);
session.setAttribute("currentQuestion", firstQuestion);
session.setAttribute("currentQuestionIndex", 0);
}
}
7 changes: 0 additions & 7 deletions src/main/webapp/WEB-INF/web.xml

This file was deleted.

Binary file added src/main/webapp/img/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/webapp/img/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 16 additions & 1 deletion src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>CAR LOGO QUIZ</title>
<link rel="stylesheet" type="text/css" href="static/style.css">
</head>

<body>
<h2>Hello World!</h2>

<div class="card">
<h2>Виберіть рівень гри:</h2>
<p>Ласкаво просимо до <b>CAR LOGO QUIZ!</b> Ця гра допоможе вам перевірити ваші знання логотипів. Оберіть рівень складності:</p>
<a class="btn-easy" href="quiz?level=easy&newGame=true">Легко</a>
<a class="btn-medium" href="quiz?level=medium&newGame=true">Середньо</a>
<a class="btn-hard" href="quiz?level=hard&newGame=true">Складно</a>
</div>

</body>
</html>

Loading