Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleDel 6 – Grensesnitt (10%)

Det er ønskelig å kunne støtte ulike filformat for quiz-er. Forklar med tekst og kode hvordan du vil bruke Java-grensesnitt for å gjøre det enkelt å bytte mellom ulike format og hvordan filinnlesingskoden du allerede har skrevet kan utgjøre (implementasjonen av) standardformatet. Merk at du ikke skal implementere nye format, kun vise hvordan det lett kan gjøres.

Expand
titleLF
Code Block
languagejava
themeEclipse
public interface QuizFormat {
	public Collection<Question> read(Reader input) throws IOException;
}

public class StandardQuizFormat implements QuizFormat {
 
	public Collection<Question> read(Reader input) throws IOException {
		Collection<Question> questions = new ArrayList<Question>();
		BufferedReader reader = new BufferedReader(input);
		while (reader.ready()) {
			String question = reader.readLine();
			... parsing code here ...
			questions.add(...);
		}
		return questions;
	}
 

I Quiz:

Code Block
languagejava
themeEclipse
private QuizFormat quizFormat = new StandardQuizFormat();
	
public void setQuizFormat(QuizFormat quizFormat) {
	this.quizFormat = quizFormat;
}
public void init(Reader input) throws IOException {
	questions.addAll(quizFormat.read(input));
}

 

 

Lenke til løsningskode: Quiz.java, QuizFormat.java, StandardQuizFormat.java

 

...