import java.util.Random;
import java.util.StringTokenizer;
import java.io.*;
/**
*The class that contain the questions
*/
class Questions {
Player player;
private int totQ;
int qNum[] = new int[15];
/**
*The constructor of class Questions
*/
Questions() {
int k = 1, o;
//Update dbNum.txt and take the total questions.
for (k = 1 ; k < 16 ; k++) {
try {
dbTemp
= new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\database" + Integer.
toString(k
) + ".txt");
do {
o = dbTemp.read();
if (o != -1) {
if ((char) o == ';') totQ++;
}
} while (o != -1);
}
}
/**
*The constructor of class Questions
*/
Questions(Player p) {
int k = 1, o;
byte y;
int count[] = new int[15];
player = p;
//Update dbNum.txt
for (k = 1 ; k < 16 ; k++) {
try {
dbTemp
= new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\database" + Integer.
toString(k
) + ".txt");
do {
o = dbTemp.read();
if (o != -1) {
if ((char) o == ';') count[k - 1]++;
}
} while (o != -1);
}
try {
dbTemp2
= new PrintStream("C:\\Programmi\\S-Quiz\\build\\Databases\\dbNum.txt");
for (k = 0 ; k < count.length ; k++) {
dbTemp2.printf("%d", count[k]);
if (k != (count.length - 1))
dbTemp2.printf("%c", ',');
}
//Update qNum based on number on dbNum.txt
try {
dbNum
= new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\dbNum.txt");
int i = 0, a = 0;
do {
i = dbNum.read();
if (i != -1) str += (char) i;
} while (i != -1);
while (stNum.hasMoreTokens() && stNum.countTokens() > 1) {
qNum
[a
] = Integer.
parseInt(stNum.
nextToken());
a++;
}
dbNum.close();
questions
= new String[15
][qNum
[player.
getCurrentQ() - 1
]];
answer
= new String[15
][qNum
[player.
getCurrentQ() - 1
] * 4
];
exactly
= new String[15
][qNum
[player.
getCurrentQ() - 1
]];
}
/**
*This function make a casual number adapt to take a casual question.
*@return casual int number
*/
final long getCasualNumber() {
try {
return a.nextInt(qNum[player.getCurrentQ() - 1]);
return -1;
try {
return a.nextInt(qNum[player.getCurrentQ() - 1] + 1);
return -1;
}
}
}
/*
public static int getPreviousQ(int level) {
int pq = 0; //Previous Questions
for (int a = 0 ; a < level ; a++)
pq += qNum[a];
return pq;
}*/
/**
*This function make the questions and the relatives answers.
*@params q - the "questions" object.
*/
void MakeQandA(Questions q) {
int file = 1;
int i = 0;
int numST = 0;
int qu = 0, an = 0, ex = 0; //questions, answer, exactly
String totStr
= "", tempStr
= "";
//Open the files
while (file < 16) {
qu = 0; an = 0; ex = 0;
totStr = ""; tempStr = "";
numST = 0; i = 0;
try {
db
= new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\database" + Integer.
toString(file
) +".txt");
System.
out.
println("Error: " + fnfe
);
}
//Read the file and copy it into totStr String variable.
do {
try {
i = db.read();
if (i != -1) totStr += (char) i;
} while (i != -1);
//Use StringTokenizer to take the question and the relatives answers
while (st.hasMoreTokens() && st.countTokens() > 1) {
switch (numST) {
case 0:
q.questions[file - 1][qu++] = st.nextToken();
numST++;
break;
case 1:
case 2:
case 3:
case 4:
q.answer[file - 1][an++] = st.nextToken();
numST++;
break;
case 5:
q.exactly[file - 1][ex++] = st.nextToken();
numST = 0;
break;
}
}
try {
db.close();
file++;
}
}
public int getTotQ() {
return totQ;
}
}