import java sql import java util ArrayList import java util Collection

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class QuestionManager {
private String DBDriver;
private String DBHost;
private String DBUser;
private String DBPassword;
private ArrayList<Question> questions;
private ArrayList<Question> final_questions;
public QuestionManager(String DB_Driver, String DB_Host, String DB_User, String DB_Password){
DBDriver = DB_Driver;
DBHost = DB_Host;
DBUser = DB_User;
DBPassword = DB_Password;
questions = new ArrayList<>();
final_questions = new ArrayList<>();
refresh_questions();
}
public void refresh_questions(){
Connection con = null;
try {
Class.forName(DBDriver);
} catch (ClassNotFoundException exc) {
System.out.println("Exception in QuestionManager constructor (driver)" + exc);
}
try {
con = DriverManager.getConnection(DBHost, DBUser, DBPassword);
Statement st = con.createStatement();
ResultSet quest = st.executeQuery("select * from questions");
while (quest.next()) {
Question result = new Question();
result.id = quest.getInt(1);
result.quest = quest.getString(2);
for (int j = 3; j <= quest.getMetaData().getColumnCount(); j++) {
if (result.answers.size() == j - 3) {
result.answers.add("");
}
result.answers.set(j - 3, quest.getString(j));
}
result.correct = 0;
questions.add(result);
} //взяли и распарсили обычные вопросы
st = con.createStatement();
quest = st.executeQuery("select * from final");
while (quest.next()) {
Question result = new Question();
result.id = quest.getInt(1);
result.quest = quest.getString(2);
for (int j = 3; j <= quest.getMetaData().getColumnCount(); j++) {
if (result.answers.size() == j - 3) {
result.answers.add("");
}
result.answers.set(j - 3, quest.getString(j));
}
result.correct = 0;
final_questions.add(result);
}
//взяли и распарсили финальные вопросы
} catch(Exception exc){
System.out.println("Exception in QuestionManager constructor (connection)" + exc);
}
try {
if (con != null)
con.close();
} catch (SQLException exc) {
System.out.println("Exception in QuestionManager close connection" + exc);
}
}
public ArrayList<Question> get_questions(int number){
ArrayList<Question> result = new ArrayList<>();
while(number > 0){
ArrayList<Integer> order = new ArrayList<>();
for (int j = 0; j < questions.size(); j++) { //order = [1,2,3..n]
order.add(j);
}
long seed = System.nanoTime();
Collections.shuffle(order, new Random(seed)); //order теперь случайная перестановка
for(int i = 0; i < Math.min(number, order.size()); i++){
result.add(shuffle_answers(questions.get(order.get(i)))); // заполнили result вопросами в порядке из order
}
order.clear();
number -= questions.size();
}
return result;
}
public ArrayList<Question> get_final_questions(int number){
ArrayList<Question> result = new ArrayList<>();
while(number > 0){
ArrayList<Integer> order = new ArrayList<>();
for (int j = 0; j < final_questions.size(); j++) {
order.add(j);
}
long seed = System.nanoTime();
Collections.shuffle(order, new Random(seed));
for(int i = 0; i < Math.min(number, order.size()); i++){
result.add(shuffle_answers(final_questions.get(order.get(i))));
}
order.clear();
number -= final_questions.size();
}
return result;
}
public Question shuffle_answers(Question quest){ //перемешивание ответов в вопросе
ArrayList<Integer> order = new ArrayList<Integer>();
for (int j = 0; j < quest.answers.size(); j++) {
order.add(j);
}
long seed = System.nanoTime();
Collections.shuffle(order, new Random(seed));
Collections.shuffle(quest.answers, new Random(seed));
quest.correct = order.indexOf(quest.correct);
order.clear();
return quest;
}
}