Commit cbff7cca authored by doc. Ing. Jaroslav Porubän PhD.'s avatar doc. Ing. Jaroslav Porubän PhD.
Browse files

Verzia2

parent 0a9c19d8
package sk.tuke.dsl.testlang.builder;
import sk.tuke.dsl.testlang.model.Answer;
import sk.tuke.dsl.testlang.model.Question;
import sk.tuke.dsl.testlang.model.Test;
import java.util.ArrayList;
import java.util.List;
public class FunctionSequenceBuilder {
private static String title;
private static String questionText;
private static List<Answer> answers;
private static List<Question> questions = new ArrayList<>();
public static void test(String title) {
FunctionSequenceBuilder.title = title;
}
public static void question(String text) {
if (answers != null) {
createQuestion();
}
answers = new ArrayList<>();
questionText = text;
}
public static void answer(String text) {
answers.add(new Answer(text, false));
}
public static void correct_answer(String text) {
answers.add(new Answer(text, true));
}
private static void createQuestion() {
questions.add(new Question(questionText, answers.toArray(new Answer[]{})));
}
public static Test getTest() {
createQuestion();
return new Test(title, questions.toArray(new Question[] {}));
}
}
package sk.tuke.dsl.testlang.builder;
import sk.tuke.dsl.testlang.model.Answer;
import sk.tuke.dsl.testlang.model.Question;
import sk.tuke.dsl.testlang.model.Test;
import java.util.ArrayList;
import java.util.List;
public class MethodChainingBuilder implements QuestionInterface {
private static MethodChainingBuilder instance;
private String title;
private String questionText;
private List<Answer> answers;
private List<Question> questions = new ArrayList<>();
private MethodChainingBuilder(String title) {
this.title = title;
instance = this;
}
public static QuestionInterface test(String title) {
return new MethodChainingBuilder(title);
}
public MethodChainingBuilder question(String text) {
if (answers != null) {
createQuestion();
}
answers = new ArrayList<>();
questionText = text;
return this;
}
public MethodChainingBuilder answer(String text) {
answers.add(new Answer(text, false));
return this;
}
public MethodChainingBuilder correct_answer(String text) {
answers.add(new Answer(text, true));
return this;
}
private void createQuestion() {
questions.add(new Question(questionText, answers.toArray(new Answer[]{})));
}
public static Test getTest() {
instance.createQuestion();
return new Test(instance.title, instance.questions.toArray(new Question[] {}));
}
}
package sk.tuke.dsl.testlang.builder;
import sk.tuke.dsl.testlang.model.Answer;
import sk.tuke.dsl.testlang.model.Question;
import sk.tuke.dsl.testlang.model.Test;
public class NestedFunctionBuilder {
private static Test test;
public static void test(String title, Question... questions) {
test = new Test(title, questions);
}
public static Question question(String text, Answer... answers) {
return new Question(text, answers);
}
public static Answer answer(String text) {
return new Answer(text, false);
}
public static Answer correct_answer(String text) {
return new Answer(text, true);
}
public static Test getTest() {
return test;
}
}
package sk.tuke.dsl.testlang.builder;
public interface QuestionInterface {
MethodChainingBuilder question(String text);
}
package sk.tuke.dsl.testlang.main;
import sk.tuke.dsl.testlang.model.Test;
import sk.tuke.dsl.testlang.semantics.PrintInterpreter;
import static sk.tuke.dsl.testlang.builder.NestedFunctionBuilder.*;
public class Main02NestedFunction {
public static void main(String[] args) {
test("Test z fyziky",
question("Aky symbol sa pouziva na rychlost",
answer("m"),
answer("a"),
correct_answer("v"),
answer("F")
),
question("Aky symbol sa pouziva na hmotnost",
correct_answer("m"),
answer("a"),
answer("v"),
answer("F")
)
);
Test test = getTest();
test.validate();
PrintInterpreter printer = new PrintInterpreter();
printer.print(test);
}
}
package sk.tuke.dsl.testlang.main;
import sk.tuke.dsl.testlang.builder.FunctionSequenceBuilder;
import sk.tuke.dsl.testlang.model.Test;
import sk.tuke.dsl.testlang.semantics.PrintInterpreter;
import static sk.tuke.dsl.testlang.builder.FunctionSequenceBuilder.*;
public class Main03FunctionSequence {
public static void main(String[] args) {
test("Test z fyziky");
question("Aky symbol sa pouziva na rychlost");
answer("m");
answer("a");
correct_answer("v");
answer("F");
question("Aky symbol sa pouziva na hmotnost");
correct_answer("m");
answer("a");
answer("v");
answer("F");
Test test = FunctionSequenceBuilder.getTest();
test.validate();
PrintInterpreter printer = new PrintInterpreter();
printer.print(test);
}
}
package sk.tuke.dsl.testlang.main;
import sk.tuke.dsl.testlang.builder.MethodChainingBuilder;
import sk.tuke.dsl.testlang.model.Test;
import sk.tuke.dsl.testlang.semantics.PrintInterpreter;
import static sk.tuke.dsl.testlang.builder.MethodChainingBuilder.test;
public class Main04MethodChaining {
public static void main(String[] args) {
test("Test z fyziky").
question("Aky symbol sa pouziva na rychlost").
answer("m").
answer("a").
correct_answer("v").
answer("F").
question("Aky symbol sa pouziva na hmotnost").
correct_answer("m").
answer("a").
answer("v").
answer("F")
;
Test test = MethodChainingBuilder.getTest();
test.validate();
PrintInterpreter printer = new PrintInterpreter();
printer.print(test);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment