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

prednaska4

parent 8e606961
package sk.tuke.dsl.testlang.builder;
import sk.tuke.dsl.testlang.builder.types.AfterCorrectAnswer;
import sk.tuke.dsl.testlang.builder.types.AfterQuestion;
import sk.tuke.dsl.testlang.builder.types.AfterTest;
import sk.tuke.dsl.testlang.builder.types.AfterWrongAnswer;
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 AfterTest, AfterQuestion, AfterCorrectAnswer, AfterWrongAnswer {
private static String testTitle;
private static String questionText;
private static List<Question> questions = new ArrayList<>();
private static List<Answer> answers;
public static AfterTest test(String title) {
testTitle = title;
return new MethodChainingBuilder();
}
@Override
public AfterQuestion question(String text) {
if (questionText != null) {
createQuestion();
}
answers = new ArrayList<>();
questionText = text;
return this;
}
@Override
public AfterCorrectAnswer correct_answer(String text) {
answers.add(new Answer(text, true));
return this;
}
@Override
public AfterWrongAnswer wrong_answer(String text) {
answers.add(new Answer(text, false));
return this;
}
private static void createQuestion() {
questions.add(new Question(questionText, answers.toArray(new Answer[]{})));
}
public static Test getTest() {
createQuestion();
return new Test(testTitle, 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 Test getTest() {
return 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 correct_answer(String text) {
return new Answer(text, true);
}
public static Answer wrong_answer(String text) {
return new Answer(text, false);
}
}
package sk.tuke.dsl.testlang.builder.types;
import sk.tuke.dsl.testlang.builder.MethodChainingBuilder;
public interface AfterCorrectAnswer {
AfterCorrectAnswer correct_answer(String text);
AfterWrongAnswer wrong_answer(String text);
}
package sk.tuke.dsl.testlang.builder.types;
public interface AfterQuestion {
AfterCorrectAnswer correct_answer(String text);
}
package sk.tuke.dsl.testlang.builder.types;
public interface AfterTest {
AfterQuestion question(String text);
}
package sk.tuke.dsl.testlang.builder.types;
import sk.tuke.dsl.testlang.builder.MethodChainingBuilder;
import sk.tuke.dsl.testlang.builder.types.AfterQuestion;
public interface AfterWrongAnswer {
AfterQuestion question(String text);
AfterWrongAnswer wrong_answer(String text);
}
......@@ -3,6 +3,7 @@ package sk.tuke.dsl.testlang.main;
import sk.tuke.dsl.testlang.model.Answer;
import sk.tuke.dsl.testlang.model.Question;
import sk.tuke.dsl.testlang.model.Test;
import sk.tuke.dsl.testlang.semantics.TestPrinter;
public class Main01Model {
public static void main(String[] args) {
......@@ -31,7 +32,7 @@ public class Main01Model {
test.validate();
// TestPrinter testPrinter = new TestPrinter();
// testPrinter.print(test);
TestPrinter testPrinter = new TestPrinter();
testPrinter.print(test);
}
}
package sk.tuke.dsl.testlang.main;
import sk.tuke.dsl.testlang.builder.NestedFunctionBuilder;
import sk.tuke.dsl.testlang.model.Test;
import sk.tuke.dsl.testlang.semantics.TestPrinter;
import static sk.tuke.dsl.testlang.builder.NestedFunctionBuilder.*;
public class Main05NestedFunction {
public static void main(String[] args) {
/********************************************************************************/
test("Test z astronomie",
question("Kolko planet ma Slnecna sustava?",
wrong_answer("6"),
wrong_answer("7"),
correct_answer("8"),
wrong_answer("9")
),
question("Zem je v poradi?",
wrong_answer("1"),
wrong_answer("2"),
correct_answer("3"),
wrong_answer("4")
)
)
/********************************************************************************/
;
Test test = NestedFunctionBuilder.getTest();
test.validate();
TestPrinter testPrinter = new TestPrinter();
testPrinter.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.TestPrinter;
import static sk.tuke.dsl.testlang.builder.MethodChainingBuilder.*;
public class Main06MethodChaining {
public static void main(String[] args) {
/********************************************************************************/
test("Test z astronomie").
question("Kolko planet ma Slnecna sustava?").
correct_answer("8").
wrong_answer("6").
wrong_answer("7").
wrong_answer("9").
question("Zem je v poradi?").
correct_answer("3").
wrong_answer("1").
wrong_answer("2").
wrong_answer("4")
/********************************************************************************/
;
Test test = MethodChainingBuilder.getTest();
test.validate();
TestPrinter testPrinter = new TestPrinter();
testPrinter.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