Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package quizz.factory;
- import java.io.*;
- public class QuestionnaireFactory {
- public static final QuestionnaireFactory FACTORY = new QuestionnaireFactory ( );
- private QuestionnaireFactory () {}
- public Question createQuestion(String text, String answer, String points) throws IOException, Exception {
- int nbPoints = Integer.parseInt(points);
- try {
- return new Question(text, AnswerFactory.getInstance().buildAnswer(answer, text), nbPoints);
- }catch(IOException io){
- throw new Exception("bad format");
- }
- }
- public Quizz createQuestionnaire (String fileName) throws IOException {
- Quizz questionnaire = new Quizz();
- File source = new File ( fileName );
- BufferedReader in = null ;
- try {
- in = new BufferedReader(new FileReader( source ) );
- String text ;
- while (( text = in . readLine())!= null ) {
- String answer = in . readLine ( );
- String nbPoints = in . readLine ( );
- if(answer == null || nbPoints == null ) {
- throw new IOException("bad format");
- }
- questionnaire.addQuestion(this.createQuestion(text,answer,nbPoints));
- }
- } catch (FileNotFoundException e) {
- throw new IOException(e );
- }
- finally {
- in.close();
- }
- return questionnaire ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement