-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.go
38 lines (32 loc) · 784 Bytes
/
quiz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package quiz
import (
"fmt"
"github.com/edreg/awesome/quiz/problem"
"io"
)
type Quiz struct {
problems []problem.Problem
rightAnswers int
}
// Run runs the quiz for all the problems keeping track of correct answers
func (q *Quiz) Run(w io.Writer, r io.Reader) {
for _, singleProblem := range q.problems {
singleProblem.AskQuestion(w)
correct := singleProblem.CheckAnswer(r)
if correct {
q.rightAnswers++
}
}
q.PrintResults(w)
}
// PrintResults outputs the results of the quiz
func (q Quiz) PrintResults(w io.Writer) {
fmt.Fprintf(w, "You got %d questions right!\n", q.rightAnswers)
}
// New creates a new quiz from the supplied slice of problems
func New(problems []problem.Problem) Quiz {
return Quiz{
problems: problems,
rightAnswers: 0,
}
}