Skip to content

Commit

Permalink
Factory method and builder tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Secchol committed Sep 12, 2023
1 parent 8e83ed2 commit 9d01acf
Show file tree
Hide file tree
Showing 22 changed files with 244 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.ontotext.javacourse.designpatterns.abstractfactory;

import com.ontotext.javacourse.designpatterns.abstractfactory.factories.GeographyFactory;
import com.ontotext.javacourse.designpatterns.abstractfactory.factories.HistoryFactory;
import com.ontotext.javacourse.designpatterns.abstractfactory.factories.MathsFactory;
import com.ontotext.javacourse.designpatterns.abstractfactory.factories.QuestionFactory;
import java.security.InvalidParameterException;
import java.util.Random;

/**
* This is a trivia game. Each game you start has rounds and every round the player gets a multiple
* choice question and an open answer question on a randomly selected topic - History, Maths or
* Geography.
*/
public class Game {
private static final Random random = new Random();
private static final String[] questionTopics = new String[] {"History", "Maths", "Geography"};

/** Selects a random topic and starts the round. */
public void startRound() {
String topic = pickRandomTopic();
QuestionFactory factory;
if (topic.equals("History")) {
factory = new HistoryFactory();
} else if (topic.equals("Maths")) {
factory = new MathsFactory();
} else if (topic.equals("Geography")) {
factory = new GeographyFactory();
} else {
throw new InvalidParameterException("Unknown topic!");
}
Round round = new Round(factory);
round.start();
}

private String pickRandomTopic() {
int randomNumber = random.nextInt(3);
return questionTopics[randomNumber];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ontotext.javacourse.designpatterns.abstractfactory;

import com.ontotext.javacourse.designpatterns.abstractfactory.factories.QuestionFactory;
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MultipleChoiceQuestion;
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.OpenQuestion;
import lombok.AllArgsConstructor;

/**
* The round class is a part of the game. Every round one multiple choice and one open answer
* questions are asked.
*/
@AllArgsConstructor
public class Round {
private QuestionFactory factory;

/** Starts the round and asks two questions both on the same randomly chosen topic. */
public void start() {
MultipleChoiceQuestion multipleChoiceQuestion = factory.createMultipleChoiceQuestion();
OpenQuestion openQuestion = factory.createOpenQuestion();
multipleChoiceQuestion.askQuestion();
openQuestion.writeAnswer("invalid answer");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.factories;

import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.GeographyMultipleChoiceQuestion;
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MultipleChoiceQuestion;
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.GeographyOpenQuestion;
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.OpenQuestion;

/**
* The GeographyFactory class contains method that create a multiple choice geography question and
* an open answer geography question.
*/
public class GeographyFactory implements QuestionFactory {
@Override
public MultipleChoiceQuestion createMultipleChoiceQuestion() {
return new GeographyMultipleChoiceQuestion();
}

@Override
public OpenQuestion createOpenQuestion() {
return new GeographyOpenQuestion();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.factories;

import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.HistoryMultipleChoiceQuestion;
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MultipleChoiceQuestion;
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.HistoryOpenQuestion;
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.OpenQuestion;

/**
* The HistoryFactory class contains method that create a multiple choice history question and an
* open answer history question.
*/
public class HistoryFactory implements QuestionFactory {
@Override
public MultipleChoiceQuestion createMultipleChoiceQuestion() {
return new HistoryMultipleChoiceQuestion();
}

@Override
public OpenQuestion createOpenQuestion() {
return new HistoryOpenQuestion();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.factories;

import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MathsMultipleChoiceQuestion;
import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MultipleChoiceQuestion;
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.MathsOpenQuestion;
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.OpenQuestion;

/**
* The MathsFactory class contains method that create a multiple choice maths question and an open
* answer maths question.
*/
public class MathsFactory implements QuestionFactory {
@Override
public MultipleChoiceQuestion createMultipleChoiceQuestion() {
return new MathsMultipleChoiceQuestion();
}

@Override
public OpenQuestion createOpenQuestion() {
return new MathsOpenQuestion();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.factories;

import com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions.MultipleChoiceQuestion;
import com.ontotext.javacourse.designpatterns.abstractfactory.openquestions.OpenQuestion;

/**
* Defines a question factory that can create a multiple choice question and an open question
* depending on the factory type.
*/
public interface QuestionFactory {
MultipleChoiceQuestion createMultipleChoiceQuestion();

OpenQuestion createOpenQuestion();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions;

/** This class defines a multiple choice geography question. */
public class GeographyMultipleChoiceQuestion implements MultipleChoiceQuestion {

@Override
public void askQuestion() {
System.out.println("Which is the capital city of Germany?");
}

@Override
public boolean checkAnswer(String answer) {
return answer.equalsIgnoreCase("berlin");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions;

/**
* This class defines a multiple choice history question.
*/
public class HistoryMultipleChoiceQuestion implements MultipleChoiceQuestion {

@Override
public void askQuestion() {
System.out.println("What year did WW2 start?");
}

@Override
public boolean checkAnswer(String answer) {
return answer.equalsIgnoreCase("1939");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions;

/** This class defines a multiple choice math question. */
public class MathsMultipleChoiceQuestion implements MultipleChoiceQuestion {

@Override
public void askQuestion() {
System.out.println("What is the value of PI?");
}

@Override
public boolean checkAnswer(String answer) {
return answer.equalsIgnoreCase("3.14");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.multiplechoicequestions;

/** Defines a multiple choice question that can check an answer is correct. */
public interface MultipleChoiceQuestion {
void askQuestion();

boolean checkAnswer(String answer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.openquestions;

/** This class defines an open geography question. */
public class GeographyOpenQuestion implements OpenQuestion {
@Override
public void writeAnswer(String answer) {
System.out.println(answer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.openquestions;

/**
* This class defines an open history question.
*/
public class HistoryOpenQuestion implements OpenQuestion {
@Override
public void writeAnswer(String answer) {
System.out.println(answer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.openquestions;

/** This class defines an open maths question. */
public class MathsOpenQuestion implements OpenQuestion {
@Override
public void writeAnswer(String answer) {
System.out.println(answer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ontotext.javacourse.designpatterns.abstractfactory.openquestions;

/** Defines an open question that can accept an answer. */
public interface OpenQuestion {
void writeAnswer(String answer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ public class Runner {
public static void main(String args[]) {

MailBuilder builder = new MailBuilder();
Mail mail =
builder
.from("")
.build();
Mail mail = builder.from("").to("sdfs").build();
System.out.println(
mail.getFrom()
+ mail.getTo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.ontotext.javacourse.designpatterns.mail.MissingFieldException;
import lombok.NoArgsConstructor;

/** Defines a mail builder */
/** Defines a mail builder. */
@NoArgsConstructor
public abstract class Builder {
protected Mail mail;
Expand All @@ -13,9 +13,9 @@ protected Builder(Mail mail) {
}

/**
* Builds the mail object. If the get field is empty MissingFieldException is thrown.
* Builds the mail object. If the from field is empty MissingFieldException is thrown.
*
* @return returns the constructed mail object
* @return the constructed mail object
*/
public Mail build() {
if (mail.getFrom().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.ontotext.javacourse.designpatterns.mail.builders;

/** Defines a mail builder which sets the mail attachments. */
/** Defines a mail builder which adds the mail attachments. */
public class BuilderAttachments extends Builder {
protected BuilderAttachments(Mail mail) {
super(mail);
}

/**
* Sets the mail attachments.
* Adds an attachment to the mail.
*
* @param attachment the mail attachment to add
* @return a BuilderAttachments object in case the client wants to add another attachment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ protected BuilderCC(Mail mail) {
}

/**
* Sets the mail cc.
* Sets the mail cc field.
*
* @param cc the cc of the mail
* @return a BuilderAttachments object to set the mail attachments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ protected BuilderContent(Mail mail) {
}

/**
* Sets the mail content.
* Sets the mail content field.
*
* @param content the content of the mail
* @return a BuilderCC object to set the cc of the mail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ protected BuilderSubject(Mail mail) {
}

/**
* Sets the mail subject.
* Sets the mail subject field.
*
* @param subject the subject of the mail
* @return a BuilderContent object to set the mail content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protected BuilderTo(Mail mail) {
}

/**
* Sets the mail to.
* Sets the mail to field.
*
* @param to the mail receiver
* @return a BuilderSubject object to set the mail subject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public MailBuilder() {
}

/**
* Sets the mail from.
* Sets the mail from field.
*
* @param from the sender of the mail
* @return BuilderTo object to set the receiver of the mail
Expand Down

0 comments on commit 9d01acf

Please sign in to comment.