Uproszczenie kodu JAVA

0

Witam! Mógłby ktoś mnie nakierować, w jaki sposób mógłbym uprościć kod umieszczony poniżej?

import java.sql.*;
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.exit;

public class Connector {
    PreparedStatement stmt;
    Connection con;
    ResultSet rs;
    String correct,answer;
    int number_questions;
    Scanner sc = new Scanner(System.in);
    Random r = new Random();

    public void Connect() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/quiz?useTimezone=true&serverTimezone=UTC", "root", "");
        } catch (Exception e) {
            System.out.println("Eror with connection");
        }
    }

    public void Ask(String q) {
        try {
            number_questions = 3;
            stmt = con.prepareStatement(q);
            stmt.setInt(1,r.nextInt(number_questions)+1);
            rs = stmt.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString(2));
                System.out.println("a)" +rs.getString(3));
                System.out.println("b)" +rs.getString(4));
                System.out.println("c)" +rs.getString(5));
                System.out.println("d)" +rs.getString(6));
                correct = rs.getString(7);
            }
        }catch(Exception e){
            System.out.println("Error with loading from the database");
        }
    }
    public void Check()
    {
        try{
            answer = sc.nextLine();
            if(answer.equals(correct))
            {
                System.out.println("Correct Answer!You go further!");
                Wait(3000);
            }
            else
            {
                System.out.println("Incorrect answer!Unfortunately, you're out!");
                Wait(3000);
                exit(0);
            }
        }catch (Exception e){System.out.println("Eror wtih checking the answer ");}
    }
    public void Start() {
        System.out.println("Welcome in game for a million dollars!");
        Wait(3000);
        System.out.println("You will hear 10 questions in a moment, choose only one answer for each!");
        Wait(3000);
        Connect();
        Ask("select * from questions_1t where id_question = ?");
        Check();
        Ask("select * from questions_5t where id_question = ?");
        Check();
        Ask("select * from questions_10t where id_question = ?");
        Check();
        Ask("select * from questions_20t where id_question = ?");
        Check();
        Ask("select * from questions_40t where id_question = ?");
        Check();
        Ask("select * from questions_75t where id_question = ?");
        Check();
        Ask("select * from questions_100t where id_question = ?");
        Check();
        Ask("select * from questions_250t where id_question = ?");
        Check();
        Ask("select * from questions_500t where id_question = ?");
        Check();
        Ask("select * from questions_1000t where id_question = ?");
        Check();
    }
    public static void Wait (int mills){
        try {
            Thread.sleep(mills);
        }catch (Exception e){System.out.println("Error with method Wait!");}
    }
}

Z góry dziękuję za odpowiedź.

1
        int questions[] = {1,5,10,20,40,75,100,250,500,1000};
        for (int q : questions) {
            Ask("select * from questions_" + q + "t where id_question = ?");
            Check();
        }

Może pętla ?

1

Pętla to raz, dwa zrób tu po ludzku OSOBNE klasy. Zrób klasę odpowiedzialną za czytanie z bazy, która ZWRACA obiekt typu Question który ma w sobie pytanie i odpowiedź. I potem logika gry woła sobie getQuestion i na nim operuje.

edit:

import java.sql.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

class Question {
    private final String question;
    private final String answer;

    Question(String question, String answer) {
        this.question = question;
        this.answer = answer;
    }

    public boolean validateAnswer(String resp) {
        return answer.equals(resp);
    }

    public void ask() {
        System.out.println(question);
    }
}

class QuestionRepository {
    private PreparedStatement stmt;
    private Connection con;
    private final Logger logger = Logger.getLogger(QuestionRepository.class.getName());

    public QuestionRepository() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/quiz?useTimezone=true&serverTimezone=UTC", "root", "");
            stmt = con.prepareStatement("select * from questions where points= ? ORDER BY RAND() limit 1");
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Error with connection", e);
        }
    }

    public Question getRandomQuestion(int score) {
        try {
            stmt.setInt(1, score);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return new Question(rs.getString(2), rs.getString(3));
            } else {
                throw new IllegalStateException("No question found");
            }
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Error with loading from the database", e);
            throw new IllegalStateException(e);
        }
    }
}

public class Millionaire {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        QuestionRepository questionRepository = new QuestionRepository();
        System.out.println("Welcome in game for a million dollars!");
        System.out.println("You will hear 10 questions in a moment, choose only one answer for each!");
        int[] scores = {1, 5, 10, 20, 40, 75, 100, 250, 500, 1000};
        for (int score : scores) {
            Question question = questionRepository.getRandomQuestion(score);
            question.ask();
            question.validateAnswer(sc.nextLine());
        }
    }
}

Prędzej tak bym to widział. Z jedną tabelką na pytania i polem na punkty plus z całym pytaniem w jednej kolumnie.
Da sie lepiej to zrobić, ale nie chce mi się więcej czasu na to tracić :P

1 użytkowników online, w tym zalogowanych: 0, gości: 1