Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
S-Quiz - Questions.java

Questions.java

Caricato da: Black Shadow
Scarica il programma completo

  1. import java.util.Random;
  2. import java.util.StringTokenizer;
  3. import java.io.*;
  4. /**
  5.  *The class that contain the questions
  6.  */
  7. class Questions {
  8.     Player player;
  9.     private int totQ;
  10.     int qNum[] = new int[15];
  11.     String questions[][];
  12.     String answer[][];
  13.     String exactly[][];
  14.     FileInputStream db, dbNum, dbTemp;
  15.     PrintStream dbTemp2;
  16.     /**
  17.      *The constructor of class Questions
  18.      */
  19.     Questions() {
  20.         int k = 1, o;
  21.         //Update dbNum.txt and take the total questions.
  22.         for (k = 1 ; k < 16 ; k++) {
  23.             try {
  24.                 dbTemp = new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\database" + Integer.toString(k) + ".txt");
  25.                     do {
  26.                         o = dbTemp.read();
  27.                         if (o != -1) {
  28.                             if ((char) o == ';') totQ++;
  29.                         }
  30.                     } while (o != -1);
  31.             } catch (IOException ioe) {}
  32.         }
  33.     }
  34.     /**
  35.      *The constructor of class Questions
  36.      */
  37.     Questions(Player p) {
  38.         int k = 1, o;
  39.         byte y;
  40.         int count[] = new int[15];
  41.         player = p;
  42.         //Update dbNum.txt
  43.         for (k = 1 ; k < 16 ; k++) {
  44.             try {
  45.                 dbTemp = new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\database" + Integer.toString(k) + ".txt");
  46.                     do {
  47.                         o = dbTemp.read();
  48.                         if (o != -1) {
  49.                             if ((char) o == ';') count[k - 1]++;
  50.                         }
  51.                     } while (o != -1);
  52.             } catch (IOException ioe) {}
  53.         }
  54.         try {
  55.             dbTemp2 = new PrintStream("C:\\Programmi\\S-Quiz\\build\\Databases\\dbNum.txt");
  56.             for (k = 0 ; k < count.length ; k++) {
  57.                 dbTemp2.printf("%d", count[k]);
  58.                 if (k != (count.length - 1))
  59.                     dbTemp2.printf("%c", ',');
  60.             }
  61.         } catch (IOException ioe) {}
  62.         //Update qNum based on number on dbNum.txt
  63.         try {
  64.             dbNum = new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\dbNum.txt");
  65.             int i = 0, a = 0;
  66.             String str = "";
  67.             StringTokenizer stNum;
  68.             do {
  69.                 i = dbNum.read();
  70.                 if (i != -1) str += (char) i;
  71.             } while (i != -1);
  72.             stNum = new StringTokenizer(str, ",", false);
  73.             while (stNum.hasMoreTokens() && stNum.countTokens() > 1) {
  74.                 qNum[a] = Integer.parseInt(stNum.nextToken());
  75.                 a++;
  76.             }
  77.             dbNum.close();
  78.         } catch (IOException ioe) {
  79.         } catch (NumberFormatException nfe) {}
  80.         questions = new String[15][qNum[player.getCurrentQ() - 1]];
  81.         answer = new String[15][qNum[player.getCurrentQ() - 1] * 4];
  82.         exactly = new String[15][qNum[player.getCurrentQ() - 1]];
  83.     }
  84.     /**
  85.      *This function make a casual number adapt to take a casual question.
  86.      *@return casual int number
  87.      */
  88.     final long getCasualNumber() {
  89.         Random a = new Random();
  90.         try {
  91.             return a.nextInt(qNum[player.getCurrentQ() - 1]);
  92.         } catch (ArrayIndexOutOfBoundsException aioobe) {
  93.             return -1;
  94.         } catch (IllegalArgumentException iae) {
  95.             try {
  96.                 return a.nextInt(qNum[player.getCurrentQ() - 1] + 1);
  97.             } catch (ArrayIndexOutOfBoundsException aioobe) {
  98.                 return -1;
  99.             }
  100.         }
  101.     }
  102.     /*
  103.     public static int getPreviousQ(int level) {
  104.         int pq = 0;     //Previous Questions
  105.         for (int a = 0 ; a < level ; a++)
  106.             pq += qNum[a];
  107.         return pq;
  108.     }*/
  109.     /**
  110.      *This function make the questions and the relatives answers.
  111.      *@params q - the "questions" object.
  112.      */
  113.     void MakeQandA(Questions q) {
  114.         int file = 1;
  115.         int i = 0;
  116.         int numST = 0;
  117.         int qu = 0, an = 0, ex = 0;           //questions, answer, exactly
  118.         String totStr = "", tempStr = "";
  119.         StringTokenizer st;
  120.         //Open the files
  121.         while (file < 16) {
  122.         qu = 0; an = 0; ex = 0;
  123.         totStr = ""; tempStr = "";
  124.         numST = 0; i = 0;
  125.         try {
  126.             db = new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\database" + Integer.toString(file) +".txt");
  127.         } catch (FileNotFoundException fnfe) {
  128.             System.out.println("Error: " + fnfe);
  129.         }
  130.         //Read the file and copy it into totStr String variable.
  131.         do {
  132.             try {
  133.                 i = db.read();
  134.                 if (i != -1) totStr += (char) i;
  135.             } catch (IOException ioe) {}
  136.         } while (i != -1);
  137.         //Use StringTokenizer to take the question and the relatives answers
  138.         st = new StringTokenizer(totStr, ";-_\n", false);
  139.         while (st.hasMoreTokens() && st.countTokens() > 1) {
  140.             switch (numST) {
  141.                 case 0:
  142.                     q.questions[file - 1][qu++] = st.nextToken();
  143.                     numST++;
  144.                     break;
  145.                 case 1:
  146.                 case 2:
  147.                 case 3:
  148.                 case 4:
  149.                     q.answer[file - 1][an++] = st.nextToken();
  150.                     numST++;
  151.                     break;
  152.                 case 5:
  153.                     q.exactly[file - 1][ex++] = st.nextToken();
  154.                     numST = 0;
  155.                     break;
  156.             }
  157.         }
  158.         try {
  159.             db.close();
  160.         } catch (IOException ioe) {}
  161.         file++;
  162.         }
  163.     }
  164.     public int getTotQ() {
  165.         return totQ;
  166.     }
  167. }