Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. package quizz.factory;
  2.  
  3. import java.io.*;
  4.  
  5. public class QuestionnaireFactory {
  6.  
  7.     public static final QuestionnaireFactory FACTORY = new QuestionnaireFactory ( );
  8.  
  9.     private QuestionnaireFactory () {}
  10.  
  11.     public Question createQuestion(String text, String answer, String points) throws IOException, Exception {
  12.         int nbPoints = Integer.parseInt(points);
  13.         try {
  14.             return new Question(text, AnswerFactory.getInstance().buildAnswer(answer, text), nbPoints);
  15.         }catch(IOException io){
  16.             throw new Exception("bad format");
  17.         }
  18.     }
  19.  
  20.     public Quizz createQuestionnaire (String fileName) throws IOException {
  21.         Quizz questionnaire = new Quizz();
  22.         File source = new File ( fileName );
  23.         BufferedReader in = null ;
  24.         try {
  25.             in = new BufferedReader(new FileReader( source ) );
  26.             String text ;
  27.             while (( text = in . readLine())!= null ) {
  28.                 String answer = in . readLine ( );
  29.                 String nbPoints = in . readLine ( );
  30.                 if(answer == null || nbPoints == null ) {
  31.                     throw new IOException("bad format");
  32.                 }
  33.                 questionnaire.addQuestion(this.createQuestion(text,answer,nbPoints));
  34.             }
  35.         } catch (FileNotFoundException e) {
  36.             throw new IOException(e );
  37.         }
  38.         finally {
  39.             in.close();
  40.         }
  41.         return questionnaire ;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement